【发布时间】:2015-10-31 09:32:06
【问题描述】:
我正在尝试用 Gradle 构建脚本生成的新文件替换我的资源文件夹 (src/main/resources) 中的文件。我在这样做时遇到了麻烦;排除似乎已被记住,并阻止添加我的新文件。
这是一个说明行为的简短示例。
项目结构:
TestProject
-- src/main/java
---- entry
------ EntryPoint.java
---- run
------ HelloWorldTest.java
-- src/main/resources
---- test.properties // FILE TO REPLACE
src/main/resources中的test.properties内容:
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
【问题讨论】: