当没有persistence.xml时,如何使用EclipseLink静态编织JPA实体,因为这些实体由Spring管理

Pau*_*NUK 6 spring eclipselink compile-time-weaving spring-data-jpa

我有一个基于Spring的项目,因此以编程方式设置了实体管理器,不需要persistence.xml文件列出所有实体。

我目前正在使用加载时间编织,但是正在尝试使用Eclipselink和Gradle进行静态编织。我想复制由Maven eclipselink插件执行的操作:

https://github.com/ethlo/eclipselink-maven-plugin

我设置了以下gradle(请注意,这不是Kotlin DSL的常规做法):

task<JavaExec>("performJPAWeaving") {


    val compileJava: JavaCompile = tasks.getByName("compileJava") as JavaCompile
    dependsOn(compileJava)

    val destinationDir = compileJava.destinationDir

    println("Statically weaving classes in $destinationDir")
    inputs.dir(destinationDir)
    outputs.dir(destinationDir)
    main = "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
    args = listOf("-persistenceinfo", "src/main/resources", destinationDir.getAbsolutePath(), destinationDir.getAbsolutePath())

    classpath = configurations.getByName("compile")


}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行任务时,编织任务失败,因为它正在寻找不存在的persistence.xml。

有什么方法可以在基于Spring的JPA项目中静态编织JPA实体?

Exception Description: An exception was thrown while processing persistence.xml from URL: file:/home/blabla/trunk/my-module/src/main/resources/
Internal Exception: java.net.MalformedURLException
        at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionProcessingPersistenceXML(PersistenceUnitLoadingException.java:117)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:579)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceArchive(PersistenceUnitProcessor.java:536)
        ... 6 more
Caused by: java.net.MalformedURLException
        at java.net.URL.<init>(URL.java:627)
        at java.net.URL.<init>(URL.java:490)
        at java.net.URL.<init>(URL.java:439)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:620)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:806)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:577)
        ... 7 more
Caused by: java.lang.NullPointerException
        at java.net.URL.<init>(URL.java:532)
        ... 17 more
Run Code Online (Sandbox Code Playgroud)

gal*_*ics 2

虽然有点晚了,但这对于 Gradle 来说绝对是可能的。

要完成这项工作,需要执行 3 个步骤:

  • 将 persistence.xml 文件复制到类旁边的源文件夹中
  • 做编织
  • persistence.xml从类源文件夹中删除文件以避免persistence.xml类路径上的重复冲突

另外,将编织过程挂钩到compileJava任务的最后一步非常重要,以免破坏Gradle的最新检查,否则Gradle将一直重新编译所有内容,这在开发时会非常不方便。

有关更详细的解释,请查看我的文章:EclipseLink static weaving with Gradle