Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle product flavor in "pure" gradle (not android gradle)

Tags:

java

gradle

I want to build a java library for different customers with gradle. Is there something like product flavors known from android in "pure" gradle?

Thanks.

like image 294
sbo Avatar asked Feb 23 '16 12:02

sbo


1 Answers

The answer is yes but you will have to use the new Gradle software model which is very much incubating. It will be a road full of pain as you will be a trail blazer as I have learned using it for a C/Cpp project. Here is generally how your build will look like.

plugins {
    id 'jvm-component'
    id 'java-lang'
}

model {
  buildTypes {
    debug
    release
  }
  flavors {
    free
    paid
  }
    components {
        server(JvmLibrarySpec) {
            sources {
                java {
                  if (flavor == flavors.paid) {
                    // do something to your sources
                  }
                  if (builtType == buildTypes.debug) {
                    // do something for debuging
                  }
                    dependencies {
                        library 'core'
                    }
                }
            }
        }

        core(JvmLibrarySpec) {
            dependencies {
                library 'commons'
            }
        }

        commons(JvmLibrarySpec) {
            api {
                dependencies {
                    library 'collections'
                }
            }
        }

        collections(JvmLibrarySpec)
    }
}

References: 1) Java Software Model https://docs.gradle.org/current/userguide/java_software.html 2) Flavors https://docs.gradle.org/current/userguide/native_software.html note: I'm not sure how well flavors are supported the Java Software Model, I will do some testing and report back.

Update: It is doable but is not currently supported by the JvmLibrarySpec. I will try to post a more complete answer with an example of how to do the custom spec.

like image 86
Michael Hobbs Avatar answered Oct 03 '22 22:10

Michael Hobbs