I want to replace a @VERSION@
token in a java source file with a version before building (Gradle is my build system of choice).
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:
My gradle build file: View on github
答案 0 :(得分:15)
在向公众发布软件之前,您只需要替换\\\*|([^\\]?\*)
令牌。在这里,我定义了一个完成它的任务@VERSION@
:
compileForRelease
我不建议搞乱Java插件定义的标准任务,因为这会给每个版本增加不必要的开销。
答案 1 :(得分:6)
WARNING: As indicated in comments by @Raffaele filtering source code may result in serious problems. This answer assumes that you know well what are you doing and are conscious about potential problems that may occur.
The problem is in the fact that java source files are not copied - they're compiled only - in place. So you need to:
@VERSION@
Not sure about paths but the following piece of code should be helpful:
apply plugin: 'java'
version = '0.0.1'
group = 'randers.notenoughvocab'
archivesBaseName = 'NotEnoughVocab'
def versionFile = 'src/main/java/randers/notenoughvocab/main/Reference.java'
def tempDir = 'build/tmp/sourcesCache'
def versionFileName = 'Reference.java'
compileJava.doFirst {
copy {
from(versionFile)
into(tempDir)
}
ant.replace(file: versionFile, token: '@VERSION@', value: version)
}
compileJava.doLast {
copy {
from(tempDir + '/' + versionFileName)
into(project.file(versionFile).parent)
}
}
答案 2 :(得分:3)
我遇到了同样的问题。我无法找到有效的解决方案。我找到的所有例子都没有奏效。我相信这可能是事实,我是Gradle的新手,这些例子省略了一些明显的代码。所以我调查了Gradle并找到了自己的解决方案,我想分享一下:
import org.apache.tools.ant.filters.ReplaceTokens
// Change compiler source directory
sourceSets {
main {
java {
srcDirs = ['build/src/main/java']
}
}
}
// Prepare sources for compilation
task prepareSources(type: Copy) {
from('src/main/java')
into('build/src/main/java')
filter(ReplaceTokens, tokens: [pluginVersion: version])
}
// Prepare sources, before compile
compileJava {
dependsOn prepareSources
}
答案 3 :(得分:2)
我发现现有答案有些不令人满意,所以这是我的解决方案:
import org.apache.tools.ant.filters.ReplaceTokens
task processSource(type: Sync) {
from sourceSets.main.java
inputs.property 'version', version
filter(ReplaceTokens, tokens: [VERSION: version])
into "$buildDir/src"
}
compileJava {
source = processSource.outputs
}
这解决了以下各种问题:
$buildDir/src
任务对processSource
的修改来分阶段进行的,该任务反映了标准processResources
。sourceSets.main.java.srcDirs
仍然是默认值,并且可以轻松地指定尚不存在的位置filter
的范围。src
,$buildDir
下的目录,已处理的源文件放置在该目录下。inputs.property
完成的。Sync
而不是Copy
,以便从源中删除的文件也从已过滤的源中删除(感谢@Earthcomputer)。答案 4 :(得分:1)
为了补充其他答案,如果您只想改变一个值,我发现这个成语更简单:
task generateSources(type: Copy) {
from 'src/main/java'
into 'build/src/main/java'
filter { line -> line.replaceAll('xxx', 'aaa') }
}