I';我将一个Maven WAR版本迁移到Gradle。如何使过滤器块以相同的方式运行?
我正在将一个构建从Maven转换为Gradle,并在其中执行一些资源过滤。具体来说,我们有一个块,看起来像这样:I';我将一个Maven WAR版本迁移到Gradle。如何使过滤器块以相同的方式运行?,maven,gradle,war,Maven,Gradle,War,我正在将一个构建从Maven转换为Gradle,并在其中执行一些资源过滤。具体来说,我们有一个块,看起来像这样: <build> <finalName>${project.name}-${project.version}</finalName> <resources> <resource> <directory>src/main/resources</direct
<build>
<finalName>${project.name}-${project.version}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<filters>
<filter>/path/to/more/resources.properties</filter>
<filter>/path/to/more/resources.properties</filter>
<filter>/path/to/more/resources.properties</filter>
</filters>
</build>
${project.name}-${project.version}
src/main/resources
真的
src/测试/资源
真的
/path/to/more/resources.properties
/path/to/more/resources.properties
/path/to/more/resources.properties
…我们希望在格拉德也能得到类似的东西
的文档没有那么完整,也没有描述我试图解决的具体问题:
- 筛选出特定的资源文件
- 保持使用中变量的POM样式(
)${example.property}
- 确保神器进入我的战争
filter
方法可以识别的内容中。按照惯例,这只是一个标准的Properties
对象
可以这样完成:
def resources = new Properties()
file($projectDir/src/main/resources/<insert-properties-file>.properties").withInputStream {
resources.load(it)
}
要将其应用到战争中,我们需要将其加载到processResources
任务中
总而言之:
processResources {
with copySpec {
def resources = new Properties()
file($projectDir/src/main/resources/<insert-properties-file>.properties").withInputStream {
resources.load(it)
}
from("$projectDir/path/to/folder/that/has/tokens/to/replace") {
include "**/files-to-filter-on"
filter(ReplaceTokens, tokens: resources, beginToken: "${", endToken: "}")
}
}
processResources{
用复印机{
def resources=新属性()
文件($projectDir/src/main/resources/.properties”)。withInputStream{
资源。加载(it)
}
from(“$projectDir/path/to/folder/that/has/tokens/to/replace”){
包括“***/要筛选的文件”
过滤器(替换令牌,令牌:资源,beginToken:“${”,endToken:“}”)
}
}
当您执行/gradlew clean war
时,您要筛选的资源将在存档中正确筛选
processResources {
with copySpec {
def resources = new Properties()
file($projectDir/src/main/resources/<insert-properties-file>.properties").withInputStream {
resources.load(it)
}
from("$projectDir/path/to/folder/that/has/tokens/to/replace") {
include "**/files-to-filter-on"
filter(ReplaceTokens, tokens: resources, beginToken: "${", endToken: "}")
}
}