如何在Gradle中编译之前移动生成的查询类型类?

时间:2016-07-09 10:43:22

标签: java eclipse gradle spring-data-jpa querydsl

我已经设法使用Gradle生成查询类型类(.java),但是默认情况下它们会随着编译类一起被移动到build / classes / main。我如何将它们移动到src / main / java,以便我可以在编译时引用它们?

这是我的Gradle构建脚本:

 buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
    }
}

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-serving-web-content'
    version =  '0.1.0'
}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'
    compile 'org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.3.6.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.6.RELEASE'
    compile 'mysql:mysql-connector-java:6.0.3'
    compile 'com.querydsl:querydsl-jpa:4.1.3'
    compile 'com.querydsl:querydsl-apt:4.1.3:jpa'


    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}



task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

修改

根据我的评论 - 我试图将生成的类移动到目录src / generated / java,然后将该位置添加到源目录,以便可以编译它们。我已尝试过以下操作,但它不会创建目录或任何文件:

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

1 个答案:

答案 0 :(得分:1)

This is the part you are missing:

compileJava {
    options.compilerArgs << "-s"
    options.compilerArgs << "$projectDir/generated/java" 

    doFirst {
        // make sure that directory exists
        file(new File(projectDir, "/generated/java")).mkdirs()
    }   
}

clean.doLast {
    // clean-up directory when necessary
    file(new File(projectDir, "/generated")).deleteDir()
}