2017-08-17 56 views
3

我有一個java EAR項目,其中包含一些WAR web-app。帶有當前WAR文件名的Gradle Ear update application.xml

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

這是戰爭的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之前組裝。

這是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> 

我怎樣才能做到這一點?

回答

1

可能有更好的方法,但我實現我的目標寫一個常規功能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' 
} 
相關問題