Skip to content

Commit 9360485

Browse files
committed
Move BREE calculation to project manager
Currently the AbstractOsgiCompilerMojo contains code to calculate the wanted BREEs from the manifest and map them to available execution environments, but this can be useful on other places as well. This now extracts the part into the TychoProjectManager to make it mor general available.
1 parent 30a6a70 commit 9360485

File tree

3 files changed

+56
-44
lines changed

3 files changed

+56
-44
lines changed

tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import org.eclipse.aether.resolution.DependencyResolutionException;
6666
import org.eclipse.jdt.internal.compiler.util.CtSym;
6767
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
68-
import org.eclipse.osgi.util.ManifestElement;
6968
import org.eclipse.tycho.ArtifactKey;
7069
import org.eclipse.tycho.ClasspathEntry;
7170
import org.eclipse.tycho.ClasspathEntry.AccessRule;
@@ -98,13 +97,7 @@
9897
import org.eclipse.tycho.model.classpath.M2ClasspathVariable;
9998
import org.eclipse.tycho.model.classpath.PluginDependenciesClasspathContainer;
10099
import org.eclipse.tycho.model.classpath.ProjectClasspathEntry;
101-
import org.osgi.framework.Constants;
102-
import org.osgi.framework.Filter;
103-
import org.osgi.framework.FrameworkUtil;
104-
import org.osgi.framework.InvalidSyntaxException;
105100
import org.osgi.framework.Version;
106-
import org.osgi.framework.namespace.ExecutionEnvironmentNamespace;
107-
import org.osgi.resource.Namespace;
108101

109102
import copied.org.apache.maven.plugin.AbstractCompilerMojo;
110103

@@ -348,7 +341,7 @@ public abstract class AbstractOsgiCompilerMojo extends AbstractCompilerMojo impl
348341
@Component
349342
private MavenDependenciesResolver dependenciesResolver;
350343

351-
private StandardExecutionEnvironment[] manifestBREEs;
344+
private ExecutionEnvironment[] manifestBREEs;
352345

353346
private File currentOutputDirectory;
354347

@@ -458,45 +451,15 @@ protected void doFinish() throws MojoExecutionException {
458451
/**
459452
* Only public for tests purpose!
460453
*/
461-
public StandardExecutionEnvironment[] getBREE() {
454+
public ExecutionEnvironment[] getBREE() {
462455
if (currentRelease != null) {
463456
//if there is an explicit release set we know the release and there must be a suitable EE provided
464457
return new StandardExecutionEnvironment[] { ExecutionEnvironmentUtils
465458
.getExecutionEnvironment("JavaSE-" + currentRelease, toolchainManager, session, logger) };
466459
}
467460
if (manifestBREEs == null) {
468-
OsgiManifest manifest = bundleReader.loadManifest(project.getBasedir());
469-
manifestBREEs = Arrays.stream(manifest.getExecutionEnvironments())
470-
.map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session, logger))
471-
.toArray(StandardExecutionEnvironment[]::new);
472-
if (manifestBREEs.length == 0) {
473-
ManifestElement[] requireCapability = manifest.getManifestElements(Constants.REQUIRE_CAPABILITY);
474-
if (requireCapability != null) {
475-
List<Filter> eeFilters = Arrays.stream(requireCapability)
476-
.filter(element -> ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE
477-
.equals(element.getValue())) //
478-
.map(element -> element.getDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE)) //
479-
.map(filterDirective -> {
480-
try {
481-
return FrameworkUtil.createFilter(filterDirective);
482-
} catch (InvalidSyntaxException e) {
483-
e.printStackTrace();
484-
return null;
485-
}
486-
}).filter(Objects::nonNull).toList();
487-
manifestBREEs = ExecutionEnvironmentUtils.getProfileNames(toolchainManager, session, logger)
488-
.stream() //
489-
.map(name -> name.split("-")) //
490-
.map(segments -> Map.of(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE,
491-
segments[0], "version", segments[1]))
492-
.filter(eeCapability -> eeFilters.stream().anyMatch(filter -> filter.matches(eeCapability)))
493-
.map(ee -> ee.get(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE) + '-'
494-
+ ee.get("version"))
495-
.map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session,
496-
logger))
497-
.toArray(StandardExecutionEnvironment[]::new);
498-
}
499-
}
461+
manifestBREEs = tychoProjectManager.getExecutionEnvironments(project, session)
462+
.toArray(ExecutionEnvironment[]::new);
500463
}
501464
return manifestBREEs;
502465
}
@@ -828,7 +791,7 @@ private List<AccessRule> getStrictBootClasspathAccessRules() throws MojoExecutio
828791

829792
private void configureJavaHome(CompilerConfiguration compilerConfiguration) throws MojoExecutionException {
830793
if (useJDK == JDKUsage.BREE) {
831-
StandardExecutionEnvironment[] brees = getBREE();
794+
ExecutionEnvironment[] brees = getBREE();
832795
String toolchainId;
833796
if (brees.length > 0) {
834797
toolchainId = brees[0].getProfileName();
@@ -1155,9 +1118,9 @@ public String getReleaseLevel() throws MojoExecutionException {
11551118
}
11561119

11571120
private void checkTargetLevelCompatibleWithManifestBREEs(String effectiveTargetLevel,
1158-
StandardExecutionEnvironment[] manifestBREEs) throws MojoExecutionException {
1121+
ExecutionEnvironment[] manifestBREEs) throws MojoExecutionException {
11591122
List<String> incompatibleBREEs = new ArrayList<>();
1160-
for (StandardExecutionEnvironment ee : manifestBREEs) {
1123+
for (ExecutionEnvironment ee : manifestBREEs) {
11611124
if (!ee.isCompatibleCompilerTargetLevel(effectiveTargetLevel)) {
11621125
incompatibleBREEs.add(ee.getProfileName() + " (assumes " + ee.getCompilerTargetLevelDefault() + ")");
11631126
}

tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.File;
1616
import java.io.IOException;
1717
import java.nio.file.Path;
18+
import java.util.Arrays;
1819
import java.util.Collection;
1920
import java.util.HashSet;
2021
import java.util.List;
@@ -23,6 +24,7 @@
2324
import java.util.Optional;
2425
import java.util.Set;
2526
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.stream.Stream;
2628

2729
import javax.inject.Inject;
2830

@@ -38,11 +40,13 @@
3840
import org.codehaus.plexus.logging.Logger;
3941
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
4042
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
43+
import org.eclipse.osgi.util.ManifestElement;
4144
import org.eclipse.tycho.ArtifactDescriptor;
4245
import org.eclipse.tycho.ArtifactKey;
4346
import org.eclipse.tycho.ClasspathEntry;
4447
import org.eclipse.tycho.DefaultArtifactKey;
4548
import org.eclipse.tycho.DependencyArtifacts;
49+
import org.eclipse.tycho.ExecutionEnvironment;
4650
import org.eclipse.tycho.ExecutionEnvironmentConfiguration;
4751
import org.eclipse.tycho.ReactorProject;
4852
import org.eclipse.tycho.ResolvedArtifactKey;
@@ -52,6 +56,7 @@
5256
import org.eclipse.tycho.TychoConstants;
5357
import org.eclipse.tycho.classpath.ClasspathContributor;
5458
import org.eclipse.tycho.core.ee.ExecutionEnvironmentConfigurationImpl;
59+
import org.eclipse.tycho.core.ee.ExecutionEnvironmentUtils;
5560
import org.eclipse.tycho.core.osgitools.AbstractTychoProject;
5661
import org.eclipse.tycho.core.osgitools.BundleReader;
5762
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
@@ -65,6 +70,12 @@
6570
import org.eclipse.tycho.model.project.EclipseProject;
6671
import org.eclipse.tycho.p2maven.tmp.BundlesAction;
6772
import org.eclipse.tycho.targetplatform.TargetDefinition;
73+
import org.osgi.framework.Constants;
74+
import org.osgi.framework.Filter;
75+
import org.osgi.framework.FrameworkUtil;
76+
import org.osgi.framework.InvalidSyntaxException;
77+
import org.osgi.framework.namespace.ExecutionEnvironmentNamespace;
78+
import org.osgi.resource.Namespace;
6879

6980
import aQute.bnd.osgi.Processor;
7081

@@ -334,4 +345,38 @@ public Optional<TargetPlatform> getTargetPlatform(MavenProject project) {
334345

335346
}
336347

348+
public Stream<ExecutionEnvironment> getExecutionEnvironments(MavenProject project, MavenSession session) {
349+
OsgiManifest manifest = bundleReader.loadManifest(project.getBasedir());
350+
ExecutionEnvironment[] manifestBREEs = Arrays.stream(manifest.getExecutionEnvironments())
351+
.map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session, logger))
352+
.toArray(ExecutionEnvironment[]::new);
353+
if (manifestBREEs.length == 0) {
354+
ManifestElement[] requireCapability = manifest.getManifestElements(Constants.REQUIRE_CAPABILITY);
355+
if (requireCapability != null) {
356+
List<Filter> eeFilters = Arrays.stream(requireCapability)
357+
.filter(element -> ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE
358+
.equals(element.getValue())) //
359+
.map(element -> element.getDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE)) //
360+
.map(filterDirective -> {
361+
try {
362+
return FrameworkUtil.createFilter(filterDirective);
363+
} catch (InvalidSyntaxException e) {
364+
e.printStackTrace();
365+
return null;
366+
}
367+
}).filter(Objects::nonNull).toList();
368+
return ExecutionEnvironmentUtils.getProfileNames(toolchainManager, session, logger).stream() //
369+
.map(name -> name.split("-")) //
370+
.map(segments -> Map.of(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE,
371+
segments[0], "version", segments[1]))
372+
.filter(eeCapability -> eeFilters.stream().anyMatch(filter -> filter.matches(eeCapability)))
373+
.map(ee -> ee.get(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE) + '-'
374+
+ ee.get("version"))
375+
.map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session,
376+
logger));
377+
}
378+
}
379+
return Arrays.stream(manifestBREEs);
380+
}
381+
337382
}

tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public ToolchainProvider(MavenSession mavenSession) {
6868
this.mavenSession = mavenSession;
6969
}
7070

71+
public Optional<OSGiJavaToolchain> getToolchain(String profileName) {
72+
return getToolchain(JDKUsage.BREE, profileName).or(() -> getSystemToolchain());
73+
}
74+
7175
public Optional<OSGiJavaToolchain> getToolchain(JDKUsage usage, String profileName) {
7276
if (usage == JDKUsage.SYSTEM) {
7377
return getSystemToolchain();

0 commit comments

Comments
 (0)