如何在gradle中将版本号或其他内容放入我的zip存档中

时间:2012-09-06 16:30:37

标签: gradle

编辑: 我正在尝试运行这两个命令并将这些结果放在gradle子项目中......

gradle assemble -> databus-Developer-Build.zip
gradle -DmyVersion=1.0.2 -> databus-1.0.2.zip

目前,我的输出是databus-null.zip如果我使用$ version而不是$ myVersion。使用$ MyVersion时,我收到错误“myVersion不是该任务的属性”。 :( :(。

修改

所以,尝试第一个答案完全失败了“找不到属性$ myVersion on task':webserver:myZip”

注意:我现在正试图在子项目中执行此操作。这是子项目的gradle代码...

project(':webserver') {
    project.ext.genLibDir = file('lib')
    project.ext.fixedLibDir = file('../master/libother')

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

    task deleteJars(type: Delete) {
        ext.collection = files { genLibDir.listFiles() }
        delete ext.collection
    }
    task copyJars(type: Copy) { 
        from(configurations.compile) {}
        from(fixedLibDir) {}
        into genLibDir
    }
    copyJars.dependsOn('deleteJars')
    classes.dependsOn('copyJars')

    task myZip(type: Zip) {
        archiveName "dashboard-"+$myVersion+".zip"
        from('..') {
            include 'webserver/run*.sh'
            include 'webserver/app/**'
            include 'webserver/conf/**'
            include 'webserver/play-1.2.4/**'
            include 'webserver/public/**'
        }
    }

    assemble.dependsOn('myZip')

    //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) {
    }


    gradle.taskGraph.whenReady {taskGraph ->
        if (taskGraph.hasTask(assemble) && myVersion == null) {
            myVersion = 'Developer-Build'
        }
    }
}

感谢, 迪安

2 个答案:

答案 0 :(得分:0)

我认为您收到错误“myVersion不是该任务的属性”的原因是,您必须使用-P而不是“-D”传递属性

答案 1 :(得分:0)

好的,正确的答案是做这样的事情......(将此添加到allprojects部分)..

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

有趣的是,下面的一个不起作用,因为'version'似乎是某种reserverd属性,并且在启动时设置为未指定...

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

看到版本似乎是一个保留属性,它很可能用于发布工件,所以最好的解决方案可能是以下

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

并且自动构建在myVersion和开发人员中传递,当然不会。