2016-01-29 20 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:我回答一个年纪大一点的问题,但希望这些指针可能会有所帮助的人面临着类似的问题。