How to set project.version by passing version property on gradle command line?

前端 未结 6 880
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 03:55

I want to build JAR with self-defined version passed via command line, such as:

When I execute gradle build task like this:

gradle build -Pversion=1.         


        
相关标签:
6条回答
  • 2020-12-13 04:31

    Set the property only in the gradle.properties file (i.e. remove it from build.gradle). Also make sure the options come before the command (as mentioned above).

    gradle.properties contents:

    version=1.0.12
    

    Version can then be overridden on the command line with:

    gradle -Pversion=1.0.13 publish
    
    0 讨论(0)
  • 2020-12-13 04:36

    If you need a default version other than 'unspecified':

    version = "${version != 'unspecified' ? version : 'your-default-version'}"
    

    Pass version via command line:

    gradle build -P version=1.0
    
    0 讨论(0)
  • 2020-12-13 04:41

    If you move version entry to gradle.properties file you can also:

    gradle clean build -Dorg.gradle.project.version=1.1
    
    0 讨论(0)
  • 2020-12-13 04:43

    version = (findProperty('version') == 'unspecified') ? '0.1' : version

    0 讨论(0)
  • 2020-12-13 04:55

    You are not able to override existing project properties from command line, take a look here. So try to rename a version variable to something differing from version and set it with -P flag before command, like:

    gradle -PprojVersion=10.2.10 build 
    

    And then in your build.gradle

    if (project.hasProperty('projVersion')) {
      project.version = project.projVersion
    } else {
      project.version = '10.0.0'
    }
    

    Or as you did with ?: operator

    0 讨论(0)
  • 2020-12-13 04:58

    You can pass the project version on cli with -Pversion=... as long as you don't set it in build.gradle. If you need a custom default value for when no version is passed on the cli, use gradle.properties file like so: version=...

    TL;DR: Don't set the version in build.gradle file if you want to change it later on via cli.

    0 讨论(0)
提交回复
热议问题