3

私は次のようにレイアウトされているプロジェクトを持っています:

src/
  java
  generated

src / javaには、休止状態のメタモデルアノテーションプロセッサによって生成されるjpaメタモデルクラスを使用するjpaエンティティとクエリクラスが含まれています。

アノテーション処理をJavaプラグインに組み込むための最良の方法は何ですか?

現在、次のタスクを定義していますが、compileJavaにタスクの依存関係があり、一部のコードはアノテーションプロセッサによって生成されたクラスに依存しているため失敗します。

task processAnnotations(type: Compile) {
    genDir = new File("${projectDir}/src/generated")
    genDir.mkdirs()
    source = ['src/java']
    classpath = sourceSets.test.compileClasspath
    destinationDir = genDir
    options.compilerArgs = ["-proc:only"]
}
4

3 に答える 3

6

これは、NetBeansとシームレスに連携して機能する簡単なセットアップです。Javacは基本的に、多くの介入なしに必要なすべての作業を実行します。残りは、NetbeansなどのIDEで動作するようにする小さなトレッキングです。

apply plugin:'java'

dependencies {
    // Compile-time dependencies should contain annotation processors
    compile(group: 'org.hibernate...', name: '...', version: '...')
}

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が残りの仕事をします。

于 2014-04-22T11:32:29.057 に答える
3

The reason why processAnnotations depends on compileJava is that you put the test compile class path on the former task's compile class path, and the test compile class path contains the compiled production code (i.e. output of compileJava).

As to how to best solve the problem at hand, you shouldn't need a separate compile task. The Java compiler can invoke annotation processors and compile their generated sources (along with the original sources) in one pass (see Annotation Processing). One thing you'll need to do is to put the annotation processor on the compile class path:

configurations {
    hibernateAnnotationProcessor
}

dependencies {
    hibernateAnnotationProcessor "org.hibernate: ..."
}

compileJava.compileClasspath += configurations.hibernateAnnotationProcessor

(You don't want to add the annotation processor to the compile configuration because then it will be considered a dependency of the production code.)

From what I can tell, this is all there is to it (assuming you are using JDK6 or higher).

于 2012-07-08T20:38:52.697 に答える
1

gradle 4.6以降では、適切な構成でプロセッサの依存関係を定義する必要があります。

dependencies {
    ...

    annotationProcessor 'org.hibernate:hibernate-jpamodelgen:4.3.7.Final'
}

参照:https ://docs.gradle.org/4.6/release-notes.html#added-annotationprocessor-configurations

于 2018-12-14T17:14:47.547 に答える