【问题标题】:Generating JPA2 Metamodel from a Gradle build script从 Gradle 构建脚本生成 JPA2 元模型
【发布时间】:2011-09-19 20:04:00
【问题描述】:

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

following page of Querydsl's reference documentation 上,他们解释了如何为 Maven 和 Ant 设置 JPAAnnotationProcessor (apt)。

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

【问题讨论】:

    标签: gradle apt querydsl


    【解决方案1】:

    Querydsl Ant 示例的工作原理应该与取出所有 XML 时一样。所以它最终是这样的:

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

    srccpgenerated 你可能可以从 Gradle 中提取。

    【讨论】:

      【解决方案2】:

      我没有对其进行测试,但这应该可以:

      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 的依赖吗?如果可能的话,我想避免这种情况
      • Ant 是 Gradle 的构建方式。它在 Gradle 核心中,因此不需要依赖项。很高兴知道它有效。
      • 很高兴知道,这会在我发表评论之前教我 RTFM ;-) 再次感谢。
      • gradle 是 ant,但在 groovy 中而不是 xml。
      【解决方案3】:

      虽然我对 Ant 的使用 gradle 没有任何问题,但我同意原始海报在这种情况下是不可取的。我发现 Tom Anderson here 的一个 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
      

      这种方法对我来说比另一种更有意义,如果它也对你有用,那么你有另一种查询 dsl 生成的选择。

      【讨论】:

      • 谢谢Joeg:我喜欢将源代码生成步骤和代码编译步骤分开的方法:)
      • 谢谢!这为 m 完成了工作,但我不得不将 type:Compile 替换为 type:JavaCompile
      【解决方案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

      【讨论】:

      • 嗨帕维尔!我尝试了您为我的实体(用 groovy 编写)建议的解决方案,但它不起作用。但是,如果除了实体之外还有其他东西在常规中,它会起作用。我发布了一个solution as comment to a similar question 用于您希望用 Groovy 编写的 @Entity 类在这里被 querydsl 生成器拾取的情况
      【解决方案5】:

      这家伙的要点对我有用: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。
      【解决方案6】:

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

      【讨论】:

      • 太棒了!这就是我要找的东西!
      【解决方案7】:

      要在 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 args的意义,你为什么要添加它们?
      猜你喜欢
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 2018-02-18
      • 2014-07-31
      相关资源
      最近更新 更多