You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2018/09/11 05:42:43 UTC

[maven-jlink-plugin] 01/01: [MJLINK-26] - Can not create an image from a single module project without a dependency

This is an automated email from the ASF dual-hosted git repository.

khmarbaise pushed a commit to branch MJLINK-26
in repository https://gitbox.apache.org/repos/asf/maven-jlink-plugin.git

commit 26809188e4c2a65c6ca2aa6a022c481889930e07
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Tue Sep 11 07:42:25 2018 +0200

    [MJLINK-26] - Can not create an image from a single module project without a dependency
---
 src/it/projects/MJLINK-26/invoker.properties       |  18 ++++
 src/it/projects/MJLINK-26/pom.xml                  |  56 +++++++++++
 .../src/main/java/com/corporate/project/Main.java  |  35 +++++++
 .../MJLINK-26/src/main/java/module-info.java       |  24 +++++
 src/it/projects/MJLINK-26/verify.groovy            |  50 ++++++++++
 .../org/apache/maven/plugins/jlink/JLinkMojo.java  | 106 ++++++++++++---------
 6 files changed, 246 insertions(+), 43 deletions(-)

diff --git a/src/it/projects/MJLINK-26/invoker.properties b/src/it/projects/MJLINK-26/invoker.properties
new file mode 100644
index 0000000..8948d9b
--- /dev/null
+++ b/src/it/projects/MJLINK-26/invoker.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+invoker.java.version = 1.9+
+invoker.goals = clean package
diff --git a/src/it/projects/MJLINK-26/pom.xml b/src/it/projects/MJLINK-26/pom.xml
new file mode 100644
index 0000000..c8bf47f
--- /dev/null
+++ b/src/it/projects/MJLINK-26/pom.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements. See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership. The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License. You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied. See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.plugins</groupId>
+  <artifactId>maven-jlink-plugin-mjlink-26</artifactId>
+  <version>96.0</version>
+  <packaging>jlink</packaging>
+  <name>Maven</name>
+  <url>https://maven.apache.org</url>
+  <description>Test JLink create a image from a module without any dependency.</description>
+  <properties>
+    <maven.compiler.source>1.9</maven.compiler.source>
+    <maven.compiler.target>1.9</maven.compiler.target>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.8.0</version>
+        <configuration>
+          <source>1.9</source>
+          <target>1.9</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jlink-plugin</artifactId>
+        <version>@project.version@</version>
+        <extensions>true</extensions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/projects/MJLINK-26/src/main/java/com/corporate/project/Main.java b/src/it/projects/MJLINK-26/src/main/java/com/corporate/project/Main.java
new file mode 100644
index 0000000..1b4ca36
--- /dev/null
+++ b/src/it/projects/MJLINK-26/src/main/java/com/corporate/project/Main.java
@@ -0,0 +1,35 @@
+package com.corporate.project;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class Main
+{
+    public static void main( String[] args )
+    {
+        System.out.println( "Main one from Java 9+" );
+        if ( args.length > 0 )
+        {
+            for ( String arg : args )
+            {
+                System.out.println( " Arg: '" + arg + "'" );
+            }
+        }
+    }
+}
diff --git a/src/it/projects/MJLINK-26/src/main/java/module-info.java b/src/it/projects/MJLINK-26/src/main/java/module-info.java
new file mode 100644
index 0000000..d35441d
--- /dev/null
+++ b/src/it/projects/MJLINK-26/src/main/java/module-info.java
@@ -0,0 +1,24 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+module com.corporate.project {
+  requires java.base;
+  exports com.corporate.project;
+}
diff --git a/src/it/projects/MJLINK-26/verify.groovy b/src/it/projects/MJLINK-26/verify.groovy
new file mode 100644
index 0000000..f3b862f
--- /dev/null
+++ b/src/it/projects/MJLINK-26/verify.groovy
@@ -0,0 +1,50 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.*;
+import java.util.*;
+import java.util.jar.*;
+import org.codehaus.plexus.util.*;
+
+boolean result = true;
+
+try
+{
+    File target = new File( basedir, "target" );
+    if ( !target.exists() || !target.isDirectory() )
+    {
+        System.err.println( "target file is missing or not a directory." );
+        return false;
+    }
+    File artifact = new File( target, "maven-jlink-plugin-mjlink-26-96.0.zip" );
+    if ( !artifact.exists() || artifact.isDirectory() )
+    {
+        System.err.println( "maven-jlink-plugin-mjlink-26-96.0.zip file is missing or is a directory." );
+        return false;
+    }
+
+}
+catch( Throwable e )
+{
+    e.printStackTrace();
+    result = false;
+}
+
+return result;
diff --git a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
index 79691a5..a92a687 100644
--- a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -158,6 +159,9 @@ public class JLinkMojo
     private File outputDirectoryImage;
 
     @Parameter( defaultValue = "${project.build.directory}", required = true, readonly = true )
+    private File buildDirectory;
+
+    @Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
     private File outputDirectory;
 
     /**
@@ -169,8 +173,8 @@ public class JLinkMojo
     private String endian;
 
     /**
-     * Include additional paths on the <code>--module-path</code> option.
-     * Project dedependencies and JDK modules are automatically added.
+     * Include additional paths on the <code>--module-path</code> option. Project dedependencies and JDK modules are
+     * automatically added.
      */
     @Parameter
     private List<String> modulePaths;
@@ -237,8 +241,8 @@ public class JLinkMojo
     private ZipArchiver zipArchiver;
 
     /**
-     * Name of the generated ZIP file in the <code>target</code> directory. 
-     * This will not change the name of the installed/deployed file.
+     * Name of the generated ZIP file in the <code>target</code> directory. This will not change the name of the
+     * installed/deployed file.
      */
     @Parameter( defaultValue = "${project.build.finalName}", readonly = true )
     private String finalName;
@@ -265,26 +269,18 @@ public class JLinkMojo
 
         ifOutputDirectoryExistsDelteIt();
 
-        Collection<String> modulesToAdd;
-        if ( addModules == null )
-        {
-            modulesToAdd = new ArrayList<>();
-        }
-        else
+        Collection<String> modulesToAdd = new ArrayList<>();
+        if ( addModules != null )
         {
-            modulesToAdd = new ArrayList<>( addModules );
+            modulesToAdd.addAll( addModules );
         }
-        
-        Collection<String> pathsOfModules;
-        if ( modulePaths == null )
-        {
-            pathsOfModules = new ArrayList<>();
-        }
-        else
+
+        Collection<String> pathsOfModules = new ArrayList<>();
+        if ( modulePaths != null )
         {
-            pathsOfModules = new ArrayList<>( modulePaths );
+            pathsOfModules.addAll( modulePaths );
         }
-        
+
         for ( Entry<String, File> item : getModulePathElements().entrySet() )
         {
             getLog().info( " -> module: " + item.getKey() + " ( " + item.getValue().getPath() + " )" );
@@ -293,6 +289,7 @@ public class JLinkMojo
             modulesToAdd.add( item.getKey() );
             pathsOfModules.add( item.getValue().getPath() );
         }
+
         // The jmods directory of the JDK
         pathsOfModules.add( jmodsFolder.getAbsolutePath() );
 
@@ -309,7 +306,7 @@ public class JLinkMojo
 
         executeCommand( cmd, outputDirectoryImage );
 
-        File createZipArchiveFromImage = createZipArchiveFromImage( outputDirectory, outputDirectoryImage );
+        File createZipArchiveFromImage = createZipArchiveFromImage( buildDirectory, outputDirectoryImage );
 
         if ( projectHasAlreadySetAnArtifact() )
         {
@@ -326,24 +323,26 @@ public class JLinkMojo
 
         for ( Artifact a : project.getArtifacts() )
         {
+            getLog().debug( "Artifact: " + a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion() );
             list.add( a.getFile() );
         }
         return list;
     }
 
-    private Map<String, File> getModulePathElements() throws MojoFailureException
+    private Map<String, File> getModulePathElements()
+        throws MojoFailureException
     {
         // For now only allow named modules. Once we can create a graph with ASM we can specify exactly the modules
         // and we can detect if auto modules are used. In that case, MavenProject.setFile() should not be used, so
         // you cannot depend on this project and so it won't be distributed.
 
         Map<String, File> modulepathElements = new HashMap<>();
-        
+
         try
         {
             Collection<File> dependencyArtifacts = getCompileClasspathElements( getProject() );
 
-            ResolvePathsRequest<File> request = ResolvePathsRequest.withFiles( dependencyArtifacts );
+            ResolvePathsRequest<File> request = ResolvePathsRequest.ofFiles( dependencyArtifacts );
 
             Toolchain toolchain = getToolchain();
             if ( toolchain != null && toolchain instanceof DefaultJavaToolChain )
@@ -355,25 +354,49 @@ public class JLinkMojo
 
             for ( Map.Entry<File, JavaModuleDescriptor> entry : resolvePathsResult.getPathElements().entrySet() )
             {
-                if ( entry.getValue() != null )
-                {
-                    // Don't warn for automatic modules, let the jlink tool do that
-                    modulepathElements.put( entry.getValue().name(), entry.getKey() );
-                }
-                else
+                if ( entry.getValue() == null )
                 {
                     String message = "The given dependency " + entry.getKey()
                         + " does not have a module-info.java file. So it can't be linked.";
                     getLog().error( message );
                     throw new MojoFailureException( message );
                 }
+
+                // Don't warn for automatic modules, let the jlink tool do that
+                getLog().debug( " module: " + entry.getValue().name() + " automatic: "
+                    + entry.getValue().isAutomatic() );
+                modulepathElements.put( entry.getValue().name(), entry.getKey() );
             }
-        }
+
+            // This part is for the module in target/classes ? (Hacky..)
+            // FIXME: Is there a better way to identify that code exists?
+            if ( outputDirectory.exists() )
+            {
+                List<File> singletonList = Collections.singletonList( outputDirectory );
+
+                ResolvePathsRequest<File> singleModuls = ResolvePathsRequest.ofFiles( singletonList );
+
+                ResolvePathsResult<File> resolvePaths = locationManager.resolvePaths( singleModuls );
+                for ( Entry<File, JavaModuleDescriptor> entry : resolvePaths.getPathElements().entrySet() )
+                {
+                    if ( entry.getValue() == null )
+                    {
+                        String message = "The given project " + entry.getKey()
+                            + " does not contain a module-info.java file. So it can't be linked.";
+                        getLog().error( message );
+                        throw new MojoFailureException( message );
+                    }
+                    modulepathElements.put( entry.getValue().name(), entry.getKey() );
+                }
+            }
+
+        }   
         catch ( IOException e )
         {
-            getLog().warn( e.getMessage() );
+            getLog().error( e.getMessage() );
+            throw new MojoFailureException( e.getMessage() );
         }
-        
+
         return modulepathElements;
     }
 
@@ -436,8 +459,7 @@ public class JLinkMojo
     {
         if ( compress != null && ( compress < 0 || compress > 2 ) )
         {
-            String message =
-                "The given compress parameters " + compress + " is not in the valid value range from 0..2";
+            String message = "The given compress parameters " + compress + " is not in the valid value range from 0..2";
             getLog().error( message );
             throw new MojoFailureException( message );
         }
@@ -523,14 +545,12 @@ public class JLinkMojo
         }
         if ( pathsOfModules != null )
         {
-            //@formatter:off
+            // @formatter:off
             argsFile.println( "--module-path" );
-            argsFile
-              .append( '"' )
-              .append( getPlatformDependSeparateList( pathsOfModules )
-                         .replace( "\\", "\\\\" ) 
-                     ).println( '"' );
-            //@formatter:off
+            argsFile.append( '"' )
+                .append( getPlatformDependSeparateList( pathsOfModules )
+                         .replace( "\\", "\\\\" ) ).println( '"' );
+            // @formatter:off
         }
 
         if ( noHeaderFiles )
@@ -573,7 +593,7 @@ public class JLinkMojo
             argsFile.append( '"' ).append( sb.toString().replace( "\\", "\\\\" ) ).println( '"' );
         }
 
-        if ( outputDirectory != null )
+        if ( buildDirectory != null )
         {
             argsFile.println( "--output" );
             argsFile.println( outputDirectoryImage );