tomz
tomz

Reputation: 341

How to move generated query type classes before compiling in Gradle?

I've managed to generate query type classes (.java) using Gradle, however they're being moved to build/classes/main along with compiled classes by default. How would I move them to src/main/java so I can reference them at compile time?

Here's my Gradle build script:

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

Edit

As per my comment - I'm trying to move generated classes to directory src/generated/java and then add that location to the source directories so they can get compiled. I've tried the following, but it doesn't create directory nor any files:

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

Upvotes: 1

Views: 562

Answers (1)

Flor
Flor

Reputation: 11

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

Upvotes: 1

Related Questions