0

I am introducing java 9 modules to a big project and I am facing an issue when trying to build an executable JAR (on one of the sub module) using the maven-jar-plugin. Here is a small view of my project:

├───my-sub-module
│   ├───pom.xml
│   └───src
│       ├───main
│       │   └───java
│       │       ├───com
│       │       │   └───packages
│       │       │       └───...
│       │       │           
│       │       └───module-info.java (let's say the module name is com.foo.bar)
│       └───test
│           └───java
│               └───com
│                   └───packages
│                       └───benchmark
│                           └───BenchmarkTests.java
└───pom.xml

The plugin configuration in my pom is :

<build>
        <plugins>
            <!-- Build an executable test JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.packages.benchmark.BenchmarkTests</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

EDIT When building using mvn clean install I got the following stack-trace:

Caused by: org.codehaus.plexus.archiver.ArchiverException: Could not create modular JAR file. The JDK jar tool exited with 1
    at org.codehaus.plexus.archiver.jar.JarToolModularJarArchiver.postCreateArchive (JarToolModularJarArchiver.java:123)
    at org.codehaus.plexus.archiver.AbstractArchiver.createArchive (AbstractArchiver.java:1066)
    at org.apache.maven.archiver.MavenArchiver.createArchive (MavenArchiver.java:676)
    at org.apache.maven.plugins.jar.AbstractJarMojo.createArchive (AbstractJarMojo.java:276)
    at org.apache.maven.plugins.jar.AbstractJarMojo.execute (AbstractJarMojo.java:307)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

I also noticed the following message in maven logs:

jar: Package com.package.benchmark missing from ModulePackages class file attribute

Does it mean that my test package should have the same name as in the module packages ?

Mr. D
  • 657
  • 1
  • 8
  • 21
  • anything more than that with `-X` flag? – Naman Aug 02 '21 at 17:28
  • Why do you want to build an executables jar from test code? Second if you like to build with modules is the question why an executable which combines several module which in consequence will kill any module definition because the jar can only have a single module-info.java... – khmarbaise Aug 02 '21 at 17:46
  • @khmarbaise executable jars are for thee purpose of benchmarking and collecting results when running on non physically available machines. For your second question, I am not getting your point. I don't see any reason why I should define a module-package.info for the test if that's your question. – Mr. D Aug 03 '21 at 07:53
  • Benchmarks have different dependencies like (jmh...) which usual unit tests don't have which means best to have a separate module. Creating an executable means having a single jar which in result means only a single module-info can be put into the jar... Furthermore creating a jar which is a module means that all other dependencies have to be a module as well... Also via jmh it's usually best to define the main class to be executed via command line to have several benchmarks within a single jar... – khmarbaise Aug 03 '21 at 16:24

0 Answers0