简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM