Gradle任务在.java文件中替换字符串无效。

huangapple go评论67阅读模式
英文:

Gradle task replace string in .java file not working

问题

我有下面的 VersionConstants.java 文件:

public class VersionConstants {

    /**
     * This class does not need to be instantiated.
     */
    private VersionConstants() { }

    public static final String VERSION = "@VERSION@";

    public static final String PATCH_LEVEL = "@PATCH_LEVEL@";

    public static final String REVISION = "@REVISION@";

    public static final String BUILDTIME = "@BUILDTIME@";

    public static final String BUILDHOST = "@BUILDHOST@";
}

我按照这里的回答 https://stackoverflow.com/a/33475075/1665592 进行了操作,尝试如下:

task generateSources(type: Copy) {
    from 'src/replaceme/VersionConstants.java'
    into "$buildDir/generated-src"
    filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        "@VERSION@" : '1.0.0', 
        "@PATCH_LEVEL@" : '0.5',
        ...
    ])
}

但是,它只是复制了 VersionConstants.java 文件本身,并没有将关键字替换为所需的值,即 1.0.00.5 等。

为什么会这样?

英文:

I've below VersionConstants.java file..

public class VersionConstants {

    /**
     * This class does not need to be instantiated.
     */
    private VersionConstants() { }


    public static final String VERSION = "@VERSION@";

    public static final String PATCH_LEVEL = "@PATCH_LEVEL@";

    public static final String REVISION = "@REVISION@";

    public static final String BUILDTIME = "@BUILDTIME@";

    public static final String BUILDHOST = "@BUILDHOST@";
}

I followed this answer from here https://stackoverflow.com/a/33475075/1665592 and tried as

task generateSources(type: Copy) {
    from 'src/replaceme/VersionConstants.java'
    into "$buildDir/generated-src"
    filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        "@VERSION@" : '1.0.0', 
        "@PATCH_LEVEL@" : '0.5',
        ...
    ])
}

but, it is coping VersionConstants.java file as it is and not replacing the keywords with desired values i.e. 1.0.0 or 0.5 etc.

Why?

答案1

得分: 2

默认情况下,ReplaceTokens的beginToken为'@',endToken为'@'。所以更改为

filter(ReplaceTokens, tokens: [
        "VERSION" : '1.0.0', 
        "PATCH_LEVEL" : '0.5',
        ...
    ])
英文:

By default, ReplaceTokens has beginToken='@' and endToken='@'. So change to

filter(ReplaceTokens, tokens: [
        "VERSION" : '1.0.0', 
        "PATCH_LEVEL" : '0.5',
        ...
    ])

答案2

得分: 0

明白,我刚刚将@VERSION@替换为VERSION,它就像魔法般地正常工作了!

task generateSources(type: Copy) {
    from 'src/replaceme/VersionConstants.java';
    into "$buildDir/generated-src";
    filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        "VERSION" : '1.0.0',
        "PATCH_LEVEL" : '0.5',
        ...
    ])
}
英文:

Got it, I just replaced @VERSION@ with VERSION and it work like a charm!

task generateSources(type: Copy) {
    from 'src/replaceme/VersionConstants.java'
    into "$buildDir/generated-src"
    filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        "VERSION" : '1.0.0', 
        "PATCH_LEVEL" : '0.5',
        ...
    ])
}

huangapple
  • 本文由 发表于 2020年9月15日 01:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63888914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定