2013-01-31 26 views
0

這似乎與這個問題非常相關(哦,不要誤解我的意思,gradle是炸彈......似乎偶爾會出現一些小問題)。gradle屬性有時不工作

https://stackoverflow.com/questions/14594074/gradle-command-line-ordering-not-working-in-this-case-or-source-dirs-being-ignor

我有一個gradle這個文件,如果我更改以下$ buildDir輸出,而不是它的工作原理(我用$ buildDir遍沒有問題的文件的地方....

sourceSets { 
    main { 
     java { 
      srcDir '$buildDir/generated-src' 
     } 
    } 
} 

我的完整gradle文件在這裏,它的失敗類似於上面的問題,因爲沒有找到生成的源代碼:(。我不確定它是否是相同的問題,這是一個錯誤嗎?

subprojects { 
    apply plugin: 'java' 
    apply plugin: 'eclipse' 

    //override gradle's default output directory(build) on every project as it conflicts with 
    //our build script called build causing failures. 
    buildDir = 'output' 
    project.ext.versionDir = '$buildDir/version' 

    repositories { 
     mavenCentral() 
    } 

    if (project.hasProperty('myVersion')) { 
    project.ext.realVersion = project.myVersion 
    project.version = project.myVersion 
    } else { 
    project.ext.realVersion = 'Developer-Build' 
    project.version = 'Developer-Build' 
    } 

    test { 
     beforeTest { desc -> 
      println "Executing test ${desc.name} [${desc.className}]" 
     } 
    } 

    //generate a version file in all the projects 
    task versionFile() << { 
     File f = new File('$task.project.name/$versionDir'); 
     f.mkdirs() 
     File v = new File(f, 'version'+project.ext.realVersion) 
     println('output version file='+v.getAbsolutePath()) 
     v.createNewFile() 
    } 

    task zip(type: Zip) { 
    } 
    zip.dependsOn('versionFile') 
    zip.dependsOn('jar') 
    assemble.dependsOn('zip') 

    task hello << { task -> println "I'm $task.project.name" } 
    build << { task -> println "MASTER: I'm building now" } //"building with classpath=$sourceSets.main.compileClasspath.files" 
} 

project(':webserver') { 
    //play does not follow maven/gradle standard of src/main/java and src/test/java :(:(
    //so we override the directories here...(we should put test in the sourceSets.test.java.srcDirs instead) 
    sourceSets.main{ 
     java.srcDirs = ['app', 'test'] 
     resources.srcDirs = ['app'] 
    } 

    dependencies { 
     compile fileTree(dir: 'lib', include: '*.jar') 
     compile fileTree(dir: 'play-1.2.4/framework/lib', include: '*.jar') 
     compile fileTree(dir: 'play-1.2.4/framework', include: 'play-*.jar') 
    } 

    task zip(type: Zip, overwrite: true) << { 
     archiveName 'dashboard-'+project.version+'.zip' 
     from($versionDir) { 
      into('webserver') 
     } 
     from('..') { 
      exclude '**/*.pyc' 
      exclude '**/*.class' 
      exclude '**/samples-and-tests/**' 
      exclude '**/play-1.2.4/documentation/**' 
      exclude 'webserver/conf/logback.xml' 
      include 'webserver/run*.sh' 
      include 'webserver/lib/**' 
      include 'webserver/app/**' 
      include 'webserver/conf/**' 
      include 'webserver/play-1.2.4/**' 
      include 'webserver/public/**' 
     } 
     rename 'prod.(.*)', '$1' 
    } 

    //playframework has it's own generation of .classpath and .project fils so do not 
    //overwrite their versions. NEED to call "play.bat eclipsify" here... 
    task eclipse(overwrite: true) << { 
     if (System.properties['os.name'].toLowerCase().contains('windows')) { 
      println "*** WINDOWS " 
      def result = exec { 
       commandLine 'cmd', '/c', 'play-1.2.4\\play.bat eclipsify' 
      } 
     } else { 
      println "*** NOT WINDOWS " 
      def result = exec { 
       commandLine './play-1.2.4/play eclipsify' 
      } 
     } 
    } 
} 

project(':toneserver') { 
    project.ext.genLibDir = file('$buildDir/thirdpartylibs') 

    configurations { 
     all*.exclude module: 'log4j' 
    } 

    dependencies { 
     compile 'com.google.inject:guice:3.0' 
     compile 'com.google.protobuf:protobuf-java:2.4.1' 

     //weird, why is their maven not working(we drop it in the directory instead)... 
     //compile 'org.asteriskjava:asterisk-java:1.0.0.M3' 

     //to be erased as soon as we get the chance...(we should try this NOW and see if it is needed anymore) 
     compile 'commons-configuration:commons-configuration:1.8' 
     compile 'org.bouncycastle:bcpg-jdk16:1.46' 

     compile project(':webserver') 

     //gradle is not sucking in transitive dependencies when they exist in another project so we suck them 
     //in ourselves here... 
     compile fileTree(dir: '../webserver/play-1.2.4/framework/lib', include: '*.jar') 
     compile fileTree(dir: '../webserver/lib', include: '*.jar') 
     compile fileTree(dir: '../webserver/play-1.2.4/framework', include: 'play-*.jar') 

     compile 'org.bouncycastle:bcpg-jdk16:1.46' 

     testCompile 'junit:junit:4.11' 
    } 

    task generateSources { 
     project.ext.outputDir = file("$buildDir/generated-src") 
     outputDir.exists() || outputDir.mkdirs() 
     if (System.properties['os.name'].toLowerCase().contains('windows')) { 
      println "*** WINDOWS " 
      def result = exec { 
       commandLine 'cmd', '/c', '..\\tools\\protoc\\protoc.exe', '--java_out=output\\generated-src', 'src\\schemas\\agentbridge.proto' 
      } 
     } else { 
      throw new RuntimeException("DARN, protoc only works on windows :(:(right now") 
     } 
    } 
    compileJava.dependsOn("generateSources") 
    sourceSets { 
     main { 
      java { 
       srcDir '$buildDir/generated-src' 
      } 
     } 
    } 

    tasks.eclipse.dependsOn("generateSources") 

    task copyJars(type: Copy) { 
     from(configurations.compile) {} 
     into genLibDir 
    } 

    task initconfig(type:Copy) { 
     from('src/staging/toneserver') { 
      include '**/*' 
     } 

     into '$buildDir/staging' 
    } 

    task zip(type: Zip, overwrite: true) << { 
     archiveName 'toneserver-'+project.version+'.zip' 
     from('src/staging') { 
      include 'toneserver/**' 
     } 
     from('output/thirdpartylibs') { 
      into('toneserver/lib') 
     } 
     from('$versionDir') { 
      into('toneserver') 
     } 
    } 

    zip.dependsOn('copyJars') 
} 
+0

人一個src目錄,我繼續嘗試100種不同的東西....我們需要的是一個更好的一個尋呼機文件,關於如何做這些屬性,因爲我想要的只是一個簡單的全球性屬性,xxxxx = yyyyy,並且它可以影響到任何地方,我可以看起來沒錯,現在有4個博客和幾個不同的gradle頁面......我確信一旦點擊了它,我會得到它,但這是gradle的一部分,不如其​​餘。 –

回答

3

要通過Project#file懶洋洋地評估參數解析爲每財產

sourceSets { 
    main { 
     java { 
      srcDir { "$buildDir/generated-src" } 
     } 
    } 
} 

描述它如何解決封蓋進入一個值的結果。這意味着評估會延遲到buildDir具有正確的值之後。

您同時還使用單引號,而不是一個GString型(雙引號),所以我將不得不雖然這是把你輸入一個字符串此刻字面所以尋找一個名爲$buildDir/generated-src

+0

在這種情況下,應該使用雙引號。 –

+0

是的,我所需要的只是我發現的雙引號,但是爲什麼。只使用雙引號和使用{和}大括號有什麼區別?謝謝, –

+0

雙引號將它變成一個gstring,它是一個可以放置變量的字符串(使用$)。只需使用gstring就意味着立即從'「$ buildDir/generated-src」'將其評估爲''BuildDirReallyIs/generated-src''。大括號將參數轉換爲閉包,以便延遲該代碼塊的執行時間。 – Matt