尝试创建属性文件(foo.properties)并将其添加到战争的根目录。
apply plugin: 'war'
task createProperties {
FileOutputStream os = new FileOutputStream("${project.buildDir}/foo.properties");
...
}
war {
dependsOn createProperties
from "${project.buildDir}/foo.properties"
...
}
出了什么问题:
A problem occurred evaluating project ':app'.
> E:\app\build\build.properties (The system cannot find the path specified)
我是否需要创建构建目录?
对于战争,是否有webapp的输出目录? (sourceSet:src / main / webapp)
最好直接在webapp outputDir下创建foo.properties
。
答案 0 :(得分:2)
你应该做
war {
from createProperties
...
}
这将自动添加对createProperties任务的隐式依赖,因此不需要dependsOn。
为此,您需要明确指定createProperties
的输出
task createProperties {
outputs.file("$buildDir/foo.properties")
doLast {
FileOutputStream os = new FileOutputStream("$buildDir/foo.properties");
...
}
}
但实际上你应该使用WriteProperties
类型的任务,它看起来更干净,对于可重现的构建更好。像这样:
task createProperties(type: WriteProperties) {
outputFile "$buildDir/foo.properties"
property 'foo', 'bar'
}
如果您的属性是动态计算而不是静态计算(我假设,否则您可以手动创建文件),您还应该将动态部分设置为任务的输入,以便任务最新检查工作正确并且只在必要时才运行任务,因为某些输入已更改。
答案 1 :(得分:0)
试试这样:
task createProperties {
doFirst {
FileOutputStream os = new FileOutputStream("${project.buildDir}/foo.properties");
...
}
}
用例子说明:
task foo {
println 'foo init line'
doFirst {
println 'foo doFirst'
}
doLast {
println 'foo doLast'
}
}
task bar {
println 'bar init line'
doFirst {
println 'bar doFirst'
}
doLast {
println 'bar doLast'
}
}
现在,对于指挥官gradle clean bar
,您将获得输出:
foo init line
bar init line
:clean
:foo
foo doFirst
foo doLast
:bar
bar doFirst
bar doLast
{1}}步骤在init步骤之后进行,因此在您的情况下,clean
会在尝试找到之前被删除。