How to enable static Eclipselink weaving from Gradle

I would like to enable Eclipselink static weaving for my JPA classes from Gradle. The Eclipselink docs explain how to do this in the Ant task:

<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>

Now I have 2 questions:

1. How to "translate" this into a Gradle approach? I tried this (based on docs at http://www.gradle.org/docs/current/userguide/ant.html#N1143F ):

task eclipseWeave << {
    ant.taskdef(name: "weave",
                classname: "org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask",
                classpath: configurations.compile.asPath)

    ant.weave(source: relativePath(compileJava.destinationDir),
              target: relativePath(compileJava.destinationDir),
              persistenceinfo: relativePath(processResources.destinationDir) {
    }
}

but the problem is that the class path does not work in ant.weave(..). The weaving process is interrupted after a message with the message:

Execution failed for task ':eclipseWeave'.
> java.lang.NoClassDefFoundError: some/class/from/my/dependencies

The classpath parameter works for ant.taskdef(..)since StaticWeaveAntTask is found in my dependencies. How can I apply it to ant.weave(..)myself?

2. , compileJava?

+3
2

, , , "" , . JavaExec .

( 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.compile
}

tasks.withType(Jar){
  dependsOn "performJPAWeaving"
}
+8

, , . :

  • persistence.xml .
  • EclipseLink Weaver, Weavers -persistenceunit Gradle.
  • entitiy , persistence.xml.

1: persistence.xml

-, placeholder @datasourceName @ . build.gradle:

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:

<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>

, exclude-unlisted-classes false, EclipseLink Weaver . , " eclipselink.weaving " " static ", , . OSGi, EclipseLink , .

processResources /resources/main/META-INF/persistence.xml ( ):

<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>

2. persistence.xml

, compileJava. , Weavers ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=295031). build.gradle:

task copyResourcesToClassesOutputDir(type: Copy, dependsOn: processResources) {
    from processResources.destinationDir
    into compileJava.destinationDir
}

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.

+1

Source: https://habr.com/ru/post/1535992/


All Articles