Gradle 用当前 WAR 文件名更新耳朵 application.xml

Gradle ear update application.xml with current WAR filename

我有一个 java EAR 项目,其中包含一些 WAR 网络应用程序。

我正在使用 gradle 构建 EAR 文件。

uberApp
|
\---> WarA
|     |
|     ...<src and config>
|
\---> WarB
|     |
|     ...<src and config>
|
\--> config/META-INF/application.xml

这是 uberApp build.gradle:

apply plugin: 'ear'


dependencies {
    deploy project(path: ':WarA/trunk', configuration: 'archives')
    deploy project(path: ':WarB/trunk', configuration: 'archives')

}

ear {
    appDirName 'config'
}

这是WAR build.gradle:

war {
    baseName = 'WarA'
    version = '1.2.3_rev'  + getSvnRevision() // provided by SvnKit
}

...因此生成的文件名始终包含手写版本号和 SVN 提交号:WarA-1.2.3_rev31337.war

但我需要在 EAR 组装之前用正确的 WAR 标签内的文件名更新我的 application.xml

这是 EAR application.xml:

<?xml version="1.0"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="6">
  <module>
    <web>
      <web-uri>WarA.war</web-uri>
      <context-root>/WarA</context-root>
    </web>
  </module>
  <module>
    <web>
      <web-uri>WarB.war</web-uri>
      <context-root>/WarB</context-root>
    </web>
  </module>
  <library-directory>lib</library-directory>
</application>

我怎样才能做到这一点?

可能有更好的方法,但我实现了我的目标,即编写一个 groovy 函数来在 ear 构建期间完成这项工作。

这是我的全build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        // SvnKit from https://gist.github.com/thombergs/9279728
        classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.14'
    }
}


// SvnKit get svn revision
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();
        }




// extract from file
def extractfromfile(source, pattern) {
  (source.text =~ pattern) [0]
}

// extract from string
def extract(source, pattern) {
  (source =~ pattern) [0]
}

// replace
def ReplaceText(source, targetText, replaceText){
  source.write(source.text.replaceAll(targetText, replaceText))
}


def updateApplicationXml() {
  def applicationXml = new File('config/META-INF/application.xml')
  def settingsGradle = new File('settings.gradle')
  def prjRegex = "'(.*)'"
  def prj =  settingsGradle.text.split(',')



  //for every project
  List<String> list = new ArrayList<String>(Arrays.asList(prj))

  for(String item: list){

    def prjPath = extract(item, prjRegex)[1]
    //println prjPath


    //search for build.gradle
    def buildGradle = new File(prjPath+'/build.gradle')
    def basenamePattern = "baseName = '(.*)'"
    def versionPattern = "version = '(.*)'"

    //extract basename
    def basename
    try {
      basename = extractfromfile(buildGradle, basenamePattern)
    } catch (Exception ex){
        continue
    }

    //extract version
    def version
    try {
      version = extractfromfile(buildGradle, versionPattern)
    } catch (Exception ex){
        continue
    }

    def warname = basename[1] + "-" + version[1] + getSvnRevision()

  //  println basename[1]
  //  println version[1]
  //  println warname
  //  println applicationXml


    // do the replace
    ReplaceText(applicationXml, "<web-uri>"+basename[1]+"(.*).war</web-uri>", "<web-uri>"+warname+".war</web-uri>")

  }
}

apply plugin: 'ear'

dependencies {
    deploy project(path: ':WarA/trunk', configuration: 'archives')
    deploy project(path: ':WarB/trunk', configuration: 'archives')

}



ear {
    updateApplicationXml()
    appDirName 'config'
}