从Gradle构建脚本生成JPA2元模型

21

我正在尝试为一个新项目设置Gradle构建脚本。该项目将使用JPA 2和Querydsl

Querydsl的参考文档的下一页中,他们解释了如何为Maven和Ant设置他们的JPAAnnotationProcessor(apt)。

我想用Gradle做同样的事情,但是我不知道如何做,我的好朋友也没有帮我太多。我需要找到一种方法来调用Javac(最好不使用任何额外的依赖项)并使用参数来指定apt应该使用的处理器(?)

7个回答

16

虽然我对Gradle使用Ant没有问题,但我同意原帖作者在这种情况下它是不可取的。我发现Tom Anderson的GitHub项目(此处)描述了我认为更好的方法。我对其进行了小幅修改以适应我的需求(输出到src/main/generated),因此它看起来像:

sourceSets {
     generated
}

sourceSets.generated.java.srcDirs = ['src/main/generated']

configurations {
     querydslapt
}

dependencies {     
    compile 'mine go here'
    querydslapt 'com.mysema.querydsl:querydsl-apt:2.7.1'
}

task generateQueryDSL(type: Compile, group: 'build', description: 'Generates the QueryDSL query types') {
         source = sourceSets.main.java
         classpath = configurations.compile + configurations.querydslapt
         options.compilerArgs = [
                "-proc:only",
                "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
         ]
         destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}
compileJava.dependsOn generateQueryDSL

如果这种方法对我来说比其他方法更有意义,如果你也是这样认为的话,那么你就有了另一种用于QueryDSL生成的选项。


谢谢Joeg:我喜欢将源代码生成步骤和代码编译步骤分开的方法 :) - pbanfi
2
谢谢!这对我有用,但是我必须将“_type:Compile_”替换为“_type:JavaCompile_”。 - Iuliana Cosmina

12

我没有测试过,但是这个应该可以工作:

repositories {
    mavenCentral()
}
apply plugin: 'java'
dependencies {
   compile(group: 'com.mysema.querydsl', name: 'querydsl-apt', version: '1.8.4')
   compile(group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '1.8.4')
   compile(group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1')
}

compileJava {
    doFirst {
        Map otherArgs = [
            includeAntRuntime: false,
            destdir: destinationDir,
            classpath: configurations.compile.asPath,
            sourcepath: '',
            target: targetCompatibility,
            source: sourceCompatibility
        ]
        options.compilerArgs = [
            '-processor', 'com.mysema.query.apt.jpa.JPAAnnotationProcessor',
            '-s', "${destinationDir.absolutePath}".toString()
        ]
        Map antOptions = otherArgs + options.optionMap()
        ant.javac(antOptions) {
            source.addToAntBuilder(ant, 'src', FileCollection.AntType.MatchingTask)
            options.compilerArgs.each {value ->
                compilerarg(value: value)
            }
        }
    }
}

希望能对你有所帮助。


你的解决方案是否在构建中添加了对ant的依赖?如果可能的话,我想避免这种情况。 - dSebastien
1
Ant 是 Gradle 构建的方式之一。它已经包含在 Gradle 核心中,因此不需要任何依赖。很高兴知道它能够正常工作。 - Fred Simon
好的,这会教会我在评论之前先阅读文档。;-) 再次感谢。 - dSebastien
Gradle是一种基于Groovy而非XML的Ant构建工具。 - MariuszS

9

这位的要点对我非常有用: https://gist.github.com/EdwardBeckett/5377401

sourceSets {
    generated {
        java {
            srcDirs = ['src/main/generated']
        }
    }
}

configurations {
    querydslapt
}

dependencies {
    compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final'
    compile "com.mysema.querydsl:querydsl-jpa:$querydslVersion"
    querydslapt "com.mysema.querydsl:querydsl-apt:$querydslVersion"
}

task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
    source = sourceSets.main.java
    classpath = configurations.compile + configurations.querydslapt
    options.compilerArgs = [
            "-proc:only",
            "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
    ]
    destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}

compileJava {
    dependsOn generateQueryDSL
    source generateQueryDSL.destinationDir
}

compileGeneratedJava {
    dependsOn generateQueryDSL
    options.warnings = false
    classpath += sourceSets.main.runtimeClasspath
}

clean {
    delete sourceSets.generated.java.srcDirs
}

idea {
    module {
        sourceDirs += file('src/main/generated')
    }
}

我正在使用Gradle 1.10,这个方法非常有效!感谢@Ryan。 - Yohan Liyanage

4

从Gradle 1.3版本开始(旧版未测试),您可以像这样使用Querydsl APT:

configurations {
  javacApt
}
dependencies {
  javacApt 'com.mysema.querydsl:querydsl-apt:3.3.0'
}
compileJava {
  options.compilerArgs <<
    '-processorpath' << (configurations.compile + configurations.javacApt).asPath <<
    '-processor' << 'com.mysema.query.apt.jpa.JPAAnnotationProcessor'
}

这些编译器参数直接传递给javac。
要与groovy编译器一起使用,请将compileJava替换为compileGroovy

2
嗨,Pavel!我尝试了你建议的解决方案,但对于我的实体(编写在Groovy中),它不起作用。但是,如果除了实体之外的任何内容都是Groovy编写的,它将起作用。我在这里发布了一个类似问题的评论解决方案,适用于希望使用Groovy编写的@Entity类被querydsl生成器捕获的情况。 - Kostas Filios

4
这是一个简单的设置,能够与Netbeans无缝集成。Javac基本上可以在没有太多干预的情况下完成所有需要的工作。其余部分只需要进行一些小的调整,就可以让它与像Netbeans这样的IDE配合使用。
apply plugin:'java'

dependencies {
    // Compile-time dependencies should contain annotation processors
    compile(group: 'com.mysema.querydsl', name: 'querydsl-apt', version: '1.8.4')
    compile(group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '1.8.4')
    compile(group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1')
}

ext {
    generatedSourcesDir = file("${buildDir}/generated-sources/javac/main/java")
}

// This section is the key to IDE integration.
// IDE will look for source files in both in both
//
//  * src/main/java
//  * build/generated-sources/javac/main/java
//
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir generatedSourcesDir
        }
    }
}

// These are the only modifications to build process that are required.
compileJava {
    doFirst {
        // Directory should exists before compilation started.
        generatedSourcesDir.mkdirs()
    }
    options.compilerArgs += ['-s', generatedSourcesDir]
}

就是这样。Javac会完成其余的工作。


太棒了!这正是我在寻找的! - MariuszS

2

要使用Gradle的JPA元模型生成器,我在我的build.gradle中成功地使用了以下内容,并且它非常好用:

buildscript {
    ext {}
    repositories { // maven central & plugins.gradle.org/m2 }
    dependencies {
        // other dependencies, e.g. Spring
        classpath('gradle.plugin.at.comm_unity.gradle.plugins:jpamodelgen-plugin:1.1.1')
    }

    apply plugin: 'at.comm_unity.gradle.plugins.jpamodelgen'

    dependencies {
        compile('org.hibernate:hibernate-jpamodelgen:5.1.0.Final')
    }

    jpaModelgen {
        jpaModelgenSourcesDir = "src/main/java"
    }

    compileJava.options.compilerArgs += ["-proc:none"]
}

在构建任务中,以“_”为后缀的静态元模型类被生成。之后它们被定位在与我的@Entity模型相同的目录中。


我不建议将生成的模型添加到主要源代码中。我更愿意将它们添加到单独的源代码目录,例如src/generated/java。然后需要将此路径添加到主Java SourceSet中。我不知道编译依赖和compileJava参数的意义,你为什么要添加它们呢? - Alex

1

当你将所有的XML都删除后,Querydsl Ant示例应该基本上可以正常工作。因此,它最终会变成类似于这样:

javac -sourcepath ${src} -cp ${cp} -proc:only -processor com.mysema.query.apt.jpa.JPAAnnotationProcessor -s ${generated}

srccpgenerated这三个变量你可能可以从Gradle中提取出来。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接