迁移到java 9模块时无法使用maven-jar-plugin构建可执行jar

m3eecexj  于 12个月前  发布在  Maven
关注(0)|答案(1)|浏览(150)

我正在将java 9模块引入一个大项目,当我试图使用maven-jar-plugin构建一个可执行脚本(在其中一个子模块上)时,我遇到了一个问题。下面是我的项目的一个小视图:

├───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

字符串
我的pom中的插件配置是:

<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当使用mvn clean install构建时,我得到了以下堆栈跟踪:

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)


我还注意到maven日志中的以下消息:

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


这是否意味着我的测试包应该与模块包中的名称相同?

rkkpypqq

rkkpypqq1#

撇开@khmarbaise的许多优秀观点不谈:
错误:

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

字符串
.来自Plexus Archiver执行的jar --update操作,该操作将使项目的.jar文件成为模块化.jar文件。
Plexus Archiver传递给jar的标志之一是--main-class。由于Java虚拟机规范中的一行,作为此参数的值提供的类名必须是来自当前模块的类,即由所讨论的.jar文件根处的module-info.class文件描述的模块。可能出于多种原因,jar决定com.packages.benchmark.BenchmarkTests不属于当前模块。
错误消息的“ModulePackages class file attribute“部分显示是由于jar试图查看当前module-info.class类文件中有哪些已知软件包。该module-info.class文件的ModulePackages属性不包含com.package.benchmark
(The Plexus Archiver可能会在这里输出一个更好的错误消息,如果它检测到一个“坏”的META-INF/MANIFEST.MFMain-Class属性,甚至可以完全避免--update命令,因为它具有与jar工具相同的信息。参见https://github.com/codehaus-plexus/plexus-archiver/issues/310.

相关问题