Skip to content

Commit

Permalink
Minor simplifications and clean-ups in Toolchain processing
Browse files Browse the repository at this point in the history
Replace deprecated DefaultJavaToolChain by JavaToolchainImpl.
  • Loading branch information
HannesWell authored and laeubi committed Sep 24, 2023
1 parent 7a94e1c commit 3118388
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,12 @@ private ExecutionEnvironment getTargetExecutionEnvironment() {

@Override
public List<ClasspathEntry> getClasspath() throws MojoExecutionException {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
List<ClasspathEntry> classpath;
String dependencyScope = getDependencyScope();
if (Artifact.SCOPE_TEST.equals(dependencyScope)) {
classpath = new ArrayList<>(getBundleProject().getTestClasspath(DefaultReactorProject.adapt(project)));
} else {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
classpath = new ArrayList<>(getBundleProject().getClasspath(reactorProject));
}
if (extraClasspathElements != null) {
Expand Down Expand Up @@ -939,7 +939,7 @@ public List<ClasspathEntry> getClasspath() throws MojoExecutionException {
}
}
}
List<ClasspathEntry> uniqueClasspath = classpathMap.entrySet().stream().flatMap(entry -> {
return classpathMap.entrySet().stream().flatMap(entry -> {
List<ClasspathEntry> list = entry.getValue();
if (list.isEmpty()) {
return Stream.empty();
Expand All @@ -948,7 +948,7 @@ public List<ClasspathEntry> getClasspath() throws MojoExecutionException {
return list.stream();
}
ArtifactKey key = entry.getKey();
ReactorProject compositeProject = findProjectForKey(reactorProject, key);
ReactorProject compositeProject = findProjectForKey(key);
List<File> compositeFiles = list.stream().flatMap(cpe -> cpe.getLocations().stream()).toList();
Collection<AccessRule> compositeRules = mergeRules(list);
return Stream.of(new ClasspathEntry() {
Expand Down Expand Up @@ -983,7 +983,6 @@ public String toString() {

});
}).toList();
return uniqueClasspath;
}

private ArtifactKey normalizedKey(ArtifactKey key) {
Expand Down Expand Up @@ -1013,7 +1012,7 @@ private Collection<AccessRule> mergeRules(List<ClasspathEntry> list) {
return joinedRules;
}

private ReactorProject findProjectForKey(ReactorProject root, ArtifactKey key) {
private ReactorProject findProjectForKey(ArtifactKey key) {
for (MavenProject p : session.getProjects()) {
ReactorProject rp = DefaultReactorProject.adapt(p);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -31,15 +30,13 @@
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.osgi.internal.framework.EquinoxConfiguration;
import org.eclipse.tycho.ExecutionEnvironment;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.ExecutionEnvironment.SystemPackageEntry;
import org.eclipse.tycho.core.ee.StandardExecutionEnvironment.JavaInfo;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.Constants;
import org.osgi.framework.Version;

/**
* Creative copy&paste from org.eclipse.osgi.framework.internal.core.Framework
Expand Down Expand Up @@ -166,15 +163,14 @@ public static Toolchain getToolchainFor(String profileName, TargetEnvironment en
if (manager != null) {
logger.debug("Searching profile " + profileName + " in ToolchainManager");
//First try to find it by ID
for (Toolchain toolchain : manager.getToolchains(session, "jdk",
Collections.singletonMap("id", profileName))) {
for (Toolchain toolchain : manager.getToolchains(session, "jdk", Map.of("id", profileName))) {
return toolchain;
}
//Try find by version
int version = getVersion(profileName);
if (version > 8) {
for (Toolchain toolchain : manager.getToolchains(session, "jdk",
Collections.singletonMap("version", String.valueOf(version)))) {
Map.of("version", String.valueOf(version)))) {
return toolchain;
}
}
Expand All @@ -195,11 +191,8 @@ public static int getVersion(String profileName) {
String[] split = profileName.split("-");
if (split.length == 2) {
try {
double v = Double.parseDouble(split[split.length - 1]);
if (v < 8) {
return (int) ((v - 1.0) * 10);
}
return (int) v;
String version = split[1];
return Integer.parseInt(version.startsWith("1.") ? version.substring(2) : version);
} catch (NumberFormatException e) {
//can't check then...
}
Expand Down Expand Up @@ -289,23 +282,4 @@ private static Properties createProfileJvm(int javaVersion, Collection<String> p
props.setProperty("org.eclipse.jdt.core.compiler.problem.enumIdentifier", "error");
return props;
}

public static boolean isCompatibleIU(IInstallableUnit iu, String profileName) {
if (iu.getArtifacts().isEmpty()) {
return false;
}
int version = getVersion(profileName);
int maxEE = iu.getProvidedCapabilities().stream().filter(cap -> "osgi.ee".equals(cap.getNamespace()))
.mapToInt(cap -> {
if (cap.getVersion().isOSGiCompatible()) {
Version v = new Version(cap.getVersion().toString());
if (v.getMajor() < 8) {
return v.getMinor();
}
return v.getMajor();
}
return -1;
}).max().orElse(-1);
return maxEE == version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
*******************************************************************************/
package org.eclipse.tycho.core.maven;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.io.FilenameUtils;
import org.apache.maven.toolchain.java.JavaToolchain;
import org.eclipse.tycho.TargetEnvironment;

final class JavaHomeToolchain implements JavaToolchain {

private String javaHome;
private Path javaHome;

public JavaHomeToolchain(String javaHome) {
this.javaHome = javaHome;
this.javaHome = Path.of(javaHome).toAbsolutePath();
}

@Override
Expand All @@ -34,35 +35,25 @@ public String getType() {

@Override
public String findTool(String toolName) {
File bin = new File(javaHome, "bin");
if (bin.exists()) {
File tool;
if (TargetEnvironment.getRunningEnvironment().isWindows()) {
tool = new File(bin, toolName + ".exe");
} else {
tool = new File(bin, toolName);
}
if (tool.exists()) {
return tool.getAbsolutePath();
Path bin = javaHome.resolve("bin");
if (Files.isDirectory(bin)) {
Path tool = bin.resolve(toolName + (TargetEnvironment.getRunningEnvironment().isWindows() ? ".exe" : ""));
if (Files.isRegularFile(tool)) {
return tool.toString();
}
//last resort just in case other extension or case-sensitive file-system...
File[] listFiles = bin.listFiles(new FileFilter() {

@Override
public boolean accept(File pathname) {
return pathname.isFile()
&& FilenameUtils.getBaseName(pathname.getName().toLowerCase()).equals(toolName);
}
});
if (listFiles != null && listFiles.length > 0) {
return listFiles[0].getAbsolutePath();
try (var files = Files.list(bin).filter(Files::isRegularFile)) {
return files.map(Path::toString)
.filter(pathname -> FilenameUtils.getBaseName(pathname).equalsIgnoreCase(toolName)).findFirst()
.orElse(null);
} catch (IOException e) {
}
}
return null;
}

public String getJavaHome() {
return javaHome;
return javaHome.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainPrivate;
import org.apache.maven.toolchain.java.JavaToolchainImpl;
import org.codehaus.plexus.util.xml.Xpp3Dom;

public class OSGiJavaToolchain implements Toolchain {
Expand All @@ -37,7 +38,7 @@ public String findTool(String toolName) {
}

public String getJavaHome() {
if (base instanceof @SuppressWarnings("deprecation") org.apache.maven.toolchain.java.DefaultJavaToolChain defaultToolchain) {
if (base instanceof JavaToolchainImpl defaultToolchain) {
return defaultToolchain.getJavaHome();
}
if (base instanceof JavaHomeToolchain javaHomeToolchain) {
Expand All @@ -52,10 +53,9 @@ public String getJavaHome() {
}

public Xpp3Dom getConfiguration() {
if (base instanceof ToolchainPrivate privateToolchain) {
if (privateToolchain.getModel().getConfiguration() instanceof Xpp3Dom xpp3) {
return xpp3;
}
if (base instanceof ToolchainPrivate privateToolchain
&& privateToolchain.getModel().getConfiguration() instanceof Xpp3Dom xpp3) {
return xpp3;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.maven.toolchain.ToolchainManager;
import org.apache.maven.toolchain.ToolchainManagerPrivate;
import org.apache.maven.toolchain.ToolchainPrivate;
import org.apache.maven.toolchain.java.DefaultJavaToolChain;
import org.apache.maven.toolchain.java.JavaToolchainImpl;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
Expand Down Expand Up @@ -123,8 +123,8 @@ private MavenSession getMavenSession() {
}

/**
* Finds a matching {@link DefaultJavaToolChain} in the maven toolchains for a given maven
* session and toolchain id. Returns the toolchain or null if no toolchain could be found.
* Finds a matching {@link JavaToolchainImpl} in the maven toolchains for a given maven session
* and toolchain id. Returns the toolchain or null if no toolchain could be found.
*
* @param session
* The maven session
Expand All @@ -134,15 +134,14 @@ private MavenSession getMavenSession() {
* @throws MojoExecutionException
* if the toolchains are misconfigured
*/
public DefaultJavaToolChain findMatchingJavaToolChain(final MavenSession session, final String toolchainId)
public JavaToolchainImpl findMatchingJavaToolChain(final MavenSession session, final String toolchainId)
throws MojoExecutionException {
try {
final Map<String, String> requirements = Collections.singletonMap("id", toolchainId);
for (ToolchainPrivate javaToolChain : toolChainManager.getToolchainsForType("jdk", session)) {
if (javaToolChain.matchesRequirements(requirements)) {
if (javaToolChain instanceof DefaultJavaToolChain defaultJavaToolchain) {
return defaultJavaToolchain;
}
if (javaToolChain.matchesRequirements(requirements)
&& javaToolChain instanceof JavaToolchainImpl javaToolchain) {
return javaToolchain;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.testing.SilentLog;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.java.JavaToolchainImpl;
import org.eclipse.sisu.equinox.launching.EquinoxInstallation;
import org.eclipse.sisu.equinox.launching.EquinoxLauncher;
import org.eclipse.sisu.equinox.launching.LaunchConfiguration;
Expand Down Expand Up @@ -153,9 +154,7 @@ public void testExecutionEnvironmentIsRespectedDuringDependencyResolution() thro
}

public void testExecutionEnvironmentIsRespectedDuringEclipseExecution() throws Exception {
@SuppressWarnings("deprecation")
org.apache.maven.toolchain.java.DefaultJavaToolChain mockToolchainForCustomEE = mock(
org.apache.maven.toolchain.java.DefaultJavaToolChain.class);
JavaToolchainImpl mockToolchainForCustomEE = mock(JavaToolchainImpl.class);
when(mockToolchainForCustomEE.findTool("java")).thenReturn("/path/to/custom-ee-jdk/bin/java");
when(toolchainProvider.findMatchingJavaToolChain(any(), eq("custom-ee"))).thenReturn(mockToolchainForCustomEE);

Expand Down

0 comments on commit 3118388

Please sign in to comment.