Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build variants (product flavours) in IntelliJ Java application

Is it possible to have build variants based on different source sets for a traditional Java app (NOT an Android project) in IntelliJ?

I'd like to use a feature like productFlavors that comes with the Android gradle plugin, but for a traditional Java application.

Example:

library_red -- HelloImpl.java
library_blue -- HelloImpl.java
library_common -- Hello.java

compiled library_blue -- Hello.class, HelloImpl.class
compiled library_red -- Hello.class, HelloImpl.class
like image 597
user3904083 Avatar asked Nov 17 '25 11:11

user3904083


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

Reference: https://docs.gradle.org/current/userguide/java_software.html

like image 129
Michael Hobbs Avatar answered Nov 20 '25 02:11

Michael Hobbs


We use Gradle multi-module projects for our variant system. There is a core project that contains the common code. Customizations are simply done in subprojects.

subprojects {
   dependencies {
     compile project(':core')
   }
}

The variant subprojects depend on the core and each build individual .war files. Note that in this case we don't override classes from the core project. For code customizations we use Spring and in some cases SPI, but I guess any dependency injection framework can accomplish that. It just forces you to provide explicit extension points in the core which I think is a good thing.

like image 24
tobiasH Avatar answered Nov 20 '25 02:11

tobiasH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!