它生成后的gradle - copy文件 [英] gradle - copy file after its generation

查看:418
本文介绍了它生成后的gradle - copy文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试构建jar,然后将其复制到另一个文件夹。

 任务createJar(type:Jar){
archiveName =GradleJarProject.jar
manifest {
attributes'Implementation-Title':'Gradle Jar File Example',
'Implementation-Version ':版本,
'Main-Class':'me.test.Test'
}
baseName = project.name $ b $ from {configurations.compile.collect {it.isDirectory ()?它:zipTree(it)}}
with jar

}

任务copyJarToBin {
copy'
from'build / libs / GradleJarProject.jar'
到'd:/ tmp
}
}

任务buildApp(dependsOn:[clean,createJar,copyJarToBin])

但是我找不出一个问题。
copyJarToBin任务尝试复制旧的jar。如果我在项目中删除/构建文件夹并运行buildApp()任务,任务createJar()将生成.jar文件,但copyJarToBin()不会找到该.jar文件。



你能帮助我吗?

谢谢。

解决方案

罪魁祸首是你的 copyJarToBin 任务。在做

 任务copyJarToBin {
copy'$ build'/ libs / GradleJarProject.jar'

转换为d:/ tmp
}
}

你通过使用 copy 方法在配置时间内复制jar。 (请参阅 https://docs.gradle.org/current/userguide/userguide_single上的gradle用户指南.html#sec:build_phases 来理解构建生命周期)
您希望在执行阶段(执行任务)期间运行实际的复制操作。



解决这个问题的一个方法是将 copy 方法的调用移动到doLast块中:

 任务copyJarToBin {
doLast {
copy {
from'build / libs / GradleJarProject.jar'
转换为d:/ tmp
}

}
}

这种方法的问题在于,即使文件没有更改,每次执行任务时,您都不会受益于渐变增量构建功能并复制该文件。

编写copyJarToBin任务的更好更独特的方式是将您的任务实现更改为使用 Copy 任务类型:

 任务copyJarToBin(类型:复制){$'b / b from'build / libs / GradleJarProject.jar'
转换为d:/ tmp
}

我们甚至可以通过利用Gradle的自动装配功能。您可以将一个任务的输出声明为另一个任务的输入。因此,不用编写`build / libs / GradleJarProject.jar',您可以简单地执行:

 任务copyJarToBin(type:Copy){ 
from createJar //快捷方式为createJar.outputs.files
到d:/ tmp
}

现在您不必担心任务排序,因为gradle知道 createJar 任务必须在 copyJarToBin 任务可以执行。



I try to build jar and after that copy it to another folder.

task createJar(type: Jar) {
    archiveName = "GradleJarProject.jar"
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',  
            'Implementation-Version': version,
            'Main-Class': 'me.test.Test'
    }
    baseName = project.name
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar

}

task copyJarToBin {
    copy {
        from 'build/libs/GradleJarProject.jar'
        into "d:/tmp"
    }
}

task buildApp (dependsOn: [clean, createJar, copyJarToBin])

But I can't figure out one problem. copyJarToBin task try to copy old jar. If I delete /build folder in the project and run buildApp() task, task createJar() will generate .jar file, but copyJarToBin() won't find that .jar file.

Could you help me?
Thanks.

解决方案

The culprit is your copyJarToBin task. when doing

task copyJarToBin {
    copy {
        from 'build/libs/GradleJarProject.jar'
        into "d:/tmp"
    }
}

you copy the jar during the configuration time by using the copy method. (see the gradle user guide at https://docs.gradle.org/current/userguide/userguide_single.html#sec:build_phases to understand the build lifecycle) You want to run the actual copy operation during the execution phase (the execution of the task).

One way to solve that is to move the call of the copy method into a doLast block:

task copyJarToBin {
    doLast {
        copy {
            from 'build/libs/GradleJarProject.jar'
            into "d:/tmp"
        }

    }
}

The problem with this approach is that you won't benefit of gradles incremental build feature and copy that file every single time you execute the task even though the file hasn't changed.

A better and more idionmatic way of writing your copyJarToBin task is to change your task implementation to use the Copy task type:

task copyJarToBin(type: Copy) {
    from 'build/libs/GradleJarProject.jar'
    into "d:/tmp"
}   

We can even improve this snippet by taking advantage of gradle's autowiring feature. You can declare the output of one task as input to another. So instead of writing `build/libs/GradleJarProject.jar' you can simply do:

task copyJarToBin(type: Copy) {
    from createJar // shortcut for createJar.outputs.files
    into "d:/tmp"
}   

Now you don't need to bother about task ordering as gradle know that the createJar task must be executed before the copyJarToBin task can be executed.

这篇关于它生成后的gradle - copy文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆