Skip to content

Commit

Permalink
Merge pull request #265 from orphan-oss/replace-deprecated
Browse files Browse the repository at this point in the history
Replaces deprecated Maven APIs with Eclipse Aether APIs
  • Loading branch information
lukaszlenart authored Apr 19, 2023
2 parents 66147fe + 9ddf13a commit a9c2856
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>2.3.4-SNAPSHOT</version>
<version>2.4.0-SNAPSHOT</version>

<name>Maven Launch4j Plugin</name>
<description>This plugin creates Windows executables from Java jar files using the Launch4j utility.</description>
Expand Down Expand Up @@ -114,11 +114,6 @@
<version>3.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.13.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package com.akathist.maven.plugins.launch4j;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.artifact.Artifact;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
77 changes: 47 additions & 30 deletions src/main/java/com/akathist/maven/plugins/launch4j/Launch4jMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import net.sf.launch4j.config.Config;
import net.sf.launch4j.config.ConfigPersister;
import net.sf.launch4j.config.ConfigPersisterException;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -35,10 +34,16 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.impl.ArtifactResolver;
import org.eclipse.aether.repository.LocalArtifactRequest;
import org.eclipse.aether.repository.LocalArtifactResult;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -54,6 +59,7 @@
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

/**
* Wraps a jar in a Windows executable.
Expand All @@ -76,35 +82,32 @@ public class Launch4jMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", required = true, readonly = true)
private MavenSession session;

@Parameter(defaultValue = "${project.remoteProjectRepositories}", required = true, readonly = true)
private List<RemoteRepository> repositories;

/**
* The dependencies required by the project.
*/
@Parameter(defaultValue = "${project.artifacts}", required = true, readonly = true)
private Set<Artifact> dependencies;
private Set<org.apache.maven.artifact.Artifact> dependencies;

/**
* The user's current project.
*/
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;

/**
* The user's plugins (including, I hope, this one).
*/
@Parameter(defaultValue = "${project.build.plugins}", required = true, readonly = true)
private List<Artifact> plugins;

/**
* Used to look up Artifacts in the remote repository.
*/
@Component(role = RepositorySystem.class)
private RepositorySystem factory;
private RepositorySystem repositorySystem;

/**
* The user's local repository.
*/
@Parameter(defaultValue = "${localRepository}", required = true, readonly = true)
private ArtifactRepository localRepository;
@Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true)
private RepositorySystemSession repositorySystemSession;

/**
* The artifact resolver used to grab the binary bits that launch4j needs.
Expand All @@ -117,7 +120,7 @@ public class Launch4jMojo extends AbstractMojo {
* Used to get the Launch4j artifact version.
*/
@Parameter(defaultValue = "${plugin.artifacts}")
private List<Artifact> pluginArtifacts;
private List<org.apache.maven.artifact.Artifact> oldPluginArtifacts;

/**
* The base of the current project.
Expand Down Expand Up @@ -442,7 +445,10 @@ private void doExecute() throws MojoExecutionException {
c.setVariables(vars);

if (classPath != null) {
c.setClassPath(classPath.toL4j(dependencies));
Set<Artifact> newArtifacts = dependencies.stream().map(old ->
new DefaultArtifact(old.getGroupId(), old.getArtifactId(), old.getClassifier(), null, old.getVersion())
).collect(Collectors.toSet());
c.setClassPath(classPath.toL4j(newArtifacts));
}
if (jre != null) {
jre.deprecationWarning(getLog());
Expand Down Expand Up @@ -512,8 +518,11 @@ private void doExecute() throws MojoExecutionException {
private File setupBuildEnvironment() throws MojoExecutionException {
createParentFolder();
Artifact binaryBits = chooseBinaryBits();
retrieveBinaryBits(binaryBits);
return unpackWorkDir(binaryBits);
if (retrieveBinaryBits(binaryBits)) {
return unpackWorkDir(binaryBits);
} else {
throw new MojoExecutionException("Artifact: " + binaryBits + " is not available!");
}
}

private void createParentFolder() {
Expand All @@ -536,7 +545,8 @@ private void createParentFolder() {
* Writes a marker file to prevent unzipping more than once.
*/
private File unpackWorkDir(Artifact artifact) throws MojoExecutionException {
Artifact localArtifact = localRepository.find(artifact);
LocalArtifactRequest request = new LocalArtifactRequest(artifact, null, null);
LocalArtifactResult localArtifact = repositorySystemSession.getLocalRepositoryManager().find(repositorySystemSession, request);
if (localArtifact == null || localArtifact.getFile() == null) {
throw new MojoExecutionException("Cannot obtain file path to " + artifact);
}
Expand Down Expand Up @@ -647,19 +657,16 @@ private List<String> relativizeAndCopy(File workdir, List<String> paths) throws
/**
* Downloads the platform-specific parts, if necessary.
*/
private void retrieveBinaryBits(Artifact a) throws MojoExecutionException {

ProjectBuildingRequest configuration = session.getProjectBuildingRequest();
configuration.setRemoteRepositories(project.getRemoteArtifactRepositories());
configuration.setLocalRepository(localRepository);
private boolean retrieveBinaryBits(Artifact a) throws MojoExecutionException {

getLog().debug("Retrieving artifact: " + a + " stored in " + a.getFile());

try {
resolver.resolveArtifact(configuration, a).getArtifact();
ArtifactRequest request = new ArtifactRequest(a, repositories, null);
return repositorySystem.resolveArtifact(repositorySystemSession, request).isResolved();
} catch (IllegalArgumentException e) {
throw new MojoExecutionException("Illegal Argument Exception", e);
} catch (ArtifactResolverException e) {
} catch (ArtifactResolutionException e) {
throw new MojoExecutionException("Can't retrieve platform-specific components", e);
}
}
Expand Down Expand Up @@ -692,8 +699,14 @@ private Artifact chooseBinaryBits() throws MojoExecutionException {
throw new MojoExecutionException("Sorry, Launch4j doesn't support the '" + os + "' OS.");
}

return factory.createArtifactWithClassifier(LAUNCH4J_GROUP_ID, LAUNCH4J_ARTIFACT_ID,
getLaunch4jVersion(), "jar", "workdir-" + plat);
Artifact artifact = new DefaultArtifact(LAUNCH4J_GROUP_ID, LAUNCH4J_ARTIFACT_ID, "workdir-" + plat, "jar", getLaunch4jVersion());
try {
ArtifactRequest request = new ArtifactRequest(artifact, repositories, null);

return repositorySystem.resolveArtifact(repositorySystemSession, request).getArtifact();
} catch (ArtifactResolutionException e) {
throw new MojoExecutionException(e);
}
}

private File getBaseDir() {
Expand Down Expand Up @@ -801,6 +814,10 @@ private void printState() {
private String getLaunch4jVersion() throws MojoExecutionException {
String version = null;

Set<Artifact> pluginArtifacts = oldPluginArtifacts.stream().map(old ->
new DefaultArtifact(old.getGroupId(), old.getArtifactId(), old.getClassifier(), null, old.getVersion())
).collect(Collectors.toSet());

for (Artifact artifact : pluginArtifacts) {
if (LAUNCH4J_GROUP_ID.equals(artifact.getGroupId()) &&
LAUNCH4J_ARTIFACT_ID.equals(artifact.getArtifactId())
Expand Down

0 comments on commit a9c2856

Please sign in to comment.