Java 在gradle上的战前任务文件中替换字符串${stringKey}
我有一个多项目的gradle配置。因此,我为每个子项目提供了一个Java 在gradle上的战前任务文件中替换字符串${stringKey},java,gradle,Java,Gradle,我有一个多项目的gradle配置。因此,我为每个子项目提供了一个build.gradle,另外一个是我定义一般任务的子项目 基本上,在generalbuild.gradle文件中,我列出了执行环境,每个环境用于生产,预生产,开发,等等目的 我设置了几个定义类的容器: class RemoteContainer { String name String container String hostname Integer port String usernam
build.gradle
,另外一个是我定义一般任务的子项目
基本上,在generalbuild.gradle
文件中,我列出了执行环境,每个环境用于生产
,预生产
,开发
,等等目的
我设置了几个定义类的容器:
class RemoteContainer {
String name
String container
String hostname
Integer port
String username
String password
String 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
编辑
基本上有两个文件:
- 在
/src/main/resources/META-INF/persistence.xml下:此文件包含数据库服务器位置信息。根据服务器环境,数据库位置位于IP/端口/数据库。。。例如:
- 在
/src/main/resources/configuration.settings.environment下:此文件仅包含此行
scope=${scope}
war
生成时进行更换
我完全不知道该怎么做。
有什么想法吗?如果您只需要替换占位符,就可以尝试类似的方法
tasks.taskName.doFirst {
exec {
commandLine "perl", "-pi","-w","-e","s/${stringKey}/"+config.purpose+"/g" ,"filePath"
}
}
}
可以使用ant.replace执行以下操作:
replaceTokens << {
ant.replace(
file: "path/to/your/file",
token: "stringtoreplace",
value: config.purpose
)
}
war.dependsOn replaceTokens
replaceTokens我面临着类似的问题。我发现更容易保持单独的环境(例如开发、qa、登台、产品等)特定的属性/设置/配置,然后在构建生命周期的适当时间加载/应用特定的属性/设置/配置。以下链接很有帮助:
PS:我在回答一个稍老的问题,但希望这些指针能对面临类似问题的人有所帮助。你的问题并不十分清楚。要打开文件并替换其中的字符串吗?为什么?文件名是什么?这必须在生命周期的什么时候发生?如何将general build.gradle添加到单个子项目build.gradle文件中?我已经编辑了这个问题,回答了您的一些问题。