Java 如何启用Eclipselink';这是格拉德尔的静态织造
我想为来自Gradle的JPA类启用Eclipselink的静态编织。Eclipselink文档解释了如何在Ant任务中做到这一点:Java 如何启用Eclipselink';这是格拉德尔的静态织造,java,ant,gradle,eclipselink,Java,Ant,Gradle,Eclipselink,我想为来自Gradle的JPA类启用Eclipselink的静态编织。Eclipselink文档解释了如何在Ant任务中做到这一点: <target name="define.task" description="New task definition for EclipseLink static weaving"/> <taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.Stat
<target name="define.task" description="New task definition for EclipseLink static weaving"/>
<taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask"/>
</target>
<target name="weaving" description="perform weaving" depends="define.task">
<weave source="c:\myjar.jar"
target="c:\wovenmyjar.jar"
persistenceinfo="c:\myjar-containing-persistenceinfo.jar">
<classpath>
<pathelement path="c:\myjar-dependent.jar"/>
</classpath>
</weave>
</target>
类路径设置适用于ant.taskdef(..)
,因为在我的依赖项中可以找到StaticWeaveAntTask。如何将其应用于ant.weave(…)本身
2。如何将其集成到我的构建中,以便在每个
compileJava
步骤后自动执行?我知道这是一个老问题,但基于OP对“gradle”方法的评论,我想我会分享我们的方法。我们正在使用JavaExec任务和各种可用的配置对象
因为编织是在classes目录中完成的(在构建JAR之前),所以最终只需要构建一个JAR,而不是两个。因为我们的罐子很大,这对我们很重要
task performJPAWeaving(type: JavaExec, dependsOn: "compileJava"){
inputs.dir compileJava.destinationDir
outputs.dir compileJava.destinationDir
main "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
args "-persistenceinfo",
"src/main/resources",
compileJava.destinationDir.getAbsolutePath()
classpath = configurations.compileClasspath
}
tasks.withType(Jar){
dependsOn "performJPAWeaving"
}
问题描述
我有一个类似的问题,我想分享我的解决方案,这是基于前一篇文章。解决了以下问题:
- 在编织之前处理persistence.xml并替换令牌
- 找到一个已知的解决方法,防止将Weavers-persistenceunit标志与Gradle一起使用
- 自动发现实体类,而不是在persistence.xml中列出它们
import org.apache.tools.ant.filters.ReplaceTokens
ext {
dsName = 'MyDataSourceName'
puName = 'MyPuName'
}
processResources {
filter(ReplaceTokens, tokens: [
datasourceName: dsName,
persistenceUnitName: puName
])
}
以下代码显示了(简化的)src/main/resources/META-INF/persistence.xml文件:
步骤3:编织实体类
为了增强类,有必要适当地配置构建。将以下剪接添加到build.gradle。声明了一个单独的类路径配置,因为在普通编译类路径中不需要EclipseLink类
configurations {
providedApi
}
dependencies {
providedApi 'org.eclipse.persistence:org.eclipse.persistence.jpa:2.7.2'
providedApi 'org.eclipse.persistence:javax.persistence:2.2.0'
}
task performJPAWeaving(type: JavaExec) {
main "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
args "-loglevel",
"FINE",
compileJava.destinationDir.absolutePath,
compileJava.destinationDir.absolutePath
classpath (configurations.compile, configurations.providedApi)
dependsOn compileJava
dependsOn copyResourcesToClassesOutputDir
}
// Do always weave the classes before a JAR is created
tasks.withType(Jar) {
dependsOn "performJPAWeaving"
}
就这样!如果您现在运行任何Jar任务,您的实体类将在打包到Jar文件之前得到增强。'Compile'在较新版本的Gradle中不可用。现在名为“JavaCompile”。@Terence,
execute
的用法是<建议改为使用code>Project.javaexec。@smoothreggae-是的,现在看起来确实如此!不久前,我们实际上不再调用.execute(),而是将其作为jar任务依赖项。我将更新答案以显示这一点。这应该使用classpath=configurations.compileClasspath
才能工作;此外,似乎源目录和目标目录都是必需的。检查StaticWeave
类的类文档。@cheppsn看起来compileClasspath
确实比compile
()更正确。虽然它们通常是相同的,compileClasspath
无疑是一种方法。而且,我认为你错过了例子中的最后一个论点,因为它不像其他论点那样被引用。该示例始终包含目标,如果没有它就无法工作:-)。
import org.apache.tools.ant.filters.ReplaceTokens
ext {
dsName = 'MyDataSourceName'
puName = 'MyPuName'
}
processResources {
filter(ReplaceTokens, tokens: [
datasourceName: dsName,
persistenceUnitName: puName
])
}
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="@datasourceName@" transaction-type="JTA">
<jta-data source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=@datasourceName@)</jta-data-source>
<!-- Necessary to let EclipseLink/Weaver discover local classes without listing them in this file,
see http://www.eclipse.org/eclipselink/documentation/2.7/concepts/app_dev001.htm#BGBHFFAG-->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- Tell the application container that our classes are already woven,
see https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving-->
<property name="eclipselink.weaving" value="static" />
</properties>
</persistence-unit>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MyPuName" transaction-type="JTA">
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=MyDataSourceName)</jta-data-source>
<!-- Necessary to let EclipseLink/Weaver discover local classes without listing them in this file,
see http://www.eclipse.org/eclipselink/documentation/2.7/concepts/app_dev001.htm#BGBHFFAG -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- Tell the application container that our classes are already woven,
see, https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving-->
<property name="eclipselink.weaving" value="static" />
</properties>
</persistence-unit>
task copyResourcesToClassesOutputDir(type: Copy, dependsOn: processResources) {
from processResources.destinationDir
into compileJava.destinationDir
}
configurations {
providedApi
}
dependencies {
providedApi 'org.eclipse.persistence:org.eclipse.persistence.jpa:2.7.2'
providedApi 'org.eclipse.persistence:javax.persistence:2.2.0'
}
task performJPAWeaving(type: JavaExec) {
main "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
args "-loglevel",
"FINE",
compileJava.destinationDir.absolutePath,
compileJava.destinationDir.absolutePath
classpath (configurations.compile, configurations.providedApi)
dependsOn compileJava
dependsOn copyResourcesToClassesOutputDir
}
// Do always weave the classes before a JAR is created
tasks.withType(Jar) {
dependsOn "performJPAWeaving"
}