Skip to content

Commit

Permalink
Consider runbundles when computing dependencies
Browse files Browse the repository at this point in the history
If we have a bndrun activated then the runbundles become dependencies of
the maven project.

This now calculates the enabled bndruns and return the bundle names so
they can be used to add required reactor projects.

(cherry picked from commit 807bd8e)
  • Loading branch information
laeubi authored and eclipse-tycho-bot committed Feb 3, 2025
1 parent 099af7f commit 483cc67
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2025 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.bnd;

import java.nio.file.Path;

public record BndRunFile(String name, Path path) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,26 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.bnd.BndRunFile;
import org.eclipse.tycho.bnd.mojos.BndRunMojo;
import org.eclipse.tycho.core.bnd.BndPluginManager;

import aQute.bnd.build.Project;
import aQute.bnd.build.SubProject;
import aQute.bnd.build.Workspace;
import aQute.bnd.osgi.Constants;
import biz.aQute.resolve.Bndrun;

/**
* This component injects information from the BND model into the maven model,
Expand All @@ -58,6 +67,7 @@
public class BndMavenLifecycleParticipant extends AbstractMavenLifecycleParticipant {

private static final Set<Entry<String, String>> BND_TO_MAVEN_MAPPING = Map.of(//
Constants.RUNBUNDLES, Artifact.SCOPE_RUNTIME, //
Constants.DEPENDSON, Artifact.SCOPE_RUNTIME, //
Constants.BUILDPATH, Artifact.SCOPE_COMPILE, //
Constants.TESTPATH, Artifact.SCOPE_TEST //
Expand Down Expand Up @@ -98,7 +108,7 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
Project bndProject = entry.getValue();
try {
for (Entry<String, String> mapping : BND_TO_MAVEN_MAPPING) {
Set<String> requirements = bndProject.getMergedParameters(mapping.getKey()).keySet();
Set<String> requirements = getRequirements(bndProject, mavenProject, session, mapping.getKey());
String mavenScope = mapping.getValue();
for (String required : requirements) {
BndMavenProject bndMavenProject = bndWorkspaceProjects.get(required);
Expand Down Expand Up @@ -165,6 +175,62 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
}
}

private Set<String> getRequirements(Project bndProject, MavenProject mavenProject, MavenSession session,
String property) throws MavenExecutionException {
if (Constants.RUNBUNDLES.equals(property)) {
// run bundles are from bndruns and must be handled different
Plugin plugin = mavenProject.getPlugin(Plugin.constructKey("org.eclipse.tycho", "tycho-bnd-plugin"));
Set<String> selectedBndRuns = new HashSet<>();
if (plugin != null) {
for (PluginExecution pluginExecution : plugin.getExecutions()) {
if (pluginExecution.getGoals().contains(BndRunMojo.NAME)) {
String exportsProperty = session.getUserProperties()
.getProperty(BndRunMojo.BNDRUN_EXPORTS_PROPERTY);
if (exportsProperty == null) {
Object configuration = pluginExecution.getConfiguration();
if (configuration instanceof Xpp3Dom dom) {
XmlPlexusConfiguration cfg = new XmlPlexusConfiguration(dom);
PlexusConfiguration child = cfg.getChild(BndRunMojo.BNDRUN_EXPORTS_NAME, false);
if (child != null) {
PlexusConfiguration[] children = child.getChildren();
for (PlexusConfiguration c : children) {
selectedBndRuns.add(c.getValue());
}
}
}
} else {
for (String run : exportsProperty.split(",")) {
selectedBndRuns.add(run);
}
}
}
}
}
if (selectedBndRuns.isEmpty()) {
return Set.of();
}
try {
List<BndRunFile> bndRuns = BndRunMojo.getBndRuns(mavenProject.getBasedir().toPath(), selectedBndRuns);
if (bndRuns.isEmpty()) {
return Set.of();
}
Set<String> dependencies = new HashSet<>();
for (BndRunFile runFile : bndRuns) {
try {
Bndrun bndrun = Bndrun.createBndrun(bndProject.getWorkspace(), runFile.path().toFile());
dependencies.addAll(bndrun.getMergedParameters(property).keySet());
} catch (Exception e) {
throw new MavenExecutionException("can't read required bnd run " + runFile.path(), e);
}
}
return dependencies;
} catch (MojoExecutionException e) {
throw new MavenExecutionException("can't read required bnd runs", e.getCause());
}
}
return bndProject.getMergedParameters(property).keySet();
}

private Map<String, BndMavenProject> getManifestFirstProjects(MavenSession session, Set<MavenProject> existing) {
Map<String, BndMavenProject> result = new HashMap<>();
for (MavenProject mavenProject : session.getProjects()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProjectHelper;
import org.eclipse.tycho.bnd.BndRunFile;
import org.osgi.service.resolver.ResolutionException;

import aQute.bnd.build.Container;
Expand All @@ -46,12 +47,18 @@
import biz.aQute.resolve.Bndrun;
import biz.aQute.resolve.ResolveProcess;

@Mojo(name = "run", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
@Mojo(name = BndRunMojo.NAME, defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
public class BndRunMojo extends AbstractBndMojo {

private static final String BNDRUN = ".bndrun";
public static final String NAME = "run";

@Parameter(property = "bndrun.exports")
public static final String BNDRUN_EXPORTS_NAME = "exports";

public static final String BNDRUN_EXPORTS_PROPERTY = "bndrun.exports";

public static final String BNDRUN = ".bndrun";

@Parameter(name = BNDRUN_EXPORTS_NAME, property = BNDRUN_EXPORTS_PROPERTY)
private Set<String> exports;

@Parameter
Expand All @@ -74,7 +81,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (exports == null || exports.isEmpty()) {
return;
}
List<BndRunFile> bndRuns = getBndRuns();
List<BndRunFile> bndRuns = getBndRuns(mavenProject.getBasedir().toPath(), exports);
if (bndRuns.isEmpty()) {
return;
}
Expand Down Expand Up @@ -178,17 +185,16 @@ private Bndrun getBndRun(Workspace workspace, BndRunFile run) throws MojoFailure
return bndRun;
}

private List<BndRunFile> getBndRuns() throws MojoExecutionException {
public static List<BndRunFile> getBndRuns(Path path, Collection<String> match) throws MojoExecutionException {
List<BndRunFile> bndRuns = new ArrayList<>();
Path path = mavenProject.getBasedir().toPath();
try {
Iterator<Path> iterator = Files.list(path).iterator();
while (iterator.hasNext()) {
Path child = iterator.next();
String fn = child.getFileName().toString();
if (fn.endsWith(BNDRUN)) {
String key = fn.substring(0, fn.length() - BNDRUN.length());
if (exports.contains(key)) {
if (match.contains(key)) {
bndRuns.add(new BndRunFile(key, child));
}
}
Expand All @@ -199,8 +205,4 @@ private List<BndRunFile> getBndRuns() throws MojoExecutionException {
return bndRuns;
}

private static record BndRunFile(String name, Path path) {

}

}

0 comments on commit 483cc67

Please sign in to comment.