/** * Provides the current lib version. * * @author PasanLive */ public final class Version { /** * Current lib version. */ public static final String LIB_VERSION = "${version}"; }
Have you noticed the placeholder “${version}” that’ll be replaced with the actual version in the build process. Then new gradle task need to be defined. So we can define a class extending DefaultTask in our build.gradle script.
class GenerateVersionFileTask extends DefaultTask { def templateLocation def destination File getDestination() { project.file(destination) } File getTemplateFile() { project.file(templateLocation) } String getTemplateContent() { getTemplateFile().getText('UTF-8').replace("${version}", getVersion()) } String getVersion() { project.version } @TaskAction def generate() { println 'Generating version file' def templateContent = getTemplateContent(); def file = getDestination() file.parentFile.mkdirs() file.write(templateContent, 'UTF-8') println Version file generated' } }