Maven 3 - 如何添加注释处理器依赖?

ber*_*nie 25 java maven-3 maven annotation-processing jpa-2.0

我需要在项目的源代码上运行注释处理器.注释处理器不应该成为项目的传递依赖项,因为它只需要注释处理而不需要其他内容.

这是我用于此的完整(非工作)测试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>
Run Code Online (Sandbox Code Playgroud)

org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor在测试的插件配置中明确定义为注释处理器,我知道它不应该是必需的.

我遇到的问题是hibernate-jpamodelgen依赖项没有添加到编译器类路径中,因此找不到注释处理器并且构建失败.

根据这个答案,我尝试将依赖项添加为构建扩展(不确定我理解那些应该是什么!),如下所示:

<extensions>
  <extension>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>${hibernate-jpamodelgen.version}</version>
  </extension>
</extensions>
Run Code Online (Sandbox Code Playgroud)

这也不会添加hibernate-jpamodelgen到编译器类路径中.

到目前为止,我发现唯一可行的方法是在该<dependencies>部分中将依赖项添加到项目中.这有一个令人遗憾的副作用,hibernate-jpamodelgen即后来添加作为传递依赖,我想避免.

我以前的工作设置使用maven-processor-plugin插件来实现我想要的.但是,eclipse m2e不支持这个插件,而maven-compiler-plugin现在的最新版本正确处理多个编译器参数,所以我宁愿使用后者.

Ale*_*øld 22

将依赖项添加为可选依赖项(<optional>true</optional>).这将在编译时添加依赖项,但会阻止它成为传递依赖项:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen</artifactId>
  <version>${hibernate-jpamodelgen.version}</version>
  <optional>true</optional>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如果你在这个模块中创建了一个包含所有依赖项的工件(比如.war),你可以使用它<scope>provided</scope>.这既可以防止依赖性传递,也可以包含在模块生成的工件中.

  • “可选”只是意味着依赖于这个项目的其他项目不需要它。如果他们想要它,他们必须明确声明。通常其他项目不需要你的注释处理器,所以它是可选的(不是必需的)。为了使注释处理器工作,javac 在编译时类路径上找到它们,这是 maven 中的编译范围。"provided" 是一些打包/捆绑编译时依赖项的插件的特殊范围。他们经常感到困惑,但是如果您考虑其他项目需要从您的项目中获得什么,当它们依赖于您的项目时,会更容易。 (2认同)

Gun*_*nar 19

annotationProcessorPaths选项可以在最新版本的Maven的编译器插件的使用:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>5.2.6.Final</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>
Run Code Online (Sandbox Code Playgroud)

这样,处理器就与实际的项目依赖关系分开了.如果为项目启用了注释处理,则Eclipse M2E插件也会选择此选项.


Mar*_*gon 9

对于JDK 10,我真的不得不有点疯狂才能让它工作,希望有人觉得这很有用

<jaxb.version>2.3.0</jaxb.version>
<maven.hibernate.version>5.3.2.Final</maven.hibernate.version>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven.compiler.version}</version>
    <goals>
        <goal>compile</goal>
    </goals>
    <configuration>
        <outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>${maven.hibernate.version}</version>
            </annotationProcessorPath>
            <annotationProcessorPath>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>${jaxb.version}</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
        <annotationProcessors>
            <annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
        </annotationProcessors>
        <compilerArgs>
            <arg>-AaddGeneratedAnnotation=false</arg>
        </compilerArgs>
        <compilerArguments>
            <AaddGeneratedAnnotation>false</AaddGeneratedAnnotation>
            <Adebug>true</Adebug>
        </compilerArguments>
        <failOnError>true</failOnError>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${maven.hibernate.version}</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jaxb.version}</version>
            <type>jar</type>
        </dependency>
    </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)


Vad*_*dim 8

问题实际上是3.*版本的maven-compiler-plugin.它与2.*版本有点不同.特别是,似乎maven-compiler-plugin3.*没有添加其依赖项并构建类路径的扩展,因为它使用javax.tools工具来运行编译过程.要为maven-compiler-plugin您恢复旧的行为,应使用新的配置属性forceJavacCompilerUse:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
    <forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
....
</plugin>
Run Code Online (Sandbox Code Playgroud)