我有build.gradle
个文件:
...
jar {
baseName 'dev-filename'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
}
...
属性文件src/main/resources/application.properties
:
...
database.username=dev_user
database.password=dev_password
...
如何创建任务(dev和prod)来构建jar文件并更新属性文件中的值?
UPD1: 我接下来尝试了,但它不起作用:
...
jar {
baseName 'dev-filename'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
ant.propertyfile(file: 'application.properties') {
entry(key: 'database.username', value: 'new_username')
entry(key: 'database.password', value: 'new_password')
}
}
...
答案 0 :(得分:1)
在打包之前尝试处理您的资源。
processResources {
filesMatching('*.properties') {
filter( ReplaceTokens, tokens:['foo' : 'bar'])
}
}
答案 1 :(得分:0)
我解决了我的问题:
task jarProd(type: Jar) {
doFirst {
ant.property(file: "build/resource/main/config/application.properties") {
entry(key: "database.username", value: "prod_user",
entry(key: "database.password", value: "prod_password"
}
}
baseName 'myapp'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
with jar
}
task zipProd(dependsOn: jarProd, type: Zip) {
baseName 'prod/myapp'
from ('build/libs') {
include("myapp.jar")
}
from configurations.runtime
}
task jarTest(type: Jar) {
doFirst {
ant.property(file: "build/resource/main/config/application.properties") {
entry(key: "database.username", value: "test_user",
entry(key: "database.password", value: "test_password"
}
}
baseName 'myapp'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
with jar
}
task zipTest(dependsOn: jarTest, type: Zip) {
baseName 'test/myapp'
from ('build/libs') {
include("myapp.jar")
}
from configurations.runtime
}
task zip {
dependsOn zipTest
dependsOn zipProd
}
因此,我有2个相同的zip文件,其中包含不同的DB凭据