在intellij java应用程序中构建变体(产品风格)

use*_*083 9 java intellij-idea gradle build.gradle

是否有可能在intellij中为传统的Java应用程序(不是android项目)构建基于不同源集的变体?

我想使用像android gradle插件附带的productFlavors这样的功能,但是对于传统的java应用程序.

例:

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
Run Code Online (Sandbox Code Playgroud)

Mic*_*bbs 0

答案是肯定的,但您将必须使用正在孵化的新 Gradle 软件模型。这将是一条充满痛苦的道路,因为您将成为开拓者,因为我已经学会在 C/Cpp 项目中使用它。您的构建通常如下所示。

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

参考: https: //docs.gradle.org/current/userguide/java_software.html