gpt4 book ai didi

java - 为什么注释处理在 maven 构建期间运行多次

转载 作者:行者123 更新时间:2023-11-30 11:44:10 27 4
gpt4 key购买 nike

我目前正在使用 java 注释处理生成大量代码(20 秒),当我运行 mvn process-resources 时它只会触发一次 apt 但是当我使用 mvn package, mvn jetty:run 或者mvn gwt:run 打包后容易再次触发。有没有办法强制 apt 只运行一次?

我的 pom.xml 中有一个配置文件

<profile>
<id>codegen</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>src/main/resources/META-INF</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>com.regulationworks.core.code-gen</groupId>
<artifactId>executor</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.0.5</version>
<executions>
<execution>
<id>generate-jpa-metamodel</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
<outputDirectory>${project.build.directory}/generated-sources/metamodel</outputDirectory>
</configuration>
</execution>
<execution>
<id>generate-gwt-jpa-proxies</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.hibernate.gwtmodelgen.GwtProxyModelEntityProcessor</processor>
</processors>
<outputDirectory>${project.build.directory}/generated-sources/entity-proxies</outputDirectory>
</configuration>
</execution>

<execution>
<id>code-gen-executor</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>com.regulationworks.core.gwt.codegen.CodeGenExecutor</processor>
</processors>

<options>
<generateRequestFactory>${core.codegen.requestfactory}</generateRequestFactory>
<generateMappingXml>${core.codegen.xmlmapping}</generateMappingXml>
<codegen.scaffold>${codegen.scaffold}</codegen.scaffold>
<codegen.scaffold.override>${codegen.scaffold.override}</codegen.scaffold.override>
<codegen.scaffold.classes>${codegen.scaffold.classes}</codegen.scaffold.classes>
<codegen.commands.class.name>${codegen.commands.class.name}</codegen.commands.class.name>
</options>
</configuration>
</execution>
<!--<execution>
<id>generate-gwt-rf</id>
<goals>
<goal>process</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<processors>
<processor>com.google.web.bindery.requestfactory.apt.RfValidator</processor>
</processors>
<options>
<rootOverride>${core.codegen.rfvalidate}</rootOverride>
</options>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>-->
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>process-classes</phase>
<configuration>
<id>VerifyRequestFactoryInterfaces</id>
<executable>java</executable>
<arguments>
<argument>-cp</argument>
<classpath />
<argument>com.google.web.bindery.requestfactory.apt.ValidationTool</argument>
<argument>${project.build.outputDirectory}</argument>
<argument>${core.codegen.rfvalidate}</argument>
</arguments>
<skip>${core.codegen.rfvalidate}</skip>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>
</profile>

maven-source-plugin 被配置为“jar-no-fork”(这有助于在运行正常 mvn install 时删除一次)

    <plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

这是运行“mvn clean package gwt:run”时控制台的输出

X:\workspace\Decision\decision-app>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] >>> gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app >>>
***********************************************************
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] <<< gwt-maven-plugin:2.3.0:run (default-cli) @ decision-app <<<
[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
***********************************************************

------------------------解决方案-------------------- ---

  1. 添加“包”配置文件

    <profile>
    <id>package</id>
    <build>
    <plugins>
    <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
    <packagingExcludes>WEB-INF/lib/*-sources.jar, WEB-INF/lib/gwt-user-*.jar</packagingExcludes>
    <webResources>
    <resource>
    <directory>src/main/webapp/WEB-INF</directory>
    <targetPath>WEB-INF</targetPath>
    <includes>
    <include>**/*.xml</include>
    </includes>
    <filtering>true</filtering>
    </resource>
    </webResources>
    </configuration>
    <executions>
    <execution>
    <id>explode-war</id>
    <phase>compile</phase>
    <goals>
    <goal>exploded</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>
    </profile>
  2. 使用“mvn clean gwt:run -Ppackage”代替“mvn clean package gwt:run”

最佳答案

您执行的命令是mvn clean package gwt:run。 Maven 执行clean 生命周期(clean:clean 目标默认)。然后它执行 package,它运行绑定(bind)到包阶段的所有目标和绑定(bind)到先前阶段(生成源、进程源、进程资源等)的所有目标。最后,它执行 gwt:run 目标。如果您查看 documentation对于 run 目标,有一行非常重要:

Invokes the execution of the lifecycle phase process-classes prior to executing itself.

这意味着 Maven 将再次运行绑定(bind)到 process-classes 阶段和所有早期阶段的所有目标。 apt 插件执行绑定(bind)到 generate-sources 阶段,因此它们会重复,就像 resources:resources、compiler:compile 和 exec:exec 一样,因为它们都绑定(bind)到 process-classes 或之前。

考虑到这一点:mvn clean gwt:run 是否有效?我知道 mvn clean jetty:run 可以正常工作,不会重复执行,但我以前没有使用过 gwt。

关于java - 为什么注释处理在 maven 构建期间运行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10909173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com