我有一个类似的问题,并尝试了建议的解决方案(两次执行 maven-compiler-plugin,第一次使用proc:none
和第二次使用proc:only
)但遇到了以下问题:
1)运行mvn clean install
:在运行注释处理器之前,两个执行(proc:none
和proc:only
)都将编译源代码并生成类文件:
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ foo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to <mumble>\foo\target\classes
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (process-annotations) @ foo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to <mumble>\foo\target\classes
[MyAnnotationProcessor] processing ...
2)mvn install
在上一次安装之后运行:两次执行 (proc:none
和proc:only
) 都确定类文件是最新的并且什么都不做,并且注释处理器没有运行:
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ foo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (process-annotations) @ foo ---
[INFO] Nothing to compile - all classes are up to date
所以在第一种情况下,java 文件被编译了两次(我的一个模块有数千个文件,这会显着影响构建时间)。而在第二种情况下,什么也没有发生,因为类文件已经存在。这是使用版本 3.1 和 3.0 的maven-compiler-plugin
.
对于maven-compiler-plugin
2.5.1 版本,第二次执行 ( proc:only
) 将始终确定类文件是最新的并且不运行注释处理器,无论我是在运行mvn clean install
还是mvn install
,所以第二次执行是没有意义的。
插件配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<proc>none</proc>
</configuration>
</execution>
<execution>
<id>process-annotations</id>
<goals><goal>compile</goal></goals>
<configuration>
<proc>only</proc>
<annotationProcessors>
<annotationProcessor>MyAnnotationProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</plugin>
所以proc:only
option 似乎在 3.0 和 3.1 版本中编译源代码(使其与 相同proc:both
,这会影响性能)或在 2.5.1 版本中不采取任何操作(使其与 相同proc:none
,这会影响功能)。
不知道如何解决这个问题......
编辑#1
详细输出包括以下内容:
[INFO] --- maven-compiler-plugin:3.1:compile (process-annotations) @ foo ---
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.1:compile' with basic configurator -->
[DEBUG] (f) annotationProcessors = [MyAnnotationProcessor]
[DEBUG] (f) basedir = <mumble>\foo
[DEBUG] (f) buildDirectory = <mumble>\foo\target
[DEBUG] (f) classpathElements = [<mumble>\foo\target\classes, ...]
[DEBUG] (f) compileSourceRoots = [<mumble>\foo\src\main\java]
[DEBUG] (f) compilerId = javac
[DEBUG] (f) debug = true
[DEBUG] (f) failOnError = true
[DEBUG] (f) forceJavacCompilerUse = false
[DEBUG] (f) fork = false
[DEBUG] (f) generatedSourcesDirectory = <mumble>\foo\target\generated-sources\annotations
[DEBUG] (f) mojoExecution = org.apache.maven.plugins:maven-compiler-plugin:3.1:compile {execution: process-annotations}
[DEBUG] (f) optimize = false
[DEBUG] (f) outputDirectory = <mumble>\foo\target\classes
[DEBUG] (f) proc = only
[DEBUG] (f) projectArtifact = foo:jar:1.0-SNAPSHOT
[DEBUG] (f) showDeprecation = false
[DEBUG] (f) showWarnings = false
[DEBUG] (f) skipMultiThreadWarning = false
[DEBUG] (f) source = 1.7
[DEBUG] (f) staleMillis = 0
[DEBUG] (f) target = 1.7
[DEBUG] (f) useIncrementalCompilation = true
[DEBUG] (f) verbose = true
[DEBUG] (f) mavenSession = org.apache.maven.execution.MavenSession@6fb65730
[DEBUG] (f) session = org.apache.maven.execution.MavenSession@6fb65730
[DEBUG] -- end configuration --
[DEBUG] Using compiler 'javac'.
[DEBUG] Source directories: [<mumble>\foo\src\main\java]
[DEBUG] Classpath: [<mumble>\foo\target\classes ... ]
[DEBUG] Output directory: <mumble>\foo\target\classes
[DEBUG] CompilerReuseStrategy: reuseCreated
[DEBUG] useIncrementalCompilation enabled
[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG] <mumble>\foo\target\classes ...
[DEBUG] Source roots:
[DEBUG] <mumble>\foo\src\main\java
[DEBUG] Command line options:
[DEBUG] -d <mumble>\foo\target\classes
-classpath <mumble>\foo\target\classes;...;
-sourcepath <mumble>\foo\src\main\java;
-s <mumble>\foo\target\generated-sources\annotations
-proc:only
-processor MyAnnotationProcessor
-g -verbose -nowarn -target 1.7 -source 1.7
[DEBUG] incrementalBuildHelper#beforeRebuildExecution
[INFO] Compiling 6 source files to <mumble>\foo\target\classes
[parsing started RegularFileObject[<mumble>\foo\src\main\java\Foo.java]]
[parsing completed 0ms]
...
[search path for source files: <mumble>\foo\src\main\java]
[MyAnnotationProcessor] processing ...
即使proc:only
在编译器选项中有一个[parsing started RegularFileObject]
条目......也许它只是记录它但实际上加载了类文件?