我正在尝试使用使用antbuilder的自定义gradle任务预生成jpa 2元模型类。
到目前为止我的代码是这样的:task generateJpaMetaModel {
doFirst {
sourceSets.generated.output.classesDir.mkdirs()
ant.javac (
srcDir: "$projectDir/src/main/java",
destDir: sourceSets.generated.output.classesDir,
failonerror: true,
fork: true
) {
classpath {
pathElement(location: buildscript.configurations.classpath.asPath)
pathElement(location: configurations.compile.asPath)
}
compilerarg (value: "-proc:only")
}
}
inputs.files "$projectDir/src/main/java"
outputs.files sourceSets.generated.output.classesDir
}
compileGeneratedJava.dependsOn generateJpaMetaModel
我有一个自定义的generated
sourceSet添加到我的项目,应用Java插件。
在我的依赖,我有各种JPA库:
dependencies {
...
provided "javax.servlet:javax.servlet-api:$libraryVersions.servlet_api"
provided "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:$libraryVersions.hibernate_jpa_2"
}
在我的构建脚本中,我有一个依赖于元模型生成jar:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.hibernate:hibernate-jpamodelgen:$libraryVersions.hibernate_jpamodelgen"
}
}
运行generateJpaMetaModel任务给了我以下输出,即使我认为我正确添加了类路径:
[ant:javac] C:UsersmeIdeaProjectsmyprojectsubprojectscoresrcmainjavacomexamplemyprojectcoremodelUser.java:152: error: cannot find symbol
[ant:javac] @Column(name = ONE_COLUMN)
[ant:javac] ^
[ant:javac] symbol: class Column
[ant:javac] location: class User
[ant:javac] C:UsersmeIdeaProjectsmyprojectsubprojectscoresrcmainjavacomexamplemyprojectcoremodelUser.java:155: error: cannot find symbol
[ant:javac] @Column(name = TWO_COLUMN)
[ant:javac] ^
[ant:javac] symbol: class Column
[ant:javac] location: class User
[ant:javac] C:UsersmeIdeaProjectsmyprojectsubprojectscoresrcmainjavacomexamplemyprojectcoremodelUser.java:158: error: cannot find symbol
[ant:javac] @Column(name = RED_COLUMN)
[ant:javac] ^
[ant:javac] symbol: class Column
[ant:javac] location: class User
[ant:javac] C:UsersmeIdeaProjectsmyprojectsubprojectscoresrcmainjavacomexamplemyprojectcoremodelUser.java:161: error: cannot find symbol
[ant:javac] @Column(name = BLUE_COLUMN)
[ant:javac] ^
[ant:javac] symbol:
----- End of the daemon log -----
阅读一些论坛后,我决定尝试添加类路径与addToAntBuilder方法FileCollection类:
(configurations.compile + buildscript.configurations.classpath)
.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)
ant.javac (
srcDir: "$projectDir/src/main/java",
destDir: sourceSets.generated.output.classesDir,
failonerror: true,
fork: true,
classpath: 'fileset'
) {
compilerarg (value: "-proc:only")
}
然而,它仍然给我相同的输出。
谁能指出给ant任务添加类路径的最好方法是什么?
更新:
这是我第三次尝试相同的输出:
sourceSets.generated.output.classesDir.mkdirs()
ant.javac (
srcDir: "$projectDir/src/main/java",
destDir: sourceSets.generated.output.classesDir,
failonerror: true,
includeantruntime: true
) {
(configurations.compile + buildscript.configurations.classpath)
.addToAntBuilder(ant, 'classpath', FileCollection.AntType.ResourceCollection)
compilerarg (value: "-proc:only")
}
更新2:这是我使用peter的建议的第四次失败的尝试:
task generateJpaMetaModel(type: JavaCompile) {
source = fileTree("$projectDir/src/main/java")
destinationDir = sourceSets.generated.output.classesDir
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
classpath = configurations.compile + buildscript.configurations.classpath
options.compilerArgs = ["-proc:only"]
}
我仍然有相同的输出
结果显示compile
配置不包含来自所提供配置的依赖项。当我想到它时,这是有意义的,因为我不希望提供的jar在我的子项目jar/war中结束,如果它们将在运行时由容器提供。
。Gradle插件文件包含以下内容:
/**
* Adds a configuration named 'provided'. 'Provided' dependencies
* are incoming compile dependencies that aren't outgoing
* dependencies. In other words, they have no effect on transitive
* dependency management.
*/
configurations {
provided.extendsFrom(compile)
testCompile.extendsFrom(provided)
}
sourceSets.main {
compileClasspath += configurations.provided
}
plugins.withType(IdeaPlugin) {
idea.module.scopes.PROVIDED.plus += configurations.provided
}
因此,我应该添加提供的配置,而不是将编译配置添加到javaccompile任务的类路径中:
task generateJpaMetaModel(type: JavaCompile) {
...
classpath = configurations.provided + buildscript.configurations.classpath
...
}