Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle tar task not executed

Tags:

gradle

tar

I am writing a Gradle build for a non-java project for assembling existing directories and tar archives into a .tar.gz The tar task skips if I use the definition like so:

task archive(dependsOn: 'initArchive',type: Tar) << {
    baseName = project.Name
    destinationDir = new File(project.buildDir.path+'/installer')
    compression = Compression.GZIP
    from (archiveDir)
    doLast{
        checksum(archivePath)
    }
}

here's the console output

:jenkins-maven-sonar:archive
Skipping task ':jenkins-maven-sonar:archive' as it has no source files.
:jenkins-maven-sonar:archive UP-TO-DATE

BUILD SUCCESSFUL

Total time: 9.056 secs

When I try to use tar task as a method it fails complaining cannot find method

task archive(dependsOn: 'initArchive') << {
    tar{
    baseName = project.Name
    destinationDir = new File(project.buildDir.path+'/installer')
    compression = Compression.GZIP
    from (archiveDir)
    doLast{
        checksum(archivePath)
    }
    }
}

FAILURE: Build failed with an exception.

* Where:
Build file '/home/anadi/Code/da-ci-installers/build.gradle' line: 29

* What went wrong:
Execution failed for task ':jenkins-maven-sonar:archive'.
> Could not find method tar() for arguments [build_6a2bckppv2tk8qodr6lkg5tqft$_run_closure3_closure5_closure7@4a5f634c] on task ':jenkins-maven-sonar:archive'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED

Total time: 8.749 secs

Can we run the tar task in same way as Gradle allows running copy? In the same build I have a block like follows and I want to know if tar can used in the same way

            copy {
                project.logger.info("Copying bundle :: "+bundle[x])
                from(rootProject.projectDir.path+"/3rd-party-tools/"+bundle[x]) {
                    include '**/*.*'
                }
                into(archiveDir)
           }

if not how to make sure my build does not "skip tar" task if using the first form described above.

like image 455
Anadi Misra Avatar asked Jul 19 '12 10:07

Anadi Misra


1 Answers

You have fallen for the classical mistake of configuring a task in the execution phase rather than the configuration phase. The solution is to remove the << in the first code snippet.

If you find the << (and the difference it makes) confusing, a good solution is to never use << but always the more explicit doLast {}.

There is no tar method, but it's usually better to make these things a separate task anyway. (Methods like copy should only be preferred over the corresponding task if there is a strong reason.)

like image 124
Peter Niederwieser Avatar answered Oct 26 '22 19:10

Peter Niederwieser