gradle项目生成目录不存在

gradle项目生成目录不存在,gradle,war,output-directory,Gradle,War,Output Directory,正在尝试创建属性文件(foo.properties)并将其添加到war的根目录 apply plugin: 'war' task createProperties { FileOutputStream os = new FileOutputStream("${project.buildDir}/foo.properties"); ... } war { dependsOn createProperties from "${project.buildDir}

正在尝试创建属性文件(foo.properties)并将其添加到war的根目录

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)
我需要创建构建目录吗

对于war,是否有webapp的输出目录?(源集:src/main/webapp) 最好直接在webapp outputDir下创建
foo.properties

尝试如下:

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'
    }
}
现在,对于commande
gradle clean bar
,您将得到如下结果:

foo init line
bar init line
:clean
:foo
foo doFirst
foo doLast
:bar
bar doFirst
bar doLast
clean
步骤在init步骤之后执行,因此在您的情况下,在尝试查找之前会擦除
foo.properties

尝试以下操作:

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'
    }
}
现在,对于commande
gradle clean bar
,您将得到如下结果:

foo init line
bar init line
:clean
:foo
foo doFirst
foo doLast
:bar
bar doFirst
bar doLast
clean
步骤在init步骤之后,因此在您的情况下,
foo.properties
在尝试查找之前被擦除。

您应该这样做

 war {
      from createProperties
      ...
 }
这将自动在createProperties任务上添加一个隐式依赖项,因此不需要依赖项

要使其正常工作,您需要清楚地指定
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'
}
如果您的属性是动态计算的,而不是静态计算的(我假设,否则您可以简单地手动创建文件),那么您还应该将动态部分设置为任务的输入,以便任务的最新检查正常工作,并且任务仅在必要时运行,因为某些输入发生了更改。

您应该这样做

 war {
      from createProperties
      ...
 }
这将自动在createProperties任务上添加一个隐式依赖项,因此不需要依赖项

要使其正常工作,您需要清楚地指定
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'
}

如果您的属性是动态计算的,而不是静态计算的(我假设,否则您可以简单地手动创建文件),那么您还应该将动态部分设置为任务的输入,因此,任务的最新检查工作正常,并且任务仅在必要时运行,因为某些输入已更改。

是否$project.buildDir与此分区中的$buildDir不相同?相同的范围,相同的值,
项目。
可以省略,因为$project.buildDir与此分区中的$buildDir不相同?相同的范围,相同的值,
项目。
在此可以省略$buildDir和${project.buildDir}之间的区别是什么?谢谢。没有了,你不需要限定通话的时间。您也可以使用
${project.buildDir}
,它们在这个上下文中是完全相同的。$buildDir和${project.buildDir}之间的区别是什么?谢谢。没有了,你不需要限定通话的时间。您也可以使用
${project.buildDir}
,它们在这个上下文中是完全相同的。