我需要在我的项目源上运行注释处理器。注释处理器不应成为项目的传递依赖项,因为它仅用于注释处理而没有其他需要。
这是我用于此的完整(非工作)测试 pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test annotations</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate-jpamodelgen.version>1.2.0.Final</hibernate-jpamodelgen.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<annotationProcessors>
<annotationProcessor>
org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
</annotationProcessors>
<debug>true</debug>
<optimize>true</optimize>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<AaddGeneratedAnnotation>true</AaddGeneratedAnnotation>
<Adebug>true</Adebug>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
我在插件配置中明确定义org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
为注释处理器以进行测试,并且我知道它不应该是必需的。
我遇到的问题是hibernate-jpamodelgen
依赖项没有添加到编译器类路径中,因此找不到注释处理器并且构建失败。
根据这个答案,我尝试将依赖项添加为构建扩展(不确定我是否理解那些应该是什么!),如下所示:
<extensions>
<extension>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
</extension>
</extensions>
这也不会添加hibernate-jpamodelgen
到编译器类路径中。
到目前为止,我发现唯一有效的是将依赖项添加到该<dependencies>
部分的项目中。这有一个不幸的副作用,hibernate-jpamodelgen
即我想避免之后添加作为传递依赖项。
我以前的工作设置使用maven-processor-plugin
插件来实现我想要的。但是,eclipse m2e 不支持此插件,并且最新版本的maven-compiler-plugin
now 可以正确处理多个编译器参数,因此我宁愿使用后者。