执行一个gradle任务

时间:2016-09-06 09:48:49

标签: gradle

我完成了这两项任务:

war {
    webInf { from 'src/main/resources/WEB-INF' }
    manifest {
        attributes 'Implementation-Version': versioning.info.display
    }
}

task createDemoWar(type: War, dependsOn: classes) {
    archiveName "webapi-demo-${versioning.info.display}.war"
    destinationDir = file("$buildDir/dist")
    copy {
        from 'scopes'
        include 'configuration.demo.properties'
        into 'src/main/resources/'
    }
}

task createDevelopmentWar(type: War, dependsOn: classes) {
    archiveName "webapi-dev-${versioning.info.display}.war"
    destinationDir = file("$buildDir/dist")
    copy {
        from 'scopes/'
        include 'configuration.development.properties'
        into 'src/main/resources/'
    }
}

这两项任务都试图将属性文件从scopes文件夹复制到src/main/resources/文件夹。

我只运行一项任务,但在src/main/resources/configuration.demo.propertiesconfiguration.development,properties中复制了两个文件。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您在配置阶段使用两个复制规范(注意,在这种情况下,复制规范不会配置您的任务,但会为其配置添加一些额外的操作)。这意味着,您的文件将在配置期间被复制。这都是因为:

copy {
    ...
}

你必须在执行期间运行它,为此,尝试将其移入doLastdoFirst闭包,就像这样(对于这两个任务):

task createDemoWar(type: War, dependsOn: classes) {
    archiveName "webapi-demo-${versioning.info.display}.war"
    destinationDir = file("$buildDir/dist")
    doFirst {
        copy {
            from 'scopes'
            include 'configuration.demo.properties'
            into 'src/main/resources/'
        }
    }
}

在这种情况下,只有在执行任务之前执行任务才会复制文件。