【问题标题】:Eclipselink 2.5 Metamodel Generation using MavenEclipselink 2.5 使用 Maven 生成元模型
【发布时间】:2013-06-27 10:19:37
【问题描述】:

我想知道如何使用 Maven 和 Eclipselink 2.5 生成静态元模型。在运行 Eclipselink 2.4 时,将此行添加到 pom.xml 可以正常工作。

// Generate meta model for eclipselink 2.4 (does not work for 2.5)
    <plugin>
                    <groupId>org.bsc.maven</groupId>
                    <artifactId>maven-processor-plugin</artifactId>
                    <version>1.3.1</version>
                    <executions>
                        <execution>
                            <id>process</id>
                            <goals>
                                <goal>process</goal>
                            </goals>
                            <phase>generate-sources</phase>
                            <configuration>
                                <compilerArguments>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</compilerArguments>
                                <processors>
                                    <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                                </processors>
                                <outputDirectory>${project.build.directory}/generated-sources/meta-model</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

但似乎自 2.4 以来发生了一些变化,因为我收到以下错误:

[INFO] javac option: -proc:only
[INFO] javac option: -Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml
[INFO] javac option: -processor
[INFO] javac option: org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
[INFO] javac option: -s
[INFO] javac option: /home/asdf/target/generated-sources/meta-model
[INFO] diagnostic error: Annotation processor 'org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor' not found
[INFO] diagnostic warning: Annotation processing without compilation requested but no processors were found.
[ERROR] execute error
java.lang.Exception: error during compilation
    at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.executeWithExceptionsHandled(AbstractAnnotationProcessorMojo.java:183)
    at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.execute(AbstractAnnotationProcessorMojo.java:96)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

你们能帮帮我吗? =)

B.R

【问题讨论】:

标签: maven jpa eclipselink


【解决方案1】:

看来他们已经将 CanonicalModelProcessor 类移到了它自己的 Maven 工件中:

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
  <version>2.5.0</version>
</dependency>

以下 maven-processor-plugin 配置适用于我:

        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>2.2.4</version>
            <executions>
                <execution>
                    <id>eclipselink-jpa-metamodel</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <processors>
                            <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                        </processors>
                        <outputDirectory>${project.build.directory}/generated-sources/meta-model</outputDirectory>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.persistence</groupId>
                    <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
                    <version>2.5.0</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </plugin>

非常重要的说明:只有在持久化单元中声明了实体时才会生成元模型。它不适用于已发现的实体。

【讨论】:

  • 它不适用于 2.5.0,但 2.5.1 可以正常工作 - 签名者信息存在一些问题 - 请参阅下面的答案
  • 在 Eclipse 中,您还必须为“生成的源”目录指定构建路径属性,以便识别那些生成的 java 文件。右键单击项目名称(例如在导航器中)>“属性”>“Java 构建路径”。选择“添加文件夹”并选择“目标”>“生成的源”
  • 再一次,大部分都是不必要的。添加 org.eclipse.persistence.jpa.modelgen.processor 对于 maven 构建来说已经足够了。因为它在 META-INF/services 中注册了 org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor,这就足够了。 Eclipse 可能需要额外的步骤。
  • 非常重要的说明错误。 EclipseLink 2.5.1+ 也会为未列出的实体生成元模型类,只需在 persisetence.xml 中指定 false
  • 如果您需要更改编译器参数,例如更改 persistencexml 路径,请在配置中添加:&lt;compilerArguments&gt;-Aeclipselink.persistencexml=${basedir}/src/main/resources/META-INF/persistence.xml&lt;/compilerArguments&gt;
【解决方案2】:

对我来说,使用 org.apache.maven.plugins 中的 maven-compiler-plugin 在使用除 mvn clean install 之外的其他命令时会导致 MojoFailureException

使用 org.eclipse.persistence.jpa.modelgen.processor 2.6.0测试的解决方案。

这两种情况的配置非常相似。

我对@9​​87654327@ 的主要问题是正确配置compilerArguments 部分。这就是为什么我在下面发布(两个)解决方案。

文档可用:HERE


解决方案使用 maven-compiler-plugin from org.bsc.maven

对我来说这个效果更好

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
        <execution>
            <id>eclipselink-jpa-metamodel</id>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <compilerArguments>
                    -Aeclipselink.persistencexml=${basedir}/src/main/resources/META-INF/persistence.xml
                </compilerArguments>
                <processors>
                    <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
                    </processor>
                </processors>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>${eclipselink.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</plugin>

解决方案使用 maven-compiler-plugin from org.apache.maven.plugins

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
    <version>${eclipselink.version}</version>
    <scope>compile</scope>
</dependency>

...

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArgs>
            <arg>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</arg>
        </compilerArgs>
    </configuration>
</plugin>

【讨论】:

    【解决方案3】:

    为了使配置更简单,我建议您测试:https://github.com/ethlo/eclipselink-maven-plugin。您甚至不需要保留更新的persistence.xml 文件。

    <plugin>
    <groupId>com.ethlo.persistence.tools</groupId>
    <artifactId>eclipselink-maven-plugin</artifactId>
    <version>[version]</version>
    <executions>
        <execution>
            <id>weave</id>
            <phase>process-classes</phase>
            <goals>
                <goal>weave</goal>
            </goals>
        </execution>
        <execution>
            <id>modelgen</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>modelgen</goal>
            </goals>
        </execution>
    </executions>
    </plugin>
    

    注意:我是插件的作者。

    【讨论】:

      【解决方案4】:

      您可能会收到以下错误:

      java.lang.RuntimeException: java.lang.SecurityException: class "org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProperties"'s signer information does not match signer information of other classes in the same package

      由于未解决的错误compilation is broken, modelgen jar is signed in maven repository,现在必须将版本设置为2.5.0-SNAPSHOT。

      【讨论】:

        【解决方案5】:

        2017 年更新:

        这个问题的主要答案现在已经过时了。 您现在需要执行以下步骤才能使其正常工作。

        1) 导入需要的依赖:

        <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/org.eclipse.persistence.jpa.modelgen.processor -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.2</version>
            <scope>compile</scope>
        </dependency>
        

        2) 指定 persistence.xml 位置(这是 EL 错误的一种解决方法。请注意,您的路径可能与本示例中指定的路径不同):

        <build>
          <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <executions>
                        <execution>
                            <id>default-compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        [...]
                    </executions>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <compilerArgs>
                            <arg>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</arg>
                        </compilerArgs>
                    </configuration>
            </plugins>
            [...]
          </pluginManagement>
          [...]
        </build>
        

        3) 最后参考以下项目以使用 Maven 的新 Lifecycle Mappings 触发执行: Usage of maven Build Helper Maven Plugin

        【讨论】:

          【解决方案6】:

          这是我用的

          <dependency>
                  <groupId>org.eclipse.persistence</groupId>
                  <artifactId>javax.persistence</artifactId>
                  <version>2.1.0</version>
                  <scope>provided</scope>
              </dependency>
          
              <dependency>
                  <groupId>org.eclipse.persistence</groupId>
                  <artifactId>eclipselink</artifactId>
                  <version>2.5.1</version>
                  <scope>provided</scope>
              </dependency>
          
              <dependency>
                      <groupId>org.eclipse.persistence</groupId>
                      <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
                      <version>2.5.1</version>
                      <scope>provided</scope>
                  </dependency>
          
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.1</version>
                      <configuration>
                          <source>1.6</source>
                          <target>1.6</target>
                          <compilerArguments>
                              <endorseddirs>${endorsed.dir}</endorseddirs>
                          </compilerArguments>
                      </configuration>
                      <executions>
                          <execution>
                              <id>generate-entity-metamodel</id>
                              <phase>generate-sources</phase>
                              <goals>
                                  <goal>
                                      compile
                                  </goal>
                              </goals>
                              <configuration>
                                  <source>1.6</source>
                                  <target>1.6</target>
                                  <optimize>true</optimize>
                                  <showDeprecation>true</showDeprecation>
                                  <showWarnings>true</showWarnings>
                                  <proc>only</proc>
                                  <!--<compilerArgument>-Aeclipselink.metamodel=true</compilerArgument>
                                  <generatedSourcesDirectory>${basedir}/src/main/java</generatedSourcesDirectory>-->
                              </configuration>
                          </execution>
                      </executions>
                  </plugin>
                  <plugin>
                      <groupId>org.codehaus.mojo</groupId>
                      <artifactId>build-helper-maven-plugin</artifactId>
                      <version>1.8</version>
                      <executions>
                          <execution>
                              <id>add-modelgen-generated-sources-directory</id>
                              <phase>generate-sources</phase>
                              <goals>
                                  <goal>add-source</goal>
                              </goals>
                              <configuration>
                                  <sources>
                                      <source>${project.build.directory}/target/generated-sources/annotations</source>
                                  </sources>
                              </configuration>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
          

          add-sources 部分是为了让 netbeans 8 理解它以将其放入类路径中 :)

          【讨论】:

          • Netbeans 只是运行 maven,所以这是您需要解决的问题。不过,这不是必需的,只需修复您的构建即可。
          • @ymajoros 你是什么意思?
          • 只是为了生成元模型,只是一个大的构造,只需添加~modelgen.processor 依赖就足够了。
          • 我有在 target/generated-sources 中生成 java 源的示例项目,netbeans 显示编译错误,如果您想查看它们,我很乐意与您分享
          猜你喜欢
          • 2012-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-07
          • 1970-01-01
          • 2011-01-11
          • 2015-08-26
          相关资源
          最近更新 更多