替换 Gradle 版本中的文件
Replace File in Gradle Build
我正在尝试用 Gradle 构建脚本生成的新文件替换资源文件夹 (src/main/resources) 中的文件。我在做这件事时遇到了麻烦;排除似乎被记住了,并阻止添加我的新文件。
这是一个说明该行为的简短示例。
项目结构:
TestProject
-- src/main/java
---- entry
------ EntryPoint.java
---- run
------ HelloWorldTest.java
-- src/main/resources
---- test.properties // FILE TO REPLACE
test.properties src/main/resources中的内容:
Wrong File with extra text to make it obvious which one is being put into the jar based on size
build.gradle:
apply plugin: 'java'
task makeProp {
def propDir = new File(buildDir, "props")
ext.propFile = new File(propDir, "test.properties")
outputs.file propFile
doLast {
propDir.mkdirs()
propFile.createNewFile()
propFile.withWriter('utf-8') { writer ->
writer.writeLine 'Right File'
}
}
}
jar {
dependsOn('makeProp')
if (project.hasProperty('testExclude')) {
sourceSets {
exclude('test.properties')
}
}
from (makeProp.propFile) {
into '/'
}
}
./gradlew build
的 JAR 内容(包括两个文件):
Archive: TestProject.jar
Length Date Time Name
-------- ---- ---- ----
0 08-07-15 14:27 META-INF/
25 08-07-15 14:27 META-INF/MANIFEST.MF
0 08-07-15 13:50 run/
499 08-07-15 13:50 run/HelloWorldTest.class
0 08-07-15 13:50 entry/
1413 08-07-15 13:50 entry/EntryPoint.class
95 08-07-15 14:27 test.properties
11 08-07-15 14:03 test.properties
-------- -------
2043 8 files
./gradlew build -PtestExclude
的 JAR 内容(均未包含文件):
Archive: TestProject.jar
Length Date Time Name
-------- ---- ---- ----
0 08-07-15 14:29 META-INF/
25 08-07-15 14:29 META-INF/MANIFEST.MF
0 08-07-15 13:50 run/
499 08-07-15 13:50 run/HelloWorldTest.class
0 08-07-15 13:50 entry/
1413 08-07-15 13:50 entry/EntryPoint.class
-------- -------
1937 6 files
问题是 gradle SourceSet 的排除模式适用于所有包含的路径,而不仅仅是特定路径。因此,在您上面的示例中,所有名为 test.properties
的文件都将被排除,无论它们位于什么位置。
你可以做的是将默认 test.properties
放在其他地方,并根据场景使构建 copy/generate 成为相关版本。
我做过一些非常相似的事情,这对我有用。主要 objective 是确保任务在创建 jar 和处理文件之前运行。试试这个。
// create a properties file add it to folder preprocessing
task propertiesFile << {
//
description 'Dynamically creates a properties file.'
// needed for the first pass
def folder = project.file('src/main/resources');
if(!folder.exists()){
folder.mkdirs()
}
//write it to a propertiess file
def props = project.file('src/main/resources/test.properties')
props.delete()
props << 'write this to my file!!!!'
}
processResources.dependsOn propertiesFile
我最后采用的解决方案与 Pumphouse 发布的解决方案类似,但我没有删除文件,而是将其重命名为临时名称,排除该临时名称,并在完成后重命名(我需要在构建完成后保留文件内容和位置。
已修改 build.gradle:
apply plugin: 'java'
def props = project.file('src/main/resources/test.properties')
def temp = project.file('src/main/resources/temp.properties')
task makeProp {
... // Unchanged
}
jar {
dependsOn('makeProp')
// Move the properties file to a temp location
props.renameTo(temp) // This returns a boolean; can perform conditional checks.
// Exclude the temp file
if (project.hasProperty('testExclude')) {
sourceSets {
exclude('temp.properties')
}
}
// Insert the right prop file
from (makeProp.propFile) {
into '/'
}
}
jar << {
// Restore the temp file
temp.renameTo(props)
}
我正在尝试用 Gradle 构建脚本生成的新文件替换资源文件夹 (src/main/resources) 中的文件。我在做这件事时遇到了麻烦;排除似乎被记住了,并阻止添加我的新文件。
这是一个说明该行为的简短示例。
项目结构:
TestProject
-- src/main/java
---- entry
------ EntryPoint.java
---- run
------ HelloWorldTest.java
-- src/main/resources
---- test.properties // FILE TO REPLACE
test.properties src/main/resources中的内容:
Wrong File with extra text to make it obvious which one is being put into the jar based on size
build.gradle:
apply plugin: 'java'
task makeProp {
def propDir = new File(buildDir, "props")
ext.propFile = new File(propDir, "test.properties")
outputs.file propFile
doLast {
propDir.mkdirs()
propFile.createNewFile()
propFile.withWriter('utf-8') { writer ->
writer.writeLine 'Right File'
}
}
}
jar {
dependsOn('makeProp')
if (project.hasProperty('testExclude')) {
sourceSets {
exclude('test.properties')
}
}
from (makeProp.propFile) {
into '/'
}
}
./gradlew build
的 JAR 内容(包括两个文件):
Archive: TestProject.jar
Length Date Time Name
-------- ---- ---- ----
0 08-07-15 14:27 META-INF/
25 08-07-15 14:27 META-INF/MANIFEST.MF
0 08-07-15 13:50 run/
499 08-07-15 13:50 run/HelloWorldTest.class
0 08-07-15 13:50 entry/
1413 08-07-15 13:50 entry/EntryPoint.class
95 08-07-15 14:27 test.properties
11 08-07-15 14:03 test.properties
-------- -------
2043 8 files
./gradlew build -PtestExclude
的 JAR 内容(均未包含文件):
Archive: TestProject.jar
Length Date Time Name
-------- ---- ---- ----
0 08-07-15 14:29 META-INF/
25 08-07-15 14:29 META-INF/MANIFEST.MF
0 08-07-15 13:50 run/
499 08-07-15 13:50 run/HelloWorldTest.class
0 08-07-15 13:50 entry/
1413 08-07-15 13:50 entry/EntryPoint.class
-------- -------
1937 6 files
问题是 gradle SourceSet 的排除模式适用于所有包含的路径,而不仅仅是特定路径。因此,在您上面的示例中,所有名为 test.properties
的文件都将被排除,无论它们位于什么位置。
你可以做的是将默认 test.properties
放在其他地方,并根据场景使构建 copy/generate 成为相关版本。
我做过一些非常相似的事情,这对我有用。主要 objective 是确保任务在创建 jar 和处理文件之前运行。试试这个。
// create a properties file add it to folder preprocessing
task propertiesFile << {
//
description 'Dynamically creates a properties file.'
// needed for the first pass
def folder = project.file('src/main/resources');
if(!folder.exists()){
folder.mkdirs()
}
//write it to a propertiess file
def props = project.file('src/main/resources/test.properties')
props.delete()
props << 'write this to my file!!!!'
}
processResources.dependsOn propertiesFile
我最后采用的解决方案与 Pumphouse 发布的解决方案类似,但我没有删除文件,而是将其重命名为临时名称,排除该临时名称,并在完成后重命名(我需要在构建完成后保留文件内容和位置。
已修改 build.gradle:
apply plugin: 'java'
def props = project.file('src/main/resources/test.properties')
def temp = project.file('src/main/resources/temp.properties')
task makeProp {
... // Unchanged
}
jar {
dependsOn('makeProp')
// Move the properties file to a temp location
props.renameTo(temp) // This returns a boolean; can perform conditional checks.
// Exclude the temp file
if (project.hasProperty('testExclude')) {
sourceSets {
exclude('temp.properties')
}
}
// Insert the right prop file
from (makeProp.propFile) {
into '/'
}
}
jar << {
// Restore the temp file
temp.renameTo(props)
}