2016-01-29 21 views
1

我有一個多項目gradle配置。所以我對每個子項目都有一個build.gradle,另一個是我定義一般任務的地方。

基本上,在一般build.gradle文件我設置了執行environtments,分別爲productionpre-productiondevelopment等目的。

我設置幾個容器定義一個類:

class RemoteContainer { 
    String name 
    String container 
    String hostname 
    Integer port 
    String username 
    String password 
    String purpose 
} 

所以,我設置的容器purpose字段設置爲'production''pre-production''development'的目的。

然後,我能夠創建多個容器:

def developmentRemoteContainers = [ 
    new RemoteContainer(
     name: 'wildfly8', 
     container: 'wildfly8x', 
     hostname: '----', 
     port: ----, 
     username: '----', 
     password: '----' 
     purpose: 'development' 
    ), 
    new RemoteContainer(
     name: 'glassfish4', 
     container: 'glassfish4x', 
     hostname: '----', 
     port: ----, 
     username: '----', 
     password: '----' 
     purpose: 'development' 
    ) 
] 

def preproductionRemoteContainers = [ 
    new RemoteContainer(
     name: 'wildfly8', 
     container: 'wildfly8x', 
     hostname: '----', 
     port: ----, 
     username: '----', 
     password: '----' 
     purpose: 'pro-production' 
    ), 
    new RemoteContainer(
     name: 'glassfish4', 
     container: 'glassfish4x', 
     hostname: '----', 
     port: ----, 
     username: '----', 
     password: '----' 
     purpose: 'pre-production' 
    ) 
] 

def productionUserRemoteContainers = [ 
    new RemoteContainer(
     name: 'wildfly8', 
     container: 'wildfly8x', 
     hostname: '---', 
     port: ----, 
     username: '----', 
     password: '----' 
     purpose: 'production' 
    ), 
    new RemoteContainer(
     name: 'glassfish4', 
     container: 'glassfish4x', 
     hostname: '----', 
     port: ----, 
     username: '----', 
     password: '----' 
     purpose: 'production' 
    ) 
] 

之後,創建根據每個遠程容器的內容物任務:

實施例的任務:

remoteContainers.each { config -> 
    task "deployRemote${config.name.capitalize()}"(type: com.bmuschko.gradle.cargo.tasks.remote.CargoDeployRemote) { 
     description = "Deploys WAR to remote Web Application Server: '${config.name}'." 
     containerId = config.container 
     hostname = config.hostname 
     port = config.port 
     username = config.username 
     password = config.password 
     dependsOn war 
    } 

    task "undeployRemote${config.name.capitalize()}"(type: com.bmuschko.gradle.cargo.tasks.remote.CargoUndeployRemote) { 
     description = "Deploys WAR to remote Web Application Server: '${config.name}'." 
     containerId = config.container 
     hostname = config.hostname 
     port = config.port 
     username = config.username 
     password = config.password 
    } 
} 

所以,這就是我爲每個容器創建部署和取消部署任務以及執行上下文的方式。

正如你能夠找出每個任務取決於戰爭任務。所以,我的項目有一個文件,其中包含一個像${stringKey}這樣的字符串,我需要根據每個容器目的來替換它。因此${stringKey}必須被config.purpose所替換。

編輯

有basicly兩個文件:

  • /src/main/resources/META-INF/persistence.xml:此文件包含數據庫服務器的位置信息。根據服務器environtment,數據庫的位置是在IP /端口/數據庫...例如:

    <property name="hibernate.ogm.datastore.host" value="${ip}"/> <property name="hibernate.ogm.datastore.port" value="${port}"/>

  • /src/main/resources/configuration.settings.environtment:該文件只包含此行scope = ${scope}

更換必須在war世代。

我絕對不知道該怎麼做。 任何想法?

+0

你在問什麼不是很清楚。你想打開一個文件並替換它中的一個字符串?爲什麼?什麼是文件名?生命週期的哪個階段必須發生?你如何將你的一般build.gradle添加到你的個人項目build.gradle文件? – RaGe

+0

我編輯了這個問題回答了你的問題。 – Jordi

回答

0

,你可以嘗試這樣的事情,只要你需要的是更換您的佔位符

tasks.taskName.doFirst { 
      exec { 
       commandLine "perl", "-pi","-w","-e","s/${stringKey}/"+config.purpose+"/g" ,"filePath" 
      } 
     } 
    } 
0

您可以使用ant.replace做到這一點:

replaceTokens << { 
    ant.replace(
     file: "path/to/your/file", 
     token: "stringtoreplace", 
     value: config.purpose 
    ) 
} 

war.dependsOn replaceTokens 
0

我面對一個有點類似的問題。我發現保持獨立的環境(例如dev,qa,staging,prod等)特定的屬性/ settings/config更容易,然後在構建生命週期的適當時間加載/應用特定的一個。以下鏈接是有幫助的:

  1. https://blog.gradle.org/maven-pom-profiles
  2. Gradle : Copy different properties file depending on the environment and create jar

PS:我回答一個年紀大一點的問題,但希望這些指針可能會有所幫助的人面臨着類似的問題。