40


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.

wazz
  • 760
  • 2
  • 8
  • 19

5 Answers5

81

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 idiomatic 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.

Daniel Wolf
  • 12,855
  • 13
  • 54
  • 80
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
38

I think the above answer is somehow old. Here is an answer using gradle 3.3

jar {
    baseName = 'my-app-name'
    version =  '0.0.1'
}

task copyJar(type: Copy) {
    from jar // here it automatically reads jar file produced from jar task
    into 'destination-folder'
}

build.dependsOn copyJar
Peter T.
  • 8,757
  • 3
  • 34
  • 32
  • Is there a way how I can update the jar after it was build? I try to use the `jar -uf ...` command in order to append generated files to the jar but this has to happen _after_ the jar was genereated. – Stefan Falk Jan 10 '21 at 08:55
7

Just made few corrections to above Answers:

jar {
    baseName = "$artifactId"
    version =  '0.0.1'
}

task copyJar(type: Copy) {
    from jar // copies output of file produced from jar task
    into 'destination-folder'
}

build.finalizedBy copyJar
Dharman
  • 30,962
  • 25
  • 85
  • 135
Abhijit Patil
  • 136
  • 1
  • 4
2

You probably need to ensure they are run in the right order,

task copyJarToBin(type:Copy,dependsOn:[createJar]) {
   copy {
     from "${buildDir}/GradleJarProject.jar"  // needs to be gstring       
     into "d:/tmp"
    }
}
KIC
  • 5,887
  • 7
  • 58
  • 98
Theresa Forster
  • 1,914
  • 3
  • 19
  • 35
  • using the Copy task is the right direction, but you need to get rid of the `copy` method in order to avoid execution of the copy operation during the execution phase – Rene Groeschke Jun 04 '15 at 07:13
  • @Theresa Forster you're right. copyJarToBin() was invoked before createJar(). I'll use mustRunAfter function. Thanks. – wazz Jun 04 '15 at 07:37
0

In my use case I needed projectA to consume (unzip) contents from projectB's jar output. In this case the embedded doLast { copy { .. }} was required.

configurations {
    consumeJarContents
}
dependencies {
    consumeJarContents project(':projectB')
}

task copyFromJar() {
    dependsOn configurations.consumeJarContents
    doLast {
        copy {
            configurations.consumeJarContents.asFileTree.each {
                from( zipTree(it) )
            }
            into "$buildDir/files"
        }
    }
}

The problem with task copyFromJar(type: Copy) in this scenario is that the Copy configuration phase checks for the jar file, which it does not find because it has not yet been created, and so declares NO-INPUT for the task at execution time.

Andre
  • 390
  • 3
  • 8