为什么`gradle`在每个子项目中执行`ant.replaceregexp`?

时间:2017-11-15 11:19:24

标签: java gradle ant

我有一个具有这种结构的java gradle项目:

EarApp
|
\---> WarA/trunk
|     |
|     |- build.gradle
|     ...<src and config>
|
\---> WarB/trunk
|     |
|     |- build.gradle
|     ...<src and config>
|
\---> WarC/trunk
|     |
|     |- build.gradle
|     ...<src and config>
|
|- build.gradle
|- settings.gradle

我的settings.gradle是:

include "WarA"
include "WarB"
include "WarC"

project(":WarA").projectDir          = file("WarA/trunk")
project(":WarB").projectDir          = file("WarB/trunk")
project(":WarC").projectDir          = file("WarC/trunk")

子项目中的每个build.gradle都包含:

import org.tmatesoft.svn.core.wc.*
def getSvnRevision(){
    ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
    SVNClientManager clientManager = SVNClientManager.newInstance(options);
    SVNStatusClient statusClient = clientManager.getStatusClient();
    SVNStatus status = statusClient.doStatus(projectDir, false);
    SVNRevision revision = status.getRevision();
    return revision.getNumber();
}


def replaceTokens() {
    ant.replaceregexp(
      file:  "./src/main/resources/messages/text.properties",
        match:   "application.revision=(.*)",
        replace: "application.revision=rev_" + getSvnRevision(),
        byline:  "true"
    )
}


war {
  replaceTokens() // Update rev number
  baseName = nome

  from('config') {
    include 'ibm-web-ext.xml'
    into 'WEB-INF'

    manifest {
      attributes("Application-Name": baseName,
                 "Implementation-Version": versione + 'rev_' + getSvnRevision())
    }
  }
}

执行gradle -p WarA/trunk wargradle WarA:war时,WarA.war已正确构建,但我希望 ant 仅更新./src/main/resources/messages/text.properties WarA 。

相反,我得到每个子项目更新./src/main/resources/messages/text.properties文件。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

replaceTokens()内移动doFirst{}可以解决问题。

war {
  doFirst{
    replaceTokens() // Update rev number
  }
  baseName = nome

  from('config') {
    include 'ibm-web-ext.xml'
    into 'WEB-INF'

    manifest {
      attributes("Application-Name": baseName,
                 "Implementation-Version": versione + 'rev_' + getSvnRevision())
    }
  }
}

参考此处:https://discuss.gradle.org/t/what-difference-between-configuration-setting-and-dofirst-dolast/23406