From 0b28bd67adae89a1c6ad1d986e3be7156a99b848 Mon Sep 17 00:00:00 2001 From: anpetrel Date: Thu, 12 Oct 2017 15:24:45 +0200 Subject: [PATCH 1/2] MJLINK-4 NPE on execution Using Aether to resolve artifacts. Upgraded to Maven 3.1.1. --- maven-jlink-plugin/pom.xml | 2 +- .../apache/maven/plugins/jlink/JLinkMojo.java | 93 +++++++++++++++---- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/maven-jlink-plugin/pom.xml b/maven-jlink-plugin/pom.xml index 454f3e39e2..c9247c2d4c 100644 --- a/maven-jlink-plugin/pom.xml +++ b/maven-jlink-plugin/pom.xml @@ -60,7 +60,7 @@ - 3.0 + 3.1.1 1.7 1.7 diff --git a/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java b/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java index b1cd330bb8..4b7e9104b2 100644 --- a/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java +++ b/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java @@ -52,12 +52,19 @@ import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.cli.Commandline; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; /** * The JLink goal is intended to create a Java Run Time Image file based on * http://openjdk.java.net/jeps/282, * http://openjdk.java.net/jeps/220. - * + * * @author Karl Heinz Marbaise khmarbaise@apache.org */ // CHECKSTYLE_OFF: LineLength @@ -104,7 +111,7 @@ public class JLinkMojo /** * Limit the universe of observable modules. The following gives an example of the configuration which can be used * in the pom.xml file. - * + * *
      *   <limitModules>
      *     <limitModule>mod1</limitModule>
@@ -113,7 +120,7 @@ public class JLinkMojo
      *     .
      *   </limitModules>
      * 
- * + * * This configuration is the equivalent of the command line option: * --limit-modules <mod>[,<mod>...] */ @@ -128,7 +135,7 @@ public class JLinkMojo * By using the --add-modules you can define the root modules to be resolved. The configuration in * pom.xml file can look like this: *

- * + * *
      * <addModules>
      *   <addModule>mod1</addModule>
@@ -137,7 +144,7 @@ public class JLinkMojo
      *   .
      * </addModules>
      * 
- * + * * The command line equivalent for jlink is: --add-modules <mod>[,<mod>...]. */ @Parameter @@ -205,7 +212,7 @@ public class JLinkMojo /** * Suggest providers that implement the given service types from the module path. - * + * *
      * <suggestProviders>
      *   <suggestProvider>name-a</suggestProvider>
@@ -214,7 +221,7 @@ public class JLinkMojo
      *   .
      * </suggestProviders>
      * 
- * + * * The jlink command linke equivalent: --suggest-providers [<name>,...] */ @Parameter @@ -233,7 +240,24 @@ public class JLinkMojo private ZipArchiver zipArchiver; /** - * Name of the generated ZIP file in the target directory. + * The repository system. + */ + @Component + private RepositorySystem repoSystem; + + /** + * The repository system session. + */ + @Parameter( defaultValue = "${repositorySystemSession}", readonly = true, required = true ) + private RepositorySystemSession repoSession; + /** + * The available repositories. + */ + @Parameter( defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true ) + private List repositories; + + /** + * Name of the generated ZIP file in the target directory. * This will not change the name of the installed/deployed file. */ @Parameter( defaultValue = "${project.build.finalName}", readonly = true ) @@ -310,13 +334,50 @@ public void execute() getProject().getArtifact().setFile( createZipArchiveFromImage ); } - private List getCompileClasspathElements( MavenProject project ) + private List getCompileClasspathElements( MavenProject project ) throws MojoExecutionException { List list = new ArrayList( project.getArtifacts().size() + 1 ); - - for ( Artifact a : project.getArtifacts() ) + for ( Artifact unresolvedArtifact : project.getArtifacts() ) { - list.add( a.getFile() ); + + // Here, it becomes messy. We ask Maven to resolve the artifact's location. + // It may imply downloading it from a remote repository, + // searching the local repository or looking into the reactor's cache. + + // To achieve this, we must use Aether + // (the dependency mechanism behind Maven). + String artifactId = unresolvedArtifact.getArtifactId(); + org.eclipse.aether.artifact.Artifact aetherArtifact = + new DefaultArtifact( unresolvedArtifact.getGroupId(), unresolvedArtifact.getArtifactId(), + unresolvedArtifact.getClassifier(), unresolvedArtifact.getType(), + unresolvedArtifact.getVersion() ); + + ArtifactRequest req = + new ArtifactRequest().setRepositories( this.repositories ).setArtifact( aetherArtifact ); + ArtifactResult resolutionResult; + try + { + resolutionResult = this.repoSystem.resolveArtifact( this.repoSession, req ); + + } + catch ( ArtifactResolutionException e ) + { + throw new MojoExecutionException( "Artifact " + artifactId + " could not be resolved.", e ); + } + + // The file should exists, but we never know. + File file = resolutionResult.getArtifact().getFile(); + if ( file == null || !file.exists() ) + { + getLog().warn( "Artifact " + artifactId + + " has no attached file. Its content will not be copied in the target model directory." ); + } + else + { + list.add( file ); + } + + // Do whatever you want with the file... } return list; } @@ -384,7 +445,7 @@ private void preparePaths() modulepathElements.add( file.getPath() ); } } - catch ( IOException e ) + catch ( IOException | MojoExecutionException e ) { getLog().warn( e.getMessage() ); } @@ -536,7 +597,7 @@ private Commandline createJLinkCommandLine() argsFile .append( '"' ) .append( getPlatformDependSeparateList( modulePaths ) - .replace( "\\", "\\\\" ) + .replace( "\\", "\\\\" ) ).println( '"' ); //@formatter:off } @@ -613,7 +674,7 @@ private boolean hasModules() { return addModules != null && !addModules.isEmpty(); } - + private void writeBoxedWarning( String message ) { String line = StringUtils.repeat( "*", message.length() + 4 ); @@ -621,5 +682,5 @@ private void writeBoxedWarning( String message ) getLog().warn( "* " + MessageUtils.buffer().strong( message ) + " *" ); getLog().warn( line ); } - + } From d921d7038390f11fbf401fe7474cc6aed8687e08 Mon Sep 17 00:00:00 2001 From: anpetrel Date: Mon, 16 Oct 2017 09:37:42 +0200 Subject: [PATCH 2/2] Revert "MJLINK-4 NPE on execution" This reverts commit 0b28bd67adae89a1c6ad1d986e3be7156a99b848. --- maven-jlink-plugin/pom.xml | 2 +- .../apache/maven/plugins/jlink/JLinkMojo.java | 93 ++++--------------- 2 files changed, 17 insertions(+), 78 deletions(-) diff --git a/maven-jlink-plugin/pom.xml b/maven-jlink-plugin/pom.xml index 8696226014..1cc90ba10e 100644 --- a/maven-jlink-plugin/pom.xml +++ b/maven-jlink-plugin/pom.xml @@ -60,7 +60,7 @@ - 3.1.1 + 3.0 1.7 1.7 diff --git a/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java b/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java index 8e43972c29..be29b0235f 100644 --- a/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java +++ b/maven-jlink-plugin/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java @@ -52,19 +52,12 @@ import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.cli.Commandline; -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.repository.RemoteRepository; -import org.eclipse.aether.resolution.ArtifactRequest; -import org.eclipse.aether.resolution.ArtifactResolutionException; -import org.eclipse.aether.resolution.ArtifactResult; /** * The JLink goal is intended to create a Java Run Time Image file based on * http://openjdk.java.net/jeps/282, * http://openjdk.java.net/jeps/220. - * + * * @author Karl Heinz Marbaise khmarbaise@apache.org */ // CHECKSTYLE_OFF: LineLength @@ -111,7 +104,7 @@ public class JLinkMojo /** * Limit the universe of observable modules. The following gives an example of the configuration which can be used * in the pom.xml file. - * + * *
      *   <limitModules>
      *     <limitModule>mod1</limitModule>
@@ -120,7 +113,7 @@ public class JLinkMojo
      *     .
      *   </limitModules>
      * 
- * + * * This configuration is the equivalent of the command line option: * --limit-modules <mod>[,<mod>...] */ @@ -135,7 +128,7 @@ public class JLinkMojo * By using the --add-modules you can define the root modules to be resolved. The configuration in * pom.xml file can look like this: *

- * + * *
      * <addModules>
      *   <addModule>mod1</addModule>
@@ -144,7 +137,7 @@ public class JLinkMojo
      *   .
      * </addModules>
      * 
- * + * * The command line equivalent for jlink is: --add-modules <mod>[,<mod>...]. */ @Parameter @@ -212,7 +205,7 @@ public class JLinkMojo /** * Suggest providers that implement the given service types from the module path. - * + * *
      * <suggestProviders>
      *   <suggestProvider>name-a</suggestProvider>
@@ -221,7 +214,7 @@ public class JLinkMojo
      *   .
      * </suggestProviders>
      * 
- * + * * The jlink command linke equivalent: --suggest-providers [<name>,...] */ @Parameter @@ -240,24 +233,7 @@ public class JLinkMojo private ZipArchiver zipArchiver; /** - * The repository system. - */ - @Component - private RepositorySystem repoSystem; - - /** - * The repository system session. - */ - @Parameter( defaultValue = "${repositorySystemSession}", readonly = true, required = true ) - private RepositorySystemSession repoSession; - /** - * The available repositories. - */ - @Parameter( defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true ) - private List repositories; - - /** - * Name of the generated ZIP file in the target directory. + * Name of the generated ZIP file in the target directory. * This will not change the name of the installed/deployed file. */ @Parameter( defaultValue = "${project.build.finalName}", readonly = true ) @@ -334,50 +310,13 @@ public void execute() getProject().getArtifact().setFile( createZipArchiveFromImage ); } - private List getCompileClasspathElements( MavenProject project ) throws MojoExecutionException + private List getCompileClasspathElements( MavenProject project ) { List list = new ArrayList( project.getArtifacts().size() + 1 ); - for ( Artifact unresolvedArtifact : project.getArtifacts() ) - { - - // Here, it becomes messy. We ask Maven to resolve the artifact's location. - // It may imply downloading it from a remote repository, - // searching the local repository or looking into the reactor's cache. - // To achieve this, we must use Aether - // (the dependency mechanism behind Maven). - String artifactId = unresolvedArtifact.getArtifactId(); - org.eclipse.aether.artifact.Artifact aetherArtifact = - new DefaultArtifact( unresolvedArtifact.getGroupId(), unresolvedArtifact.getArtifactId(), - unresolvedArtifact.getClassifier(), unresolvedArtifact.getType(), - unresolvedArtifact.getVersion() ); - - ArtifactRequest req = - new ArtifactRequest().setRepositories( this.repositories ).setArtifact( aetherArtifact ); - ArtifactResult resolutionResult; - try - { - resolutionResult = this.repoSystem.resolveArtifact( this.repoSession, req ); - - } - catch ( ArtifactResolutionException e ) - { - throw new MojoExecutionException( "Artifact " + artifactId + " could not be resolved.", e ); - } - - // The file should exists, but we never know. - File file = resolutionResult.getArtifact().getFile(); - if ( file == null || !file.exists() ) - { - getLog().warn( "Artifact " + artifactId - + " has no attached file. Its content will not be copied in the target model directory." ); - } - else - { - list.add( file ); - } - - // Do whatever you want with the file... + for ( Artifact a : project.getArtifacts() ) + { + list.add( a.getFile() ); } return list; } @@ -445,7 +384,7 @@ private void preparePaths() modulepathElements.add( file.getPath() ); } } - catch ( IOException | MojoExecutionException e ) + catch ( IOException e ) { getLog().warn( e.getMessage() ); } @@ -597,7 +536,7 @@ private Commandline createJLinkCommandLine() argsFile .append( '"' ) .append( getPlatformDependSeparateList( modulePaths ) - .replace( "\\", "\\\\" ) + .replace( "\\", "\\\\" ) ).println( '"' ); //@formatter:off } @@ -674,7 +613,7 @@ private boolean hasModules() { return addModules != null && !addModules.isEmpty(); } - + private void writeBoxedWarning( String message ) { String line = StringUtils.repeat( "*", message.length() + 4 ); @@ -682,5 +621,5 @@ private void writeBoxedWarning( String message ) getLog().warn( "* " + MessageUtils.buffer().strong( message ) + " *" ); getLog().warn( line ); } - + }