在构建之前替换文件中的令牌,但将令牌保留在源中 [英] Replace token in file before building, but keep token in sources

查看:69
本文介绍了在构建之前替换文件中的令牌,但将令牌保留在源中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在构建之前用一个版本替换Java源文件中的@VERSION@令牌(Gradle是我选择的构建系统).

I want to replace a @VERSION@ token in a java source file with a version before building (Gradle is my build system of choice).

在我当前的脚本ant.replace(file: 'src/main/java/randers/notenoughvocab/main/Reference.java', token: '@VERSION@', value: version)中,它替换了实际源文件中@VERSION@的出现,因此,在构建之后,所有出现的模式都已被版本替换,如果我更改了版本,则gradle生成文件它将不再在那里找到任何模式,并且版本也不会更新.

In my current script ant.replace(file: 'src/main/java/randers/notenoughvocab/main/Reference.java', token: '@VERSION@', value: version) it replaces the occurrences of @VERSION@ in the actual source file, so after a build all occurrences of the pattern have been replaced by the version and if I change the version the the gradle build file it will no longer find any patterns in there and the version will not update.

我还在此处看到了一项任务,但我没有看到得出需要为我的特定项目应用哪些值.

I have also seen a task here, but I do not get what values need to be applied for my specific project.

我的项目的项目布局,如果需要的话:

The project layout for my project, if that is needed:

推荐答案

在向公众发布软件之前,您只需要替换@VERSION@令牌即可.在这里,我定义了一个完成任务的任务compileForRelease:

You only need to replace @VERSION@ tokens before releasing your software to the public. Here I defined a task compileForRelease that accomplishes it:

import org.apache.tools.ant.filters.ReplaceTokens

task sourcesForRelease(type: Copy) {
    from 'src/main/java'
    into 'build/adjustedSrc'
    filter(ReplaceTokens, tokens: [VERSION: '2.3.1'])
}

task compileForRelease(type: JavaCompile, dependsOn: sourcesForRelease) {
    source = sourcesForRelease.destinationDir
    classpath = sourceSets.main.compileClasspath
    destinationDir = file('build/adjustedClasses')
}

我不建议您弄乱Java插件定义的标准任务,因为那样会增加每个构建的不必要的开销.

I don't recommend messing with standard tasks defined by the Java plugin because that would add unnecessary overhead to each and every build.

这篇关于在构建之前替换文件中的令牌,但将令牌保留在源中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆