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

发布于 2024-11-16 13:00:15 字数 418 浏览 3 评论 0原文

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

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

我想对 Gradle 做同样的事情,但我不知道如何做,而且我亲爱的朋友在这方面没有给我太多帮助。我需要找到一种方法来调用带有参数的 Javac(最好没有任何额外的依赖项),以便能够指定 apt 应该使用的处理器(?)

I'm trying to set up a Gradle build script for a new project. That project will use JPA 2 along with Querydsl.

On the following page of Querydsl's reference documentation, they explain how to set up their JPAAnnotationProcessor (apt) for Maven and Ant.

I would like to do the same with Gradle, but I don't know how and my beloved friend did not help me much on this one. I need to find a way to invoke Javac (preferably without any additional dependencies) with arguments to be able to specify the processor that apt should use (?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ぇ气 2024-11-23 13:00:15

虽然我对使用 Ant 的 gradle 没有任何问题,但我同意原始发布者的观点,即在这种情况下这是不可取的。我在此处找到了 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。

While I have no problem with the use gradle makes of Ant, I agree with the original poster that it is undesirable in this case. I found a github project by Tom Anderson here that describes what I believe is a better approach. I modified it a small amount to fit my needs (output to src/main/generated) so that it looks like:

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

This approach makes a lot more sense to me than the other, if it does to you too, then you have another option for querydsl generation.

耳钉梦 2024-11-23 13:00:15

我没有测试它,但这应该有效:

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

希望它有帮助。

I did not test it but this should work:

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

Hope it helps.

独自唱情﹋歌 2024-11-23 13:00:15

这家伙的要点对我有用: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')
    }
}

This guy's gist worked for me: 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')
    }
}
烟凡古楼 2024-11-23 13:00:15

这是一个简单的设置,可以与 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 将完成剩下的工作。

Here is simple setup that works and integrates seamlessly with netbeans. Javac will basicly do all the job needed without much intervention. The rest are small treaks that will make it work with IDEs like Netbeans.

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

And that's it. Javac will make the rest of the job.

╰◇生如夏花灿烂 2024-11-23 13:00:15

使用 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。

With Gradle 1.3 and newer (older not tested) you can use Querydsl APT like this:

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

These compiler args are passed directly to javac.

To use with groovy compiler replace compileJava with compileGroovy.

忘东忘西忘不掉你 2024-11-23 13:00:15

要在 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 模型相同的目录中。

To use the JPA Metamodel Generator with Gradle I'm successfully using the following in my build.gradle and it works like a charm:

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

Within the build task, the static metamodel classes suffixed with '_' are generated. Afterwards they are located in the same directory as my @Entity models are.

眉黛浅 2024-11-23 13:00:15

当您取出所有 XML 时,Querydsl Ant 示例应该可以正常工作。所以它最终会是这样的:

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

srccpgenerate 你可能能够从 Gradle 中提取。

The Querydsl Ant example should work pretty much as is when you take all the XML out. So it ends up being something like this:

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

src, cp and generated you will probably be able to extract from Gradle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文