首页 文章

使用maven-compiler和jdk9-ea 148编译测试

提问于
浏览
4

我在使用maven-compile-plugin构建应用程序时遇到问题:3.6.0 at jdk9-ea 148 .

我正在尝试制作jdk9模块 . 模块信息和其他类正在编译而没有问题,但是当涉及到default-testCompile时,它就会崩溃 . 当我的module-info.java被删除时,它编译没有问题 .

基本上这是我得到的例外:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:testCompile (default-testCompile) on project bar: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:testCompile failed. NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:testCompile (default-testCompile) on project bar: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:testCompile failed.
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:538)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:testCompile failed.
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
        ... 20 more
Caused by: java.lang.NullPointerException
        at org.apache.maven.plugin.compiler.AsmModuleInfoParser.getModuleName(AsmModuleInfoParser.java:48)
        at org.apache.maven.plugin.compiler.TestCompilerMojo.preparePaths(TestCompilerMojo.java:259)
        at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:742)
        at org.apache.maven.plugin.compiler.TestCompilerMojo.execute(TestCompilerMojo.java:164)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        ... 21 more

这是我在那里的maven-compiler-plugin的设置

<plugins>
 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.6.0</version>
  <configuration>
   <source>1.9</source>
   <target>1.9</target>
   <compilerArgument>-Xlint:all</compilerArgument>
  </configuration>
 </plugin>
</plugins>

有谁知道如何处理这个问题才能使它工作?

3 回答

  • 0

    当Maven尝试读取 module-info.class 的类名时会发生异常,因为a recent change被指定为null . 看起来Maven使用ASM并且根据Remi Forax ASM6 should already handle that case .

    我建议在Maven问题跟踪器中查找问题,如果找不到,则打开一个问题 .

  • 2

    使用maven-compiler-plugin:3.7.0的更新版本将帮助您解决此问题: -

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
                <compilerArgument>-Xlint:all</compilerArgument>
            </configuration>
         </plugin>
    </plugins>
    
  • 2

    我在maven-compile-plugin遇到了同样的问题:在jdk9-ea 149下有3.6.0 .

    我注意到这个的实际根本原因是 test 目录没有 module-info.java 文件(我只在 src 目录中添加了一个) . 一旦我添加了模块信息文件,问题就消失了 .

    也就是说,我不知道如何在单元测试中布置拼图目录/模块结构 - 如果测试在一个单独的模块中,他们只能访问公共(导出)API .

相关问题