在gradle中将类路径添加到ant任务的最佳方法

时间:2014-05-20 01:10:50

标签: java ant groovy gradle

我尝试使用使用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:\Users\me\IdeaProjects\myproject\subprojects\core\src\main\java\com\example\myproject\core\model\User.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:\Users\me\IdeaProjects\myproject\subprojects\core\src\main\java\com\example\myproject\core\model\User.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:\Users\me\IdeaProjects\myproject\subprojects\core\src\main\java\com\example\myproject\core\model\User.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:\Users\me\IdeaProjects\myproject\subprojects\core\src\main\java\com\example\myproject\core\model\User.java:161: error: cannot find symbol
[ant:javac]     @Column(name = BLUE_COLUMN)
[ant:javac]      ^
[ant:javac]   symbol:
----- End of the daemon log -----

在阅读了一些论坛后,我决定尝试在FileCollection类上添加带有addToAntBuilder方法的类路径:

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

以下是我使用彼得建议的第四次失败尝试:

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"]    
}

我仍然有相同的输出

1 个答案:

答案 0 :(得分:1)

事实证明compile配置不包含所提供配置的依赖关系。当我考虑它时,这是有道理的,因为我不希望提供的罐子在我的子项目jar / war中结束,如果它们将在运行时由容器提供。

我的provideConfiguration.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
}

因此,我应该添加提供的配置,而不是将编译配置添加到JavaCompile任务的类路径中:

task generateJpaMetaModel(type: JavaCompile) {
    ...
    classpath = configurations.provided + buildscript.configurations.classpath
    ...    
}