Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change output directory of generated code in gradle

Project contains annotation processor which generates java code during compilation. By default, gradle outputs generated source files into build/classes directory. That causes some problems with discovery of newly generated source files by IntelliJ.

Is there any straightforward way of configuring gradle to output source files into another directory? For example $buildDir/gen/main/java or $buildDir/build/generated/main/java?

like image 816
Araz Abishov Avatar asked May 29 '16 17:05

Araz Abishov


People also ask

How do I change directory in Gradle?

There is no way at the moment to force the 'working dir' of gradle process. Normally, we strongly recommend that your build. gradle should not depend on the working dir. Instead of new File(projectDir, “foo.

Where is the output of Gradle build?

Project contains annotation processor which generates java code during compilation. By default, gradle outputs generated source files into build/classes directory.

Where does Gradle output jars?

The Jar is created under the $project/build/libs/ folder.

Where does Gradle output APK?

After you build the project, the output APK for the app module is located in app/build/outputs/apk/ , and the output AAR for any lib modules is located in lib/build/outputs/libs/ . Each time you change a source file or resource, you must run Gradle again in order to package up the latest version of the application.


2 Answers

There is an option for java compiler which allows to customize output directory for generated java sources (documentation).

-s dir

Specify the directory where to place generated source files. The directory must already exist; javac will not create it. If a class is part of a package, the compiler puts the source file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -s C:\mysrc and the class is called com.mypackage.MyClass, then the source file will be placed in C:\mysrc\com\mypackage\MyClass.java.

Example of build.gradle

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

sourceSets {
    generated {
        java {
            srcDir "$projectDir/generated/java"
        }
    }
}

This code snippet does next:

  • creates and specifies directory as output for generated code
  • deletes generated sources if clean task is invoked
  • adds new source set

Update

Use gradle apt plugin instead.

like image 136
Araz Abishov Avatar answered Sep 19 '22 14:09

Araz Abishov


Simply specify value for project.buildDir property in your build.gradle file:

project.buildDir = '/gen/main/java'

This will put all generated build files to the <project_root>/gen/main/java folder.

like image 23
Ivan Pronin Avatar answered Sep 19 '22 14:09

Ivan Pronin