在 Maven 中设置注释处理器生成的源目录

发布于 2024-08-08 09:13:08 字数 2436 浏览 6 评论 0原文

我正在尝试将使用注释处理器生成源的构建移动到 Maven。我尝试按如下方式配置 maven-compiler-plugin:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <compilerArgument>-s ${project.build.directory}/target/generated-sources/annotation-processing</compilerArgument>
            </configuration>
        </plugin>
    </plugins>

但 javac 失败

[INFO] Compilation failure  
Failure executing javac,  but could not parse the error:
javac: invalid flag: -s /home/robert/workspaces/betbrain/sportsengine.common/sportsengine.bean.test/target/target/generated-sources/annotation-processing  
Usage: javac <options> <source files>  
use -help for a list of possible options

据我所知, -s 应该在源文件之前传递给 javac,但 maven 在之后传递它。

如何将 -s 标志正确传递给 maven-compiler-plugin?


更新maven-annotation-plugin似乎不起作用。

当配置为

        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <outputDirectory>${generated.sources.directory}</outputDirectory>
                        <processors>
                            <processor>xxx.annotation.EnforceJavaBeansConventionsProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

执行失败时

[INFO] [processor:process {execution: process}]
error: Annotation processor 'xxx.annotation.EnforceJavaBeansConventionsProcessor' not found
1 error

I'm trying to move a build which generates sources using an annotation processor to Maven. I've tried configuring the maven-compiler-plugin as follows:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <compilerArgument>-s ${project.build.directory}/target/generated-sources/annotation-processing</compilerArgument>
            </configuration>
        </plugin>
    </plugins>

But javac fails with

[INFO] Compilation failure  
Failure executing javac,  but could not parse the error:
javac: invalid flag: -s /home/robert/workspaces/betbrain/sportsengine.common/sportsengine.bean.test/target/target/generated-sources/annotation-processing  
Usage: javac <options> <source files>  
use -help for a list of possible options

As far as I can tell, -s should be passed before the source files to javac, but maven passes it after.

How can I pass the -s flag properly to the maven-compiler-plugin?


Update: the maven-annotation-plugin does not seem to work.

When configured as

        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <outputDirectory>${generated.sources.directory}</outputDirectory>
                        <processors>
                            <processor>xxx.annotation.EnforceJavaBeansConventionsProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Execution fails with

[INFO] [processor:process {execution: process}]
error: Annotation processor 'xxx.annotation.EnforceJavaBeansConventionsProcessor' not found
1 error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

可是我不能没有你 2024-08-15 09:13:08

该插件使用硬编码的 Windows 类路径分隔符来构建类路径,因此它在我的 Linux 计算机上失败。

提交的补丁:

  • 系统相关路径分隔符
  • < a href="http://code.google.com/p/maven-annotation-plugin/issues/detail?id=6" rel="noreferrer">允许运行默认处理器发现机制

The plugin was using the harcoded Windows classpath separator to build the classpath, so it was failing on my Linux machine.

Submitted patches:

内心激荡 2024-08-15 09:13:08

我可能遗漏了一些东西,但你不应该:

  1. generate-sources阶段在target/ generated-sources/annotation-processing中生成源吗? apt-maven-pluginmaven-annotation-plugin 可以提供帮助。

  2. 使用 maven-compiler-pluginmaven-build-helper-plugin

编辑:xxx.annotation.EnforceJavaBeansConventionsProcessor位于哪里?您不需要将依赖项添加到maven-annotation-plugin的配置中,如使用页面?

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <executions>
    <execution>
      <id>process</id>
      <goals>
        <goal>process</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
        <outputDirectory>src/main/generated</outputDirectory><!-- PROCESSOR OUT DIR --> 
        <processors><!-- LIST OF PROCESSOR CLASS(S) -->
          <processor>org.bsc.apt.BeanInfoAnnotationProcessor</processor>
        </processors>
      </configuration> 
    </execution>
  </executions>
  <dependencies/><!-- ADD DEPENDENCIES HERE IF REQUIRED -->
</plugin>

PS:我不会使用 src/main/ generated 来作为输出目录,而是使用 target/ generated-sources 的子目录。

I may be missing something but shouldn't you:

  1. Generate sources in target/generated-sources/annotation-processing during the generate-sources phase? The apt-maven-plugin or the maven-annotation-plugin could help.

  2. Include generated sources when compiling sources into target/classes using <includes> in the maven-compiler-plugin or the maven-build-helper-plugin?

EDIT: Where is xxx.annotation.EnforceJavaBeansConventionsProcessor located? Don't you need to add dependencies to the configuration of the maven-annotation-plugin as documented on the Usage page?

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <executions>
    <execution>
      <id>process</id>
      <goals>
        <goal>process</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
        <outputDirectory>src/main/generated</outputDirectory><!-- PROCESSOR OUT DIR --> 
        <processors><!-- LIST OF PROCESSOR CLASS(S) -->
          <processor>org.bsc.apt.BeanInfoAnnotationProcessor</processor>
        </processors>
      </configuration> 
    </execution>
  </executions>
  <dependencies/><!-- ADD DEPENDENCIES HERE IF REQUIRED -->
</plugin>

PS: I wouldn't use src/main/generated as output directory but rather a subdirectory of target/generated-sources.

拿命拼未来 2024-08-15 09:13:08

不完全是您问题的答案,但很有趣:

https://issues.apache。 org/jira/browse/MCOMPILER-75

恐怕在 Maven 中使用 JSR 269 会出现很多问题,至少使用默认的编译器插件是这样。

Not exactly an answer to your question, but of interest:

https://issues.apache.org/jira/browse/MCOMPILER-75

I'm afraid there are a number of issues using JSR 269 in Maven, at least with the default compiler plugin.

私藏温柔 2024-08-15 09:13:08

原因之一可能是 $JAVA_HOME 指向 jdk 1.5 版本而不是 1.6。 (在 Windows 上当然检查 %JAVA_HOME%)

One reason for this could be that $JAVA_HOME is pointing to jdk 1.5 version instead of 1.6. (on windows check %JAVA_HOME% of course)

夜司空 2024-08-15 09:13:08

我遇到了 GWTP SourceGenerateAPTProcessingannotations 的问题,

这是因为我没有为编译器插件设置版本,我的最终设置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <!-- <compilerArgument>-proc:none</compilerArgument> -->
        <generatedSourcesDirectory>${generated.sources}</generatedSourcesDirectory>
        <annotationProcessors>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenEventProcessor</annotationProcessor>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenDtoProcessor</annotationProcessor>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenDispatchProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>

I had this issue with GWTP Source Generate APT Processing annotations

It was because I didn't set a version for the compiler plugin, my final setup:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <!-- <compilerArgument>-proc:none</compilerArgument> -->
        <generatedSourcesDirectory>${generated.sources}</generatedSourcesDirectory>
        <annotationProcessors>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenEventProcessor</annotationProcessor>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenDtoProcessor</annotationProcessor>
            <annotationProcessor>com.gwtplatform.dispatch.annotation.processor.GenDispatchProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>
稍尽春風 2024-08-15 09:13:08

我遇到了同样的问题...


[ERROR] COMPILATION ERROR :

[INFO] -------------------------------------------------------------

[ERROR] Failure executing javac, but could not parse the error:

javac: invalid flag: -s
...

你使用 java 5 吗?

$ mvn -version

Apache Maven 2.2.1 (rdebian-8)

Java version: 1.5.0_22

Java home: /usr/lib/jvm/java-1.5.0-sun-1.5.0.22/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux" version: "3.2.0-24-generic" arch: "amd64" Family: "unix"

$ which java
/usr/bin/java

$ ll /usr/bin/java
/usr/bin/java -> /etc/alternatives/java*

$ ll /etc/alternatives/java
/etc/alternatives/java -> /usr/lib/jvm/java-1.5.0-sun/jre/bin/java*

$ sudo update-alternatives --config java



$ mvn -version

Apache Maven 2.2.1 (rdebian-8)

Java version: 1.6.0_24

Java home: /usr/lib/jvm/java-6-openjdk-amd64/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux" version: "3.2.0-24-generic" arch: "amd64" Family: "unix"

现在,mvn install 应该可以工作了:-)

I got the same problem ...


[ERROR] COMPILATION ERROR :

[INFO] -------------------------------------------------------------

[ERROR] Failure executing javac, but could not parse the error:

javac: invalid flag: -s
...

Are you using java 5 ?

$ mvn -version

Apache Maven 2.2.1 (rdebian-8)

Java version: 1.5.0_22

Java home: /usr/lib/jvm/java-1.5.0-sun-1.5.0.22/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux" version: "3.2.0-24-generic" arch: "amd64" Family: "unix"

$ which java
/usr/bin/java

$ ll /usr/bin/java
/usr/bin/java -> /etc/alternatives/java*

$ ll /etc/alternatives/java
/etc/alternatives/java -> /usr/lib/jvm/java-1.5.0-sun/jre/bin/java*

$ sudo update-alternatives --config java



$ mvn -version

Apache Maven 2.2.1 (rdebian-8)

Java version: 1.6.0_24

Java home: /usr/lib/jvm/java-6-openjdk-amd64/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux" version: "3.2.0-24-generic" arch: "amd64" Family: "unix"

now, mvn install should work :-)

一指流沙 2024-08-15 09:13:08

我一直在使用maven-processor-plugin,如下所示:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
      <execution>
        <id>process-my-annotations</id>
        <goals>
          <goal>process</goal>
        </goals>
        <phase>generate-test-sources</phase>
        <configuration>
          <processors>
            <processor>x.y.z.MyAnnotationProcessor</processor>
          </processors>
          <outputDirectory>${project.build.directory}/generated-test-sources/test-annotations</outputDirectory>
          <outputClassDirectory>${project.build.directory}/test-classes</outputClassDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

生成的代码进入target/ generated-test-sources/test-annotations,生成的资源进入< em>target/test-classes(process 目标的默认值是 target/ generated-sourcestarget/classes,分别)。

我还需要使用 maven-build-helper-plugintarget/ generated-test-sources/test-annotations 添加到测试源路径,以便 test -compile阶段将编译生成的代码。

I've been using the maven-processor-plugin as follows:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
      <execution>
        <id>process-my-annotations</id>
        <goals>
          <goal>process</goal>
        </goals>
        <phase>generate-test-sources</phase>
        <configuration>
          <processors>
            <processor>x.y.z.MyAnnotationProcessor</processor>
          </processors>
          <outputDirectory>${project.build.directory}/generated-test-sources/test-annotations</outputDirectory>
          <outputClassDirectory>${project.build.directory}/test-classes</outputClassDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

The generated code goes into target/generated-test-sources/test-annotations, and generated resources into target/test-classes (the default for the process goal is target/generated-sources and target/classes, respectively).

I also need to use maven-build-helper-plugin to add target/generated-test-sources/test-annotations to the test source path so that test-compile phase will compile the generated code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文