【问题标题】:gradle project build directory does not existgradle 项目构建目录不存在
【发布时间】:2017-12-03 06:09:08
【问题描述】:

尝试创建属性文件 (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

【问题讨论】:

    标签: gradle war output-directory


    【解决方案1】:

    你应该这样做

     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'
    }
    

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

    【讨论】:

    • $buildDir 和 ${project.buildDir} 有什么区别?谢谢。
    • 没有,您只是不需要限定通话。你也可以使用${project.buildDir},它们在这种情况下是完全一样的。
    【解决方案2】:

    试试这样:

    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,您将得到 otput:

    foo init line
    bar init line
    :clean
    :foo
    foo doFirst
    foo doLast
    :bar
    bar doFirst
    bar doLast
    

    clean 步骤在 init 步骤之后进行,因此在您的情况下,foo.properties 在尝试被发现之前已被删除。

    【讨论】:

    • 这种情况下$project.buildDir和$buildDir不一样吗?
    • 同范围,同值,project.这里可以省略
    猜你喜欢
    • 2013-09-05
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2018-03-29
    • 2014-06-13
    相关资源
    最近更新 更多