通过maven生成JPA Hibernate Metamodel

gen*_*qew 14 hibernate jpa

我按照JPA modelgen 指南,我能够生成我需要的规范元模型.设置这个pom:

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.6-redhat</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <outputDirectory>target/metamodel</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/metamodel</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

生成的源在指定的目录中正确创建,我必须手动将其指定为eclipse项目类路径中的源以使用它.当我触发maven时,日志显示cannot find symbol或者duplicate class我仍然可以成功构建.所以我的问题是,这是创建元模型的预期/正确行为吗?还是我错过了cofig的东西?谢谢

小智 16

我也在使用JPA Metamodel生成器,我没有你描述的问题,也许我的配置可以帮助,我看到一些差异,第一个是 maven-processor-plugin

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <version>2.1.0</version>
  <executions>
    <execution>
      <id>process</id>
      <goals>
        <goal>process</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
        <processors>
          <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
        </processors>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-jpamodelgen</artifactId>
      <!--version>1.2.0.Final</version-->
      <version>4.3.4.Final</version>
    </dependency>
  </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我必须添加hibernate-jpamodelgen依赖项和处理器属性.

我不确定是否build-helper-maven-plugin有必要将生成的源目录添加到源路径.我没有使用它,它适用于我,但也许是因为我使用生成源的默认输出路径.