Maven:编译OK,包OK,编译+包失败

我有一个使用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相关的配置。 为什么我不能在build之后运行package

可能相关:我认为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运行在一个干净的项目上时,它默默地生成并编译它的源代码。 然后package看到新的来源,并试图编译它们。 神秘的是它失败的原因。 在同一行中运行两次compile失败的方式相同。

编辑3:我试过这个答案,指定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成功。 但是,如果我运行package ,或再次compile ,它会像以前一样失败。

Dagger生成的源代码位于/target/generated-sources/annotations 我想我需要告诉Maven做这件事情,但我不知道该怎么做。 当连续运行两次时, compile检测到源变更是有道理的,但我不知道为什么会导致失败。 也许我需要有条件地运行注释处理步骤,但我不知道该怎么做或条件应该如何。


我设法重现你的问题。

关键似乎是maven-compiler-plugin:3.3

当我使用maven-compiler-plugin:3.1 ,错误不会显示。

链接地址: http://www.djcxy.com/p/32777.html

上一篇: Maven: compile OK, package OK, compile + package fails

下一篇: Should I be worried about encoding during serialization?