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

时间:2016-02-18 09:57:08

标签: 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

2 个答案:

答案 0 :(得分: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)
    }
}

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

答案 1 :(得分:-1)

我们为我们的变体系统使用Gradle多模块项目。有一个包含公共代码的核心项目。自定义只需在子项目中完成。

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

变体子项目依赖于核心,每个构建单独的.war文件。请注意,在这种情况下,我们不会覆盖核心项目中的类。对于代码自定义,我们使用Spring,在某些情况下使用SPI,但我想任何依赖注入框架都可以实现。它只是迫使你在核心中提供明确的扩展点,我认为这是一件好事。

相关问题