Skip to content

Commit

Permalink
#10 reworked host OS processing to replace default extensions in tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
raydac committed Mar 19, 2023
1 parent a08da23 commit 3a0b6eb
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 78 deletions.
8 changes: 4 additions & 4 deletions mvn-jlink-wrapper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
<version>20230227</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.0</version>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand All @@ -36,7 +36,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
<version>1.22</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
Expand All @@ -56,7 +56,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
<version>4.5.14</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.igormaznitsa.meta.common.utils.Assertions;
import com.igormaznitsa.mvnjlink.exceptions.IORuntimeWrapperException;
import com.igormaznitsa.mvnjlink.mojos.AbstractJdkToolMojo;
import com.igormaznitsa.mvnjlink.utils.HostOs;
import com.igormaznitsa.mvnjlink.utils.HttpUtils;
import com.igormaznitsa.mvnjlink.utils.StringUtils;
import java.io.File;
Expand All @@ -45,11 +46,9 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.SystemUtils;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
Expand All @@ -61,7 +60,6 @@

public abstract class AbstractJdkProvider {

protected static final Pattern ETAG_PATTERN = Pattern.compile("^\"?([a-fA-F0-9]{32}).*\"?$");
protected static final String[] MIME_TEXT =
new String[] {"text/plain", "application/octet-stream"};
protected final AbstractJdkToolMojo mojo;
Expand Down Expand Up @@ -208,24 +206,9 @@ protected boolean isOfflineMode() {
}

@Nonnull
protected String findCurrentOs(@Nonnull final String macOsId) {
final String defaultOs;
if (SystemUtils.IS_OS_MAC) {
defaultOs = macOsId;
} else if (SystemUtils.IS_OS_WINDOWS) {
defaultOs = "windows";
} else if (SystemUtils.IS_OS_AIX) {
defaultOs = "aix";
} else if (SystemUtils.IS_OS_FREE_BSD) {
defaultOs = "freebsd";
} else if (SystemUtils.IS_OS_IRIX) {
defaultOs = "irix";
} else if (SystemUtils.IS_OS_ZOS) {
defaultOs = "zos";
} else {
defaultOs = "linux";
}
return defaultOs;
protected HostOs findCurrentOs(@Nonnull final HostOs defaultHostOs) {
final HostOs hostOs = HostOs.findHostOs();
return hostOs == HostOs.UNKNOWN ? defaultHostOs : hostOs;
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.igormaznitsa.mvnjlink.jdkproviders.AbstractJdkProvider;
import com.igormaznitsa.mvnjlink.mojos.AbstractJdkToolMojo;
import com.igormaznitsa.mvnjlink.utils.ArchUtils;
import com.igormaznitsa.mvnjlink.utils.HostOs;
import com.igormaznitsa.mvnjlink.utils.HttpUtils;
import com.igormaznitsa.mvnjlink.utils.WildCardMatcher;
import java.io.IOException;
Expand Down Expand Up @@ -94,15 +95,15 @@ public Path getPathToJdk(

assertParameters(config, "version", "arch", "type", "impl", "build");

final String defaultOs = findCurrentOs("macos");
final HostOs defaultOs = findCurrentOs(HostOs.LINUX);

log.debug("Default OS recognized as: " + defaultOs);

final String jdkVersion = config.get("version").toUpperCase(ENGLISH);
final String gitRepositoryName =
config.getOrDefault("repositoryName", "temurin" + getRawVersion(jdkVersion) + "-binaries");
final String build = config.get("build");
final String jdkOs = GetUtils.ensureNonNull(config.get("os"), defaultOs);
final String jdkOs = GetUtils.ensureNonNull(config.get("os"), defaultOs.getId());
final String jdkArch = config.get("arch");
final String jdkType = config.get("type");
final String jdkImpl = config.get("impl");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.igormaznitsa.mvnjlink.jdkproviders.AbstractJdkProvider;
import com.igormaznitsa.mvnjlink.mojos.AbstractJdkToolMojo;
import com.igormaznitsa.mvnjlink.utils.ArchUtils;
import com.igormaznitsa.mvnjlink.utils.HostOs;
import com.igormaznitsa.mvnjlink.utils.HttpUtils;
import com.igormaznitsa.mvnjlink.utils.StringUtils;
import com.igormaznitsa.mvnjlink.utils.WildCardMatcher;
Expand Down Expand Up @@ -80,13 +81,15 @@ public Path getPathToJdk(

assertParameters(config, "type", "version", "arch");

final String defaultOs = findCurrentOs("darwin");
final HostOs defaultOs = findCurrentOs(HostOs.LINUX);
final String defaultOsId =
defaultOs == HostOs.MAC_OSX || defaultOs == HostOs.MAC ? "darwin" : defaultOs.getId();

log.debug("Default OS recognized as: " + defaultOs);
log.debug("Default OS recognized as: " + defaultOsId);

final String jdkType = config.get("type");
final String jdkVersion = config.get("version");
final String jdkOs = GetUtils.ensureNonNull(config.get("os"), defaultOs);
final String jdkOs = GetUtils.ensureNonNull(config.get("os"), defaultOsId);
final String jdkArch = config.get("arch");
final boolean checkArchive = Boolean.parseBoolean(config.getOrDefault("check", "true"));
final int perPage = ensurePageSizeValue(config.getOrDefault("perPage", "40"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.igormaznitsa.mvnjlink.jdkproviders.AbstractJdkProvider;
import com.igormaznitsa.mvnjlink.mojos.AbstractJdkToolMojo;
import com.igormaznitsa.mvnjlink.utils.ArchUtils;
import com.igormaznitsa.mvnjlink.utils.HostOs;
import com.igormaznitsa.mvnjlink.utils.HttpUtils;
import com.igormaznitsa.mvnjlink.utils.StringUtils;
import com.igormaznitsa.mvnjlink.utils.WildCardMatcher;
Expand Down Expand Up @@ -93,13 +94,13 @@ public Path getPathToJdk(

assertParameters(config, "type", "version", "arch");

final String defaultOs = findCurrentOs("macos");
final HostOs defaultOs = findCurrentOs(HostOs.LINUX);

log.debug("Default OS recognized as: " + defaultOs);

final String jdkType = config.get("type");
final String jdkVersion = config.get("version");
final String jdkOs = GetUtils.ensureNonNull(config.get("os"), defaultOs);
final String jdkOs = GetUtils.ensureNonNull(config.get("os"), defaultOs.getId());
final String jdkArch = config.get("arch");
final int perPage = ensurePageSizeValue(config.getOrDefault("perPage", "40"));
final boolean checkArchive = Boolean.parseBoolean(config.getOrDefault("check", "true"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.igormaznitsa.meta.annotation.MustNotContainNull;
import com.igormaznitsa.mvnjlink.mojos.AbstractJdkToolMojo;
import com.igormaznitsa.mvnjlink.utils.HostOs;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
Expand All @@ -22,8 +22,8 @@ public MicrosoftJdkProvider(@Nonnull final AbstractJdkToolMojo mojo) {

@Nonnull
private String findAppropriateExtension() {
final String os = this.findCurrentOs("macos");
if (os.toLowerCase(Locale.ENGLISH).contains("windows")) {
final HostOs os = this.findCurrentOs(HostOs.LINUX);
if (os == HostOs.WINDOWS) {
return "zip";
} else {
return "tar.gz";
Expand All @@ -42,7 +42,11 @@ public final Path getPathToJdk(@Nullable final String authorization,

final String jdkType = config.getOrDefault("type", "jdk");
final String jdkVersion = config.getOrDefault("version", "17.0.4.1");
final String jdkOs = config.getOrDefault("os", findCurrentOs("macOs"));

final HostOs hostOs = findCurrentOs(HostOs.LINUX);

final String jdkOs = config.getOrDefault("os",
(hostOs == HostOs.MAC || hostOs == HostOs.MAC_OSX ? "macOs" : hostOs.getId()));
final String jdkArch = config.getOrDefault("arch", "x64");
final String jdkExtension = config.getOrDefault("extension", this.findAppropriateExtension());
final String sha256 = config.getOrDefault("sha256", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.igormaznitsa.mvnjlink.jdkproviders.AbstractJdkProvider;
import com.igormaznitsa.mvnjlink.mojos.AbstractJdkToolMojo;
import com.igormaznitsa.mvnjlink.utils.ArchUtils;
import com.igormaznitsa.mvnjlink.utils.HostOs;
import com.igormaznitsa.mvnjlink.utils.HttpUtils;
import com.igormaznitsa.mvnjlink.utils.StringUtils;
import com.igormaznitsa.mvnjlink.utils.WildCardMatcher;
Expand Down Expand Up @@ -100,7 +101,9 @@ public Path getPathToJdk(

assertParameters(config, "type", "version", "arch");

final String defaultOs = findCurrentOs("osx");
final HostOs hostOs = findCurrentOs(HostOs.LINUX);
final String defaultOs =
hostOs == HostOs.MAC || hostOs == HostOs.MAC_OSX ? "osx" : hostOs.getId();

log.debug("Default OS recognized as: " + defaultOs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.igormaznitsa.mvnjlink.mojos;

import com.igormaznitsa.meta.annotation.MustNotContainNull;
import com.igormaznitsa.mvnjlink.exceptions.FailureException;
import com.igormaznitsa.mvnjlink.jdkproviders.JdkProviderId;
import com.igormaznitsa.mvnjlink.utils.HostOs;
import com.igormaznitsa.mvnjlink.utils.ProxySettings;
import com.igormaznitsa.mvnjlink.utils.SystemUtils;
import java.io.File;
Expand Down Expand Up @@ -57,6 +59,22 @@ public abstract class AbstractJdkToolMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

/**
* Force host OS from predefined list to override auto-detection
*
* @since 1.2.1
*/
@Parameter(name = "forceHostOs")
private HostOs forceHostOs;

/**
* Way to replace default extensions for host os during mojo processing
*
* @since 1.2.1
*/
@Parameter(name = "forceOsExtensions")
private Map<HostOs, String> forceOsExtensions = new HashMap<>();

/**
* Disable loading and use only cached JDKs.
* Can be overridden by property 'mvn.jlink.use.only.cache'
Expand Down Expand Up @@ -157,6 +175,33 @@ public boolean isUseOnlyCache() {
Boolean.toString(this.useOnlyCache)));
}

/**
* Find host OS, force Host OS parameter aware.
*
* @return detected host OS
* @since 1.2.1
*/
@Nonnull
public HostOs findHostOs() {
if (this.forceHostOs == null) {
return HostOs.findHostOs();
} else {
return this.forceHostOs;
}
}

/**
* Get map of defined Host OS file extensions to replace default ones.
*
* @return map contains pairs of Host OS values and default file extensions
* @since 1.2.1
*/
@Nonnull
@MustNotContainNull
public Map<HostOs, String> getForceOsExtensions() {
return this.forceOsExtensions;
}

@Nonnull
public Map<String, String> getProviderConfig() {
return this.providerConfig;
Expand Down Expand Up @@ -245,6 +290,7 @@ public Path findJdkCacheFolder() throws IOException {
return result;
}

@SuppressWarnings("unchecked")
@Nonnull
protected Path getSourceJdkFolderFromProvider() throws MojoExecutionException, MojoFailureException {
try {
Expand All @@ -270,17 +316,21 @@ public String findJdkTool(@Nonnull final String toolName) {
if (toolchain == null) {
final String mavenJavaHome = System.getProperty("java.home");
log.debug("Maven java.home: " + mavenJavaHome);
final Path path = SystemUtils.findJdkExecutable(log, Paths.get(mavenJavaHome), SystemUtils.ensureOsExtension(toolName));
final Path path = SystemUtils.findJdkExecutable(log, Paths.get(mavenJavaHome), toolName,
this.findHostOs(), this.getForceOsExtensions());
toolPath = path == null ? null : path.toString();
} else {
log.debug("Detected toolchain: " + toolchain);
toolPath = SystemUtils.ensureOsExtension(toolchain.findTool(toolName));
toolPath = SystemUtils.addHostFileExtensionIfNeeded(toolchain.findTool(toolName),
this.findHostOs(), this.getForceOsExtensions());
}
} else {
final Path jdkHome = Paths.get(this.getToolJdk());
if (jdkHome.toFile().isDirectory()) {
log.debug("Tool base JDK home: " + jdkHome);
final Path foundPath = SystemUtils.findJdkExecutable(this.getLog(), jdkHome, toolName);
final Path foundPath =
SystemUtils.findJdkExecutable(this.getLog(), jdkHome, toolName, this.findHostOs(),
this.getForceOsExtensions());
toolPath = foundPath == null ? null : foundPath.toString();
} else {
log.error("Can't find directory: " + jdkHome);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.igormaznitsa.mvnjlink.mojos;

import static com.igormaznitsa.mvnjlink.utils.StringUtils.match;
import static java.nio.file.Files.walk;
import static java.util.stream.Collectors.toMap;


import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
Expand All @@ -31,7 +31,6 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.SelectorUtils;

/**
* Allows to download JDK from provider into cache and save path of cached JDK folder into project property.
Expand Down Expand Up @@ -73,7 +72,8 @@ public void onExecute() throws MojoExecutionException, MojoFailureException {
final Path jdkPath = this.getSourceJdkFolderFromProvider();
this.getLog().info("cached JDK path: " + jdkPath);
this.getProject().getProperties().setProperty(this.getJdkPathProperty(), jdkPath.toString());
this.getLog().info(String.format("Project property '%s' <= '%s'", this.getJdkPathProperty(), jdkPath.toString()));
this.getLog().info(String.format("Project property '%s' <= '%s'", this.getJdkPathProperty(),
jdkPath.toString()));

if (this.pathAsProperty != null && !this.pathAsProperty.isEmpty()) {
final Map<String, Path> found = findForPatterns(jdkPath, this.pathAsProperty);
Expand All @@ -83,17 +83,23 @@ public void onExecute() throws MojoExecutionException, MojoFailureException {
this.getLog().error("Can't find any file for pattern: " + f.getKey());
}
}
throw new MojoExecutionException("Can't find some files in JDK for path patterns, see log!");
throw new MojoExecutionException(
"Can't find some files in JDK for path patterns, see log!");
}
for (final Map.Entry<String, Path> f : found.entrySet()) {
this.getLog().info(String.format("Project property '%s' <= '%s' (pattern: %s)", f.getKey(), jdkPath.relativize(f.getValue()).toString(), this.pathAsProperty.get(f.getKey())));
this.getProject().getProperties().setProperty(f.getKey(), f.getValue().toAbsolutePath().toString());
this.getLog().info(String.format("Project property '%s' <= '%s' (pattern: %s)", f.getKey(),
jdkPath.relativize(f.getValue()).toString(), this.pathAsProperty.get(f.getKey())));
this.getProject().getProperties()
.setProperty(f.getKey(), f.getValue().toAbsolutePath().toString());
}
}
}

@Nonnull
private Map<String, Path> findForPatterns(@Nonnull final Path rootFolder, @Nonnull final Map<String, String> patterns) throws MojoExecutionException {
private Map<String, Path> findForPatterns(
@Nonnull final Path rootFolder,
@Nonnull final Map<String, String> patterns
) throws MojoExecutionException {
final Map<String, Path> result = new HashMap<>();
final Map<String, String> normalized = patterns.entrySet().stream()
.collect(toMap(Map.Entry::getKey,
Expand All @@ -105,7 +111,7 @@ private Map<String, Path> findForPatterns(@Nonnull final Path rootFolder, @Nonnu
final String propertyName = e.getKey();
final String pattern = e.getValue();
final Path absolutePath = rootFolder.resolve(x).toAbsolutePath();
if (SelectorUtils.match(pattern, absolutePath.toString())) {
if (match(pattern.toCharArray(), absolutePath.toString().toCharArray(), true)) {
result.put(propertyName, x);
break;
}
Expand Down
Loading

0 comments on commit 3a0b6eb

Please sign in to comment.