2015-09-07 24 views
1

我希望根據項目的settings.gradle中定義的自定義設置來構建基於條件構建的Java庫的一些功能。基於gradle.settings的Gradle源集

例子:

的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 
+0

,目前還不清楚你是什麼意思的鏈接的「extra_feature」。 –

+0

這個sourceSet包含一些Java文件(在src/extra_feature/java下),我只想在某些設置爲true的情況下編譯這些文件。 –

回答

2

首先,將你的參數gradle.properties

其次,使用簡單的if控制源集:

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

    } 
) 

實例(我沒有測試它):

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

沒有辦法直接評估屬性,而不是將其內插到字符串中嗎? – sandris

+1

無論如何,我們必須將字符串轉換爲布爾值。可以更好地使用'if(Boolean.valueOf(「$ extraFeatures」))' –

+0

如果設置被設置,我該如何爲「構建」任務自動啓用此sourceSet? –