Maven:编译成功,打包成功,编译+打包失败

Maven: compile OK, package OK, compile + package fails

我有一个使用 Dagger2 并使用 Maven 构建的桌面 Java 项目。我不能按顺序 运行 compilepackage。这有效:

$ mvn clean
$ mvn compile

这有效,生成了一个 运行 没有错误的可执行 jar:

$ mvn clean
$ mvn package

但这失败了:

$ mvn clean
$ mvn compile
$ mvn package

package到达使用Dagger2的模块时,输出:

[INFO] Changes detected - recompiling the module!

来自 mvn -e package 的错误是:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project weapon: Compilation failure -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project weapon: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    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:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.compiler.CompilationFailureException: Compilation failure
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:915)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more

该模块具有此依赖项:

<dependency>
    <groupId>com.google.dagger</groupId>
    <artifactId>dagger</artifactId>
    <version>2.1</version>
    <scope>compile</scope>
</dependency>

该模块使用编译器插件版本 3.3,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.1</version>
        </dependency>
    </dependencies>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <forceJavacCompilerUse>true</forceJavacCompilerUse>
    </configuration>
</plugin>

这是唯一与 Dagger 相关的配置。为什么我不能 运行 packagebuild 之后?

可能相关:我认为不再需要 forceJavacCompilerUse,但没有它,Dagger 似乎根本就不需要 运行。我在生成的组件实现中收到未知符号错误 (DaggerMyComponent)。

编辑: 这是 mvn -e package 的完整输出,命令提示符到命令提示符:

kevin@aphrodite:~/Projects/IDEA/Dark Matter$ mvn -e package
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] parent
[INFO] weapon
[INFO] scripts
[INFO] assembly
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building parent 2.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building weapon 2.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ weapon ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ weapon ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to /home/kevin/Projects/IDEA/Dark Matter/weapon/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] parent ............................................ SUCCESS [0.001s]
[INFO] weapon ............................................ FAILURE [1.641s]
[INFO] scripts ........................................... SKIPPED
[INFO] assembly .......................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.735s
[INFO] Finished at: Fri Mar 11 04:32:38 MST 2016
[INFO] Final Memory: 21M/173M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project weapon: Compilation failure -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project weapon: Compilation failure
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    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:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.compiler.CompilationFailureException: Compilation failure
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:915)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :weapon
kevin@aphrodite:~/Projects/IDEA/Dark Matter$ 

编辑 2:我注意到当我在 clean 之后 运行 package 时,它说它正在编译 6 个源代码。就是这个项目手写了多少类。当我在 package 之前 运行 compile 时,后者说它正在编译 8 个源。看起来当 dagger-compiler 运行s 在一个干净的项目上时,它会静默生成并编译其源代码。然后 package 看到新的源代码并尝试编译它们。神秘之处在于它失败的原因。 运行 compile 连续两次以同样的方式失败。

编辑 3: 我尝试了 this answer,将 dagger.internal.codegen.ComponentProcessor 指定为 -processor 参数。现在我第一次 运行 编译,我看到它编译了 6 个文件然后编译了 8 个文件的两遍:

[INFO] --- maven-compiler-plugin:3.3:compile (process-annotations) @ weapon ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to /home/kevin/Projects/IDEA/Dark Matter/weapon/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ weapon ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ weapon ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to /home/kevin/Projects/IDEA/Dark Matter/weapon/target/classes

这是预期的,compile 成功了。但是如果我再次 运行 packagecompile,它会像以前一样失败。

Dagger 生成的资源在 /target/generated-sources/annotations 中。我想我需要告诉 Maven 对该路径做一些事情,但我不知道该做什么。 compile 在连续两次 运行 时检测源更改是有道理的,尽管我不知道为什么会导致失败。也许我需要有条件地 运行 注释处理步骤,但我不知道该怎么做或条件应该是什么。

我成功重现了你的问题。

关键好像是maven-compiler-plugin:3.3

当我 运行 和 maven-compiler-plugin:3.1 时,错误没有出现。