Gradle Ear 使用当前 WAR 文件名更新 application.xml

2023-12-31

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

我正在使用 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

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

这是 EARapplication.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>

我怎样才能实现这个目标?


可能有更好的方法,但我实现了我的目标,编写一个常规函数来完成这项工作ear build.

这是我的完整版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'
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Gradle Ear 使用当前 WAR 文件名更新 application.xml 的相关文章

随机推荐