gradle - gradle属性有时不起作用

标签 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文件在这里,并且与上面的问题类似,但是失败了,因为找不到生成的源代码:(。我不确定是否是相同的问题。这是bug吗?
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')
}

最佳答案

您想懒惰地评估属性(property)

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

按照 Project#file 解析该参数,该参数描述了如何将闭包的结果解析为一个值。这意味着评估将延迟到buildDir具有正确的值之后。

您还使用了单引号而不是GString(双引号),所以我现在虽然将您的输入作为字符串文字使用,所以正在寻找名为$buildDir/generated-src的src目录

关于gradle - gradle属性有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14630545/

相关文章:

android - 我无法将我的项目与 gradle 同步?

java - Gradle - 同一库的 2 个不同版本

java - Android Studio:运行项目时出错

android - 在 Android Studio 中将 Build Type debuggable 设置为 True 不适用

java - 如何在 Docker 镜像中安装 Java 9 和 Gradle

gradle - 无法在Gradle中获取类路径

gradle - 带有gradle的影子插件的 list 问题

java - 我不知道我对 Gluon Start 做错了什么

java - 使用Gradle进行时间点构建

gradle - 如何让 Gradle 在动态版本上使用特定的存储库?