为什么注解处理器在Maven和Java 11下不起作用?

huangapple go评论85阅读模式
英文:

Why annotation processor doesn't work with Maven and java11?

问题

我正在将一个Java Web应用程序从Java8迁移到Java17。该项目使用了一个很好的解决方案来嵌入字符串(例如SQL查询)到常量中:https://github.com/adrianwalker/multiline-string 这个库需要使用注解处理器。

在旧版本中,我可以运行 "mvn clean install" 来获取我的应用程序的war文件,这个过程可以正确处理注解。我只需要将该库添加到pom依赖中。

现在在使用Java17时,注解不起作用。然后我尝试在Eclipse IDE中使用它,在这里编译也不起作用。

然后我尝试降级到Java11,并且可以在Eclipse中启动项目,将库设置为Eclipse的Java编译选项中的注解处理器。最后我尝试运行maven install,它成功完成,但注解没有被处理。

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MyApp</groupId>
    <artifactId>MyApp</artifactId>
    <version>1.1.0</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <finalName>ROOT</finalName>
        <resources>
            <resource>
                <directory>src</directory>
                <includes>
                    <include>**/*.so</include>
                    <include>**/*.dll</include>
                    <include>**/*.xml</include>
                    <include>**/*.jrxml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- ...其他依赖... -->
</project>

我还尝试在pom.xml的Maven编译器插件设置中添加以下选项:

<configuration>
    <annotationProcessors>
        <annotationProcessor>org.adrianwalker.multilinestring.MultilineProcessor</annotationProcessor>
    </annotationProcessors>
</configuration>

我无法理解为什么Eclipse可以编译和运行带有注解的应用程序,它还可以导出有效的ROOT.war文件,但Maven完全忽略(没有错误)这些注解。

您知道为什么Maven不运行注解处理器吗?您知道如何获取从项目导出WAR文件时Eclipse调用的命令吗?

编辑:

这些是版本信息:

  • Apache Maven 3.8.7
  • Java版本:17.0.7
  • Eclipse版本:2023-03(4.27.0)
  • M2E Maven Integration for Eclipse:2.2.1.20230307-1553
英文:

I'm migrating a Java webapp from Java8 to Java17.
This project uses this very nice solution to embed strings (ex. SQL queries) in constanst:
https://github.com/adrianwalker/multiline-string
This library requires the annotation processor.

With the old version I could run "mvn clean install" to get the war file of my application, this process correctly processed annotations. I had just to add the library in the pom dependencies.

Now using java17 the annotation doesn't work. Then I tried to use in my Eclipse IDE and here the compilation doesn't work too.

Then I tried to downgrade to Java11 and I could start the project from Eclipse, setting the library as annotation processor in java compile option of Eclipse.
Finally I tried the maven install, it completed successfully but the annotations are not processed.

This is my pom.xml

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;MyApp&lt;/groupId&gt;
&lt;artifactId&gt;MyApp&lt;/artifactId&gt;
&lt;version&gt;1.1.0&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;build&gt;
&lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
&lt;finalName&gt;ROOT&lt;/finalName&gt;
&lt;resources&gt;
&lt;resource&gt;
&lt;directory&gt;src&lt;/directory&gt;
&lt;includes&gt;
&lt;include&gt;**/*.so&lt;/include&gt;
&lt;include&gt;**/*.dll&lt;/include&gt;
&lt;include&gt;**/*.xml&lt;/include&gt;
&lt;include&gt;**/*.jrxml&lt;/include&gt;
&lt;/includes&gt;
&lt;/resource&gt;
&lt;/resources&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.8.1&lt;/version&gt;
&lt;configuration&gt;
&lt;source&gt;11&lt;/source&gt;
&lt;target&gt;11&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
&lt;version&gt;3.3.1&lt;/version&gt;
&lt;configuration&gt;
&lt;warSourceDirectory&gt;WebContent&lt;/warSourceDirectory&gt;
&lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mariadb.jdbc&lt;/groupId&gt;
&lt;artifactId&gt;mariadb-java-client&lt;/artifactId&gt;
&lt;version&gt;2.5.4&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.java.dev.jna&lt;/groupId&gt;
&lt;artifactId&gt;jna&lt;/artifactId&gt;
&lt;version&gt;5.2.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-core&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-websocket&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-messaging&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
&lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
&lt;version&gt;6.1.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.sun.mail&lt;/groupId&gt;
&lt;artifactId&gt;javax.mail&lt;/artifactId&gt;
&lt;version&gt;1.6.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.bouncycastle&lt;/groupId&gt;
&lt;artifactId&gt;bcprov-jdk15on&lt;/artifactId&gt;
&lt;version&gt;1.60&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
&lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
&lt;version&gt;3.8.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.pdfbox&lt;/groupId&gt;
&lt;artifactId&gt;pdfbox&lt;/artifactId&gt;
&lt;version&gt;2.0.13&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.sf.jasperreports&lt;/groupId&gt;
&lt;artifactId&gt;jasperreports&lt;/artifactId&gt;
&lt;version&gt;6.7.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;commons-net&lt;/groupId&gt;
&lt;artifactId&gt;commons-net&lt;/artifactId&gt;
&lt;version&gt;3.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
&lt;version&gt;2.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
&lt;version&gt;2.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
&lt;version&gt;2.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
&lt;version&gt;6.0.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;commons-io&lt;/groupId&gt;
&lt;artifactId&gt;commons-io&lt;/artifactId&gt;
&lt;version&gt;2.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;commons-codec&lt;/groupId&gt;
&lt;artifactId&gt;commons-codec&lt;/artifactId&gt;
&lt;version&gt;1.11&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.bouncycastle&lt;/groupId&gt;
&lt;artifactId&gt;bcpkix-jdk15on&lt;/artifactId&gt;
&lt;version&gt;1.60&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
&lt;artifactId&gt;commons-collections4&lt;/artifactId&gt;
&lt;version&gt;4.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
&lt;artifactId&gt;commons-text&lt;/artifactId&gt;
&lt;version&gt;1.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.validation&lt;/groupId&gt;
&lt;artifactId&gt;validation-api&lt;/artifactId&gt;
&lt;version&gt;2.0.1.Final&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.hibernate.validator&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
&lt;version&gt;6.0.2.Final&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.sf.barcode4j&lt;/groupId&gt;
&lt;artifactId&gt;barcode4j&lt;/artifactId&gt;
&lt;version&gt;2.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.zxing&lt;/groupId&gt;
&lt;artifactId&gt;javase&lt;/artifactId&gt;
&lt;version&gt;3.3.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.xmlgraphics&lt;/groupId&gt;
&lt;artifactId&gt;batik-bridge&lt;/artifactId&gt;
&lt;version&gt;1.10&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.sf.jasperreports&lt;/groupId&gt;
&lt;artifactId&gt;jasperreports-fonts&lt;/artifactId&gt;
&lt;version&gt;6.0.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.lyncode&lt;/groupId&gt;
&lt;artifactId&gt;jtwig-core&lt;/artifactId&gt;
&lt;version&gt;3.1.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.activation&lt;/groupId&gt;
&lt;artifactId&gt;activation&lt;/artifactId&gt;
&lt;version&gt;1.1.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jdbc&lt;/artifactId&gt;
&lt;version&gt;8.5.38&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;version&gt;5.1.15&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
&lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
&lt;version&gt;2.3.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.sun.xml.ws&lt;/groupId&gt;
&lt;artifactId&gt;jaxws-ri&lt;/artifactId&gt;
&lt;version&gt;2.3.0&lt;/version&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;junit&lt;/groupId&gt;
&lt;artifactId&gt;junit&lt;/artifactId&gt;
&lt;version&gt;4.12&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
&lt;artifactId&gt;json-path&lt;/artifactId&gt;
&lt;version&gt;2.4.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.adrianwalker&lt;/groupId&gt;
&lt;artifactId&gt;multiline-string&lt;/artifactId&gt;
&lt;version&gt;0.1.2&lt;/version&gt;
&lt;scope&gt;system&lt;/scope&gt;
&lt;systemPath&gt;${projectPath}\WebContent\WEB-INF\lib\multilineo-string-0.1.2b.jar&lt;/systemPath&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.projectreactor&lt;/groupId&gt;
&lt;artifactId&gt;reactor-core&lt;/artifactId&gt;
&lt;version&gt;3.5.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.projectreactor.netty&lt;/groupId&gt;
&lt;artifactId&gt;reactor-netty&lt;/artifactId&gt;
&lt;version&gt;1.1.7&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.w3c&lt;/groupId&gt;
&lt;artifactId&gt;dom&lt;/artifactId&gt;
&lt;version&gt;2.3.0-jaxb-1.0.6&lt;/version&gt;
&lt;/dependency&gt;		
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.amqp&lt;/groupId&gt;
&lt;artifactId&gt;spring-rabbit&lt;/artifactId&gt;
&lt;version&gt;3.0.4&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;

I also tried to add this option in maven compilter plugin settings of pom.xml

&lt;configuration&gt;
&lt;annotationProcessors&gt;
&lt;annotationProcessor&gt;
org.adrianwalker.multilinestring.MultilineProcessor&lt;/annotationProcessor&gt;
&lt;/annotationProcessors&gt;
&lt;/configuration&gt;

I cannot understand why Eclipse can compile and run the application with annotations, it can also export valid ROOT.war, but Maven ignores (without errors) completely the annotations.

Do you have any idea why maven doesnt run the annotation processor?
Do you know how can I get the eclipse command called when I export WAR file from a project?

EDIT

These are the versions:
Apache Maven 3.8.7
Java version: 17.0.7
Eclipse Version: 2023-03 (4.27.0)
M2E Maven Integration for Eclipse: 2.2.1.20230307-1553

答案1

得分: 1

Java 17支持在代码中直接使用多行字符串(称为文本块)。

不要再使用古老的技巧,只需重构为现代Java。

英文:

Java 17 supports multi line strings directly in code (so-called text blocks).

Instead of making some ancient hack work again, just refactor to modern Java.

huangapple
  • 本文由 发表于 2023年6月13日 04:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定