From 42ed378c52d72f8fc8bfe826da7f2ed226747601 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 29 Apr 2023 13:01:41 +0200 Subject: [PATCH] Minor simplifications and clean-ups in Toolchain processing Replace deprecated DefaultJavaToolChain by JavaToolchainImpl. --- .../compiler/AbstractOsgiCompilerMojo.java | 9 ++-- .../core/ee/ExecutionEnvironmentUtils.java | 34 ++------------- .../tycho/core/maven/JavaHomeToolchain.java | 41 ++++++++----------- .../tycho/core/maven/OSGiJavaToolchain.java | 10 ++--- .../tycho/core/maven/ToolchainProvider.java | 15 ++++--- .../extras/eclipserun/EclipseRunMojoTest.java | 5 +-- 6 files changed, 38 insertions(+), 76 deletions(-) diff --git a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java index a6b8ec40f8..05598dd3c8 100644 --- a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java +++ b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java @@ -882,12 +882,12 @@ private ExecutionEnvironment getTargetExecutionEnvironment() { @Override public List getClasspath() throws MojoExecutionException { - ReactorProject reactorProject = DefaultReactorProject.adapt(project); List 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) { @@ -939,7 +939,7 @@ public List getClasspath() throws MojoExecutionException { } } } - List uniqueClasspath = classpathMap.entrySet().stream().flatMap(entry -> { + return classpathMap.entrySet().stream().flatMap(entry -> { List list = entry.getValue(); if (list.isEmpty()) { return Stream.empty(); @@ -948,7 +948,7 @@ public List getClasspath() throws MojoExecutionException { return list.stream(); } ArtifactKey key = entry.getKey(); - ReactorProject compositeProject = findProjectForKey(reactorProject, key); + ReactorProject compositeProject = findProjectForKey(key); List compositeFiles = list.stream().flatMap(cpe -> cpe.getLocations().stream()).toList(); Collection compositeRules = mergeRules(list); return Stream.of(new ClasspathEntry() { @@ -983,7 +983,6 @@ public String toString() { }); }).toList(); - return uniqueClasspath; } private ArtifactKey normalizedKey(ArtifactKey key) { @@ -1013,7 +1012,7 @@ private Collection mergeRules(List 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 { diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/ee/ExecutionEnvironmentUtils.java b/tycho-core/src/main/java/org/eclipse/tycho/core/ee/ExecutionEnvironmentUtils.java index a618a962a4..629934f169 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/ee/ExecutionEnvironmentUtils.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/ee/ExecutionEnvironmentUtils.java @@ -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; @@ -31,7 +30,6 @@ 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; @@ -39,7 +37,6 @@ 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 @@ -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; } } @@ -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... } @@ -289,23 +282,4 @@ private static Properties createProfileJvm(int javaVersion, Collection 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; - } } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/JavaHomeToolchain.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/JavaHomeToolchain.java index d7c1ecc53a..524859a84b 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/JavaHomeToolchain.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/JavaHomeToolchain.java @@ -12,8 +12,9 @@ *******************************************************************************/ 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; @@ -21,10 +22,10 @@ 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 @@ -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 diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/OSGiJavaToolchain.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/OSGiJavaToolchain.java index 61342a4240..facecd2250 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/OSGiJavaToolchain.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/OSGiJavaToolchain.java @@ -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 { @@ -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) { @@ -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; } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java index 38fec1b0e3..221af779f4 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java @@ -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; @@ -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 @@ -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 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; diff --git a/tycho-extras/tycho-eclipserun-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java b/tycho-extras/tycho-eclipserun-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java index 5880c7433d..76b8f0fca1 100644 --- a/tycho-extras/tycho-eclipserun-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java +++ b/tycho-extras/tycho-eclipserun-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java @@ -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; @@ -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);