如何使用 Ant + IntelliJ IDEA 进行 OpenJPA 增强

如何使用 Ant + IntelliJ IDEA 进行 OpenJPA 增强

问题描述:

这让我发疯了,我很震惊官方文档绝对有用.

This is driving me crazy and I'm shocked that official documentation is absolutely useles.

这是我所拥有的:

  • IntelliJ IDEA 11
  • OpenJPA 2.1.1

自从 openjpa 添加到使用的库列表中以来,我已经有了 OpenJPA 的类路径,如下所示

Since openjpa is added into list of used libraries I already had classpath to OpenJPA which looks like this

<path id="library.openjpa.classpath">
    <fileset dir="${basedir}/lib/openjpa">
        <patternset refid="library.patterns"/>
    </fileset>
</path>

根据官方文档,我添加了以下目标

According to official documentation I added following target

<target name="enhance">
    <copy includeemptydirs="false" todir="${basedir}/lib/openjpa">
        <fileset dir="src" excludes="**/*.launch, **/*.java"/>
    </copy>

    <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
        <classpath refid="library.openjpa.classpath"/>
    </taskdef>

    <openjpac>
        <classpath refid="library.openjpa.classpath"/>
    </openjpac>
</target>

它给了我例外

C:\work\prj\build.xml:283: org.apache.openjpa.util.MetaDataException:无法配置 MetaDataFactory(conf.newMetaDataFactoryInstance() 返回空值).这可能意味着没有找到配置属性.确保您有一个META-INF/persistence.xml 文件,它在您的类路径中可用,或者您用于配置的属性文件是可用的.如果您使用的是 Ant,请参阅 或任务的嵌套元素的属性.如果您的 OpenJPA 分发 jar 损坏,也可能发生这种情况,或者如果您的安全政策过于严格.

C:\work\prj\build.xml:283: org.apache.openjpa.util.MetaDataException: MetaDataFactory could not be configured (conf.newMetaDataFactoryInstance() returned null). This might mean that no configuration properties were found. Ensure that you have a META-INF/persistence.xml file, that it is available in your classpath, or that the properties file you are using for configuration is available. If you are using Ant, please see the or attributes of the task's nested element. This can also occur if your OpenJPA distribution jars are corrupt, or if your security policy is overly strict.

我使用 Process Monitor 进行了测试,可以看到它打开并读取 persistence.xml.

I tested with Process Monitor and can see that it opens and reads persistence.xml.

有些人提交了错误遇到了我遇到的问题,他得到的答案是发现 persistence.xml 不是问题的根源.

Some person filed bug having problems I have and the answer he got was that finding persistence.xml is not a source of problem.

问题是:

  1. 我该怎么做才能让它发挥作用?
  2. 我可以通过跳过对 persistence.xml 的需求而只为我想要增强的 .class 文件指定模式来使其工作吗?
  3. 这更像是 Ant 问题.如何让 OpenJPA 增强器在 openjpa-2.1.1.jar 所在的目录以外的目录中查找 persistence.xml ?
  1. What can I do to make it work ?
  2. Can I make it work by skipping need for persistence.xml and just specifying pattern for .class files I want to be enhanced ?
  3. It's more Ant question. How can I make OpenJPA enhancer to look for persistence.xml in directory other than where openjpa-2.1.1.jar resides ?

所以如果没有未记录的 propertiesFile,我无法让它工作.这是对我有用的版本.还通过 # 指定持久性单元使其失败并显示 NullReferenceException.

So I couldn't make it work without undocumented propertiesFile. Here is version that works for me. Also specifying persistence-unit via # makes it fail with NullReferenceException.

<target name="enhance">
    <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
        <classpath refid="library.openjpa.classpath"/>
    </taskdef>

    <openjpac>
        <classpath refid="library.openjpa.classpath"/>
        <classpath location="${reporting.output.dir}"/>
        <config propertiesFile = "${basedir}/src/META-INF/persistence.xml"/>
    </openjpac>
</target>