Gradle sourceSets 基于 gradle.settings

Gradle sourceSets based on gradle.settings

我希望根据项目 settings.gradle 中定义的自定义设置有条件地构建我的 Java 库的某些功能。

示例:

build.gradle

apply plugin: 'java'

sourceCompatibility = 1.7

repositories {
    mavenCentral()
}

sourceSets {
    extra_feature
}

dependencies {
    compile 'net.java.dev.jna:jna:4.1.0'
}

// I need a way to link the "extra_feature" sourceSets to the default "build" action based on some settings in settings.gradle.

settings.gradle

extraFeatures = true

首先,将您的参数移动到 gradle.properties

其次,使用简单的if来控制源集:

sourceSets (
   if ("true" == "$extraFeatures") {

   }
)

示例(我没有测试):

sourceSets {
    main {
        java {
            srcDir 'src/java'
            if ("true" == "$extraFeatures") {
                srcDir 'src/java/mysecretcode'
            }
        }
    }
}