Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Flavours for Java project

Tags:

java

gradle

Build Flavours are commonly used in Android applications through the android gradle plugin. This allows a project to have a directory structure

src
  - main
    - com.stack.A.java
  - debug
    - com.stack.B.java
  - release
    - com.stack.B.java

This will only compile the correct B.java depending on the release type that has been selected.

Is there a way to mirror this functionality without using the android gradle plugin and just using the java plugin?

like image 691
Paul Thompson Avatar asked Jun 15 '15 23:06

Paul Thompson


2 Answers

It is called sourceSet in Java plugin, see https://docs.gradle.org/current/userguide/java_plugin.html

main and test sourceSets are created automatically, to add more sourceSets you can do something like below. Basically both releases and debug also uses codes in main

apply plugin: 'java'

sourceSets {
    release {
        java {
            srcDirs 'src/main/java', 'src/release/java'
        }
    }

    debug {
        java {
            srcDirs 'src/main/java', 'src/debug/java'
        }
    }
}
like image 198
n4h0y Avatar answered Oct 16 '22 12:10

n4h0y


I've written a plugin to achieve this. Eg

apply plugin: 'com.lazan.javaflavours'
javaFlavours {
    flavours = ['free', 'paid']
}

Then sources in src/free/java and src/paid/java and resources in src/free/resources and src/paid/resources

Project on github: https://github.com/uklance/gradle-java-flavours

like image 35
lance-java Avatar answered Oct 16 '22 14:10

lance-java