The persistence.xml
is required at compile time. One way to achieve this is:
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
compileJava.dependsOn(processResources)
Pergunta
I am building my JPA
/EJB
project using gradle
and getting this warning:
$ gradle jar
:compileJava
Note: Creating static metadata factory ...
Note: The persistence xml file [META-INF/persistence.xml] was not found. NO GENERATION will occur!! Please ensure a persistence xml file is available either from the CLASS_OUTPUT directory [META-INF/persistence.xml] or using the eclipselink.persistencexml property to specify its location.
2 warnings
:processResources UP-TO-DATE
:classes
:jar
My project structure:
ProjectX
|-src
|---main
|-----java
|-------domain
|-------ejb
|-----resources
|-------META-INF
|-----------persistence.xml
The interesting part is that the persistence.xml ends up in the target jar artifact:
$ jar -tf target/projectx.jar
META-INF/
META-INF/MANIFEST.MF
domain/
domain/Employee.class
ejb/
ejb/EmployeeEJB.class
META-INF/persistence.xml
Why is there complain that the [META-INF/persistence.xml]
even though it is there?
The gradle file:
apply plugin: 'java'
project.buildDir = 'target'
sourceSets.all {
output.resourcesDir = output.classesDir
}
jar{
destinationDir=project.buildDir
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.glassfish:javax.ejb:3.0.1','org.eclipse.persistence:javax.persistence:2.0.0'
}
I have quickly tested with maven now and ther is no such warning there.
Solução
The persistence.xml
is required at compile time. One way to achieve this is:
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
compileJava.dependsOn(processResources)
Outras dicas
In fact the solution suggested by @peter-niederwieser to set your main resources and classes dir to same location results in duplicated entries in your jar archive. Basically, it's twice the original size, don't know if there are other problems.
So to fix this, you have to add
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
jar {
duplicatesStrategy = 'exclude'
}
to your build.gradle
.
META-INF should be in the root of your classpath (/src/main/java/META-INF)
The persistence.xml should be in src/main/resources/META-INF. This worked for me. Please not that this was a non spring project