Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle how to change version number in source code

Tags:

gradle

Java code:

public static String VERSION = "version_number";

Gradle build.gradle

version = '1.0'

How to set the version in java code from grade? The version must be in source code.

Is there a convenient way? A not-so-nice way:

  1. copy the java file to another location, e.g. build/changed-source
  2. change the version in the source, by replacing token
  3. add the build/changed-source in main source set.
like image 666
eastwater Avatar asked Aug 20 '18 19:08

eastwater


People also ask

How do I set Gradle version in build Gradle?

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or update your Gradle version using the command line.

How do I get the latest version of dependency in Gradle?

You can use the version string “latest. integration” to achieve this. This is a dynamic version that tells Gradle to use the very latest version of a dependency. Note that for a maven repository, this feature requires that the list of all available versions is published in a maven-metadata.


2 Answers

I'd do similar to Michael Easter but with these differences

  1. Store generated sources separately from main sources (src/main/java and $buildDir/generated/java). This has the added benefit of not needing custom gitignore
  2. Generate in a subdirectory of $buildDir so that clean task will delete the generated sources
  3. Use a separate task for code generation with proper up-to-date & skip support
  4. Use Copy.expand(Map) to do the token replacement
  5. Since its directory based, everything in src/template/java will have tokens replaced. You can easily add more templates in future

src/template/java/com/foo/BuildInfo.java

package com.foo;
public class BuildInfo {
    public static String getVersion() {
        return "${version}";
    }
} 

build.gradle

task generateJava(type:Copy) {
    def templateContext = [version: project.version]
    inputs.properties templateContext // for gradle up-to-date check 
    from 'src/template/java' 
    into "$buildDir/generated/java" 
    expand templateContext 
} 
sourceSets.main.java.srcDir "$buildDir/generated/java" // add the extra source dir 
compileJava.dependsOn generateJava // wire the generateJava task into the DAG
like image 172
lance-java Avatar answered Oct 29 '22 02:10

lance-java


One method is to similar to your not-so-nice way, but slightly easier. Consider a file in templates/BuildInfo.java:

package __PACKAGE;

public class BuildInfo {
    private static final String version = "__VERSION";
    private static final String buildTimestamp = "__BUILD_TIMESTAMP";

    public String toString() {
        return "version         : " + version + "\n" + 
               "build timestamp : " + buildTimestamp + "\n";
    }
}

This file can then be "stamped" with information as first thing in the compileJava task and written to src/main/java/your/package/BuildInfo.java:

def targetPackage = 'net/codetojoy/util'
def targetPackageJava = 'net.codetojoy.util'

def appVersion = project.appVersion // from gradle.properties
def buildTimeStamp = new Date().toString()

compileJava { 
    doFirst {
        ant.mkdir(dir: "${projectDir}/src/main/java/${targetPackage}")
        def newBuildInfo = new File("${projectDir}/src/main/java/${targetPackage}/BuildInfo.java")
        def templateBuildInfo = new File("${projectDir}/templates/TemplateBuildInfo.java")

        newBuildInfo.withWriter { def writer ->
            templateBuildInfo.eachLine { def line ->
                def newLine = line.replace("__PACKAGE", targetPackageJava)
                                  .replace("__VERSION", appVersion)
                                  .replace("__BUILD_TIMESTAMP", buildTimeStamp)
                writer.write(newLine + "\n");
            }
        }
    }
}

A working example is provided here. Everything would be stored in source-control except the src/main/java/your/package/BuildInfo.java file. Note the version would be stored in gradle.properties.

like image 20
Michael Easter Avatar answered Oct 29 '22 02:10

Michael Easter