为 maven-processor-plugin 编写注释处理器

发布于 2024-10-21 18:17:36 字数 149 浏览 1 评论 0原文

我有兴趣为 maven-processor-plugin 编写一个注释处理器。我对 Maven 比较陌生。

处理器 Java 源代码应该位于项目路径中的哪个位置(例如:src/main/java/...),以便正确编译它,但最终不会成为我的工件 JAR 文件的一部分?

I am interested in writing an annotation processor for the maven-processor-plugin. I am relatively new to Maven.

Where in the project path should the processor Java source code go (e.g.: src/main/java/...) so that it gets compiled appropriately, but does not end up as part of my artifact JAR file?

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

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

发布评论

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

评论(1

谁的年少不轻狂 2024-10-28 18:17:36

最简单的方法是将注释处理器保留在作为依赖项包含的单独项目中。

如果这对您不起作用,请使用此配置

编译器插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.5</source>
        <target>1.5</target>
    </configuration>
    <inherited>true</inherited>
    <executions>
        <execution>
            <id>default-compile</id>
            <inherited>true</inherited>
            <configuration>
                <!-- limit first compilation run to processor -->
                <includes>path/to/processor</includes>
            </configuration>
        </execution>
        <execution>
            <id>after-processing</id>
            <phase>process-classes</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
                <excludes>path/to/processor</excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

处理器插件:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
                <processors>
                    <processor>com.yourcompany.YourProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
</plugin>

(请注意,这必须在两次编译运行之间执行,因此至关重要将此代码放置在 pom.xml 之后上述 maven-compiler-plugin 配置)

Jar 插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <excludes>path/to/processor</excludes>
    </configuration>
    <inherited>true</inherited>
</plugin>

The easiest way is to keep your annotation processor in a separate project that you include as dependency.

If that doesn't work for you, use this config

Compiler Plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.5</source>
        <target>1.5</target>
    </configuration>
    <inherited>true</inherited>
    <executions>
        <execution>
            <id>default-compile</id>
            <inherited>true</inherited>
            <configuration>
                <!-- limit first compilation run to processor -->
                <includes>path/to/processor</includes>
            </configuration>
        </execution>
        <execution>
            <id>after-processing</id>
            <phase>process-classes</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
                <excludes>path/to/processor</excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

Processor Plugin:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
                <processors>
                    <processor>com.yourcompany.YourProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
</plugin>

(Note that this must be executed between the two compile runs, so it is essential that you place this code in the pom.xml after the above maven-compiler-plugin configuration)

Jar Plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <excludes>path/to/processor</excludes>
    </configuration>
    <inherited>true</inherited>
</plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文