title: Why annotation processing run multiple times during maven build
body: <p>I'm currently using java annotation processing to generate a lot of code (20 seconds), when I run <code>mvn process-resources</code> it will trigger apt only once however when I use <code>mvn package</code>, <code>mvn jetty:run</code> or <code>mvn gwt:run</code> apt been triggered again after war been packaged. Is there a way to force apt only run once?</p>
  
  <p>I have a profile in my pom.xml</p>
  
  <pre><code>&lt;profile&gt;
      &lt;id&gt;codegen&lt;/id&gt;
      &lt;activation&gt;
          &lt;activeByDefault&gt;false&lt;/activeByDefault&gt;
          &lt;file&gt;
              &lt;exists&gt;src/main/resources/META-INF&lt;/exists&gt;
          &lt;/file&gt;
      &lt;/activation&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.regulationworks.core.code-gen&lt;/groupId&gt;
              &lt;artifactId&gt;executor&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.bsc.maven&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-processor-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.0.5&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;generate-jpa-metamodel&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;process&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;phase&gt;generate-sources&lt;/phase&gt;
                          &lt;configuration&gt;
                              &lt;processors&gt;
                                  &lt;processor&gt;org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor&lt;/processor&gt;
                              &lt;/processors&gt;
                              &lt;outputDirectory&gt;${project.build.directory}/generated-sources/metamodel&lt;/outputDirectory&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                      &lt;execution&gt;
                          &lt;id&gt;generate-gwt-jpa-proxies&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;process&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;phase&gt;generate-sources&lt;/phase&gt;
                          &lt;configuration&gt;
                              &lt;processors&gt;
                                  &lt;processor&gt;org.hibernate.gwtmodelgen.GwtProxyModelEntityProcessor&lt;/processor&gt;
                              &lt;/processors&gt;
                              &lt;outputDirectory&gt;${project.build.directory}/generated-sources/entity-proxies&lt;/outputDirectory&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
  
                      &lt;execution&gt;
                          &lt;id&gt;code-gen-executor&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;process&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;phase&gt;generate-sources&lt;/phase&gt;
                          &lt;configuration&gt;
                              &lt;processors&gt;
                                  &lt;processor&gt;com.regulationworks.core.gwt.codegen.CodeGenExecutor&lt;/processor&gt;
                              &lt;/processors&gt;
  
                              &lt;options&gt;
                                  &lt;generateRequestFactory&gt;${core.codegen.requestfactory}&lt;/generateRequestFactory&gt;
                                  &lt;generateMappingXml&gt;${core.codegen.xmlmapping}&lt;/generateMappingXml&gt;
                                  &lt;codegen.scaffold&gt;${codegen.scaffold}&lt;/codegen.scaffold&gt;
                                  &lt;codegen.scaffold.override&gt;${codegen.scaffold.override}&lt;/codegen.scaffold.override&gt;
                                  &lt;codegen.scaffold.classes&gt;${codegen.scaffold.classes}&lt;/codegen.scaffold.classes&gt;
                                  &lt;codegen.commands.class.name&gt;${codegen.commands.class.name}&lt;/codegen.commands.class.name&gt;
                              &lt;/options&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                      &lt;!--&lt;execution&gt;
                                 &lt;id&gt;generate-gwt-rf&lt;/id&gt;
                                 &lt;goals&gt;
                                     &lt;goal&gt;process&lt;/goal&gt;
                                 &lt;/goals&gt;
                                 &lt;phase&gt;process-classes&lt;/phase&gt;
                                 &lt;configuration&gt;
                                     &lt;processors&gt;
                                         &lt;processor&gt;com.google.web.bindery.requestfactory.apt.RfValidator&lt;/processor&gt;
                                     &lt;/processors&gt;
                                     &lt;options&gt;
                                     &lt;rootOverride&gt;${core.codegen.rfvalidate}&lt;/rootOverride&gt;
                                     &lt;/options&gt;
                                     &lt;outputDirectory&gt;${project.build.outputDirectory}&lt;/outputDirectory&gt;
                                 &lt;/configuration&gt;
                             &lt;/execution&gt;--&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.2&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;process-classes&lt;/phase&gt;
                          &lt;configuration&gt;
                              &lt;id&gt;VerifyRequestFactoryInterfaces&lt;/id&gt;
                              &lt;executable&gt;java&lt;/executable&gt;
                              &lt;arguments&gt;
                                  &lt;argument&gt;-cp&lt;/argument&gt;
                                  &lt;classpath /&gt;
                                  &lt;argument&gt;com.google.web.bindery.requestfactory.apt.ValidationTool&lt;/argument&gt;
                                  &lt;argument&gt;${project.build.outputDirectory}&lt;/argument&gt;
                                  &lt;argument&gt;${core.codegen.rfvalidate}&lt;/argument&gt;
                              &lt;/arguments&gt;
                              &lt;skip&gt;${core.codegen.rfvalidate}&lt;/skip&gt;
                          &lt;/configuration&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;exec&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
  
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/profile&gt;
  </code></pre>
  
  <p>and maven-source-plugin been configured to 'jar-no-fork' (this helped to remove one time when run normal mvn install)</p>
  
  <pre><code>    &lt;plugin&gt;
          &lt;artifactId&gt;maven-source-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.1.2&lt;/version&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;goals&gt;
                      &lt;goal&gt;jar-no-fork&lt;/goal&gt;
                  &lt;/goals&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
      &lt;/plugin&gt;
  </code></pre>
  
  <p>here is output in console when run "mvn clean package gwt:run"</p>
  
  <pre><code>X:\workspace\Decision\decision-app&gt;mvn clean package gwt:run
  [INFO] Scanning for projects...
  [INFO]
  [INFO] ------------------------------------------------------------------------
  [INFO] Building DecisionWorks App 2.0.2-SNAPSHOT
  [INFO] ------------------------------------------------------------------------
  [WARNING] The POM for org.opensaml:opensaml:jar:1.1b is missing, no dependency information available
  [WARNING] The POM for net.sf.saxon:saxon-dom:jar:8.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
  [INFO]
  [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ decision-app ---
  [INFO] Deleting X:\workspace\Decision\decision-app\target
  [INFO]
  ***********************************************************
   This is where apt executed first time which look right
  ***********************************************************
  [INFO] --- maven-processor-plugin:2.0.5:process (generate-jpa-metamodel) @ decision-app ---
  [INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\metamodel added
  [INFO] javac option: -cp
  [INFO] javac option: ...
  [INFO] javac option: -proc:only
  [INFO] javac option: -processor
  [INFO] javac option: org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
  [INFO] javac option: -d
  [INFO] javac option: X:\workspace\Decision\decision-app\target\classes
  [INFO] javac option: -s
  [INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\metamodel
  [INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 1.0.0.Final
  [INFO]
  [INFO] --- maven-processor-plugin:2.0.5:process (generate-gwt-jpa-proxies) @ decision-app ---
  [INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies added
  [INFO] javac option: -cp
  [INFO] javac option: ...
  [INFO] javac option: -proc:only
  [INFO] javac option: -processor
  [INFO] javac option: org.hibernate.gwtmodelgen.GwtProxyModelEntityProcessor
  [INFO] javac option: -d
  [INFO] javac option: X:\workspace\Decision\decision-app\target\classes
  [INFO] javac option: -s
  [INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies
  [INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 3.7.0-SNAPSHOT
  [INFO]
  [INFO] --- maven-processor-plugin:2.0.5:process (code-gen-executor) @ decision-app ---
  [INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\apt added
  [INFO] Adding compiler arg: -Acodegen.scaffold.override=true
  [INFO] javac option: -cp
  [INFO] javac option: ...
  [INFO] javac option: -proc:only
  [INFO] javac option: -Acodegen.scaffold.override=true
  [INFO] javac option: -processor
  [INFO] javac option: com.regulationworks.core.gwt.codegen.CodeGenExecutor
  [INFO] javac option: -d
  [INFO] javac option: X:\workspace\Decision\decision-app\target\classes
  [INFO] javac option: -s
  [INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\apt
  [INFO]
  [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ decision-app ---
  [debug] execute contextualize
  [INFO] Using 'UTF-8' encoding to copy filtered resources.
  [INFO] Copying 10 resources
  [INFO] Copying 17 resources
  [INFO] Copying 80 resources
  [INFO]
  [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ decision-app ---
  [INFO] Compiling 260 source files to X:\workspace\Decision\decision-app\target\classes
  [INFO]
  [INFO] --- exec-maven-plugin:1.2:exec (default) @ decision-app ---
  [INFO]
  [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ decision-app ---
  [debug] execute contextualize
  [INFO] Using 'UTF-8' encoding to copy filtered resources.
  [INFO] Copying 3 resources
  [INFO] Copying 3 resources
  [INFO] Copying 10 resources
  [INFO] Copying 17 resources
  [INFO] Copying 80 resources
  [INFO]
  [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ decision-app ---
  [INFO] Compiling 3 source files to X:\workspace\Decision\decision-app\target\test-classes
  [INFO]
  [INFO] --- maven-surefire-plugin:2.9:test (default-test) @ decision-app ---
  [INFO] Surefire report directory: X:\workspace\Decision\decision-app\target\surefire-reports
  
  -------------------------------------------------------
   T E S T S
  -------------------------------------------------------
  ...
  Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.172 sec
  
  Results :
  
  Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
  
  [INFO]
  [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ decision-app ---
  [INFO] Packaging webapp
  [INFO] Assembling webapp [decision-app] in [X:\workspace\Decision\decision-app\target\decision]
  [INFO] Processing war project
  [INFO] Copying webapp webResources [X:\workspace\Decision\decision-app\target] to [X:\workspace\Decision\decision-app\target\decision]
  [INFO] Copying webapp webResources [X:\workspace\Decision\decision-app\src/main/webapp/WEB-INF] to [X:\workspace\Decision\decision-app\target\decision]
  [INFO] Copying webapp resources [X:\workspace\Decision\decision-app\src\main\webapp]
  [INFO] Processing overlay [ id com.regulationworks.core:document-cmis-plugin-config]
  [INFO] Webapp assembled in [10118 msecs]
  [INFO] Building war: X:\workspace\Decision\decision-app\target\decision.war
  [WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
  (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
  [INFO]
  [INFO] --- maven-source-plugin:2.1.2:jar-no-fork (default) @ decision-app ---
  [INFO]
  [INFO] &gt;&gt;&gt; gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app &gt;&gt;&gt;
  ***********************************************************
  This is where apt executed second time which is a waste of time
  ***********************************************************
  [INFO]
  [INFO] --- maven-processor-plugin:2.0.5:process (generate-jpa-metamodel) @ decision-app ---
  [INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\metamodel added
  [INFO] javac option: -cp
  [INFO] javac option: ...
  [INFO] javac option: -proc:only
  [INFO] javac option: -processor
  [INFO] javac option: org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
  [INFO] javac option: -d
  [INFO] javac option: X:\workspace\Decision\decision-app\target\classes
  [INFO] javac option: -s
  [INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\metamodel
  [INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 1.0.0.Final
  [INFO]
  [INFO] --- maven-processor-plugin:2.0.5:process (generate-gwt-jpa-proxies) @ decision-app ---
  [INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies added
  [INFO] javac option: -cp
  [INFO] javac option: ...
  [INFO] javac option: -proc:only
  [INFO] javac option: -processor
  [INFO] javac option: org.hibernate.gwtmodelgen.GwtProxyModelEntityProcessor
  [INFO] javac option: -d
  [INFO] javac option: X:\workspace\Decision\decision-app\target\classes
  [INFO] javac option: -s
  [INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\entity-proxies
  [INFO] diagnostic Note: Hibernate JPA 2 Static-Metamodel Generator 3.7.0-SNAPSHOT
  [INFO]
  [INFO] --- maven-processor-plugin:2.0.5:process (code-gen-executor) @ decision-app ---
  [INFO] Source directory: X:\workspace\Decision\decision-app\target\generated-sources\apt added
  [INFO] Adding compiler arg: -Acodegen.scaffold.override=true
  [INFO] javac option: -cp
  [INFO] javac option: ...
  [INFO] javac option: -proc:only
  [INFO] javac option: -Acodegen.scaffold.override=true
  [INFO] javac option: -processor
  [INFO] javac option: com.regulationworks.core.gwt.codegen.CodeGenExecutor
  [INFO] javac option: -d
  [INFO] javac option: X:\workspace\Decision\decision-app\target\classes
  [INFO] javac option: -s
  [INFO] javac option: X:\workspace\Decision\decision-app\target\generated-sources\apt
  [INFO]
  [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ decision-app ---
  [debug] execute contextualize
  [INFO] Using 'UTF-8' encoding to copy filtered resources.
  [INFO] Copying 10 resources
  [INFO] Copying 17 resources
  [INFO] Copying 80 resources
  [INFO]
  [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ decision-app ---
  [INFO] Compiling 60 source files to X:\workspace\Decision\decision-app\target\classes
  [INFO]
  [INFO] --- exec-maven-plugin:1.2:exec (default) @ decision-app ---
  [INFO]
  [INFO] &lt;&lt;&lt; gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app &lt;&lt;&lt;
  [INFO]
  [INFO] --- gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app ---
  ***********************************************************
  finally be able to start the web app, 30 seconds wasted for second apt execution
  ***********************************************************
  </code></pre>
  
  <p><strong>----------------------- solution -----------------------</strong></p>
  
  <ol>
  <li><p>add a "package" profile</p>
  
  <pre><code>&lt;profile&gt;
      &lt;id&gt;package&lt;/id&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.1.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;packagingExcludes&gt;WEB-INF/lib/*-sources.jar, WEB-INF/lib/gwt-user-*.jar&lt;/packagingExcludes&gt;
                      &lt;webResources&gt;
                          &lt;resource&gt;
                              &lt;directory&gt;src/main/webapp/WEB-INF&lt;/directory&gt;
                              &lt;targetPath&gt;WEB-INF&lt;/targetPath&gt;
                              &lt;includes&gt;
                                  &lt;include&gt;**/*.xml&lt;/include&gt;
                              &lt;/includes&gt;
                              &lt;filtering&gt;true&lt;/filtering&gt;
                          &lt;/resource&gt;
                      &lt;/webResources&gt;
                  &lt;/configuration&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;explode-war&lt;/id&gt;
                          &lt;phase&gt;compile&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;exploded&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/profile&gt;
  </code></pre></li>
  <li><p>use "mvn clean gwt:run -Ppackage" instead of "mvn clean package gwt:run"</p></li>
  </ol>