How can I add a file from a repository to a zip file?

What is the best solution to add a file from a repository to a zip file? I think like this:

task zip(type: Zip) {
    from repositories( 'mylib.jar' )
    into 'libs'
}

or

task zip(type: Zip) {
    from repositories
    include 'mylib.jar' 
    into 'libs'
}
configurations {
   temp { transitive = false } 
} 
dependencies {
   temp "foo:bar:1.0"
}
task zip(type: Zip) {
    from configurations.temp
    into 'libs'
}

Thanks for the hint. But your solution is to static. We have approx 50 files to add in this way. This will be confused. This is the same more dynamic. It fetch ever the latest version.

ext.resourceFile = { String fileName ->
    def config = configurations.maybeCreate( fileName )
    def notation
    def idx = fileName.lastIndexOf( '.' )
    if( idx > 0 ) {
        notation = fileName.substring( 0, idx) + '@' + fileName.substring( idx + 1 )
    } else {
        notation = fileName + '@'
    }
    def dep = dependencies.add fileName, "lib:" + notation
    return copySpec {
        from config
        rename{ fileName }
    }
}
task zip(type: Zip) {
    with resourceFile( 'mylib.jar' )
    into 'libs'
}

Are these 50 files going into the one zip? If so you could simply add to the dependencies { ... } block in my solution.

This are 50 different “from” expressions because different “into”, “filter” settings. This are not only jar files, also zip files, images, scripts, etc. It is the final build script that accumulate the results from many other build job to a distribution.

@Horcrux, I think @Lance_Java has already given you the clue to this. Remember that the from statement can take many kinds of input. If you are going to copy from another subproject within your project then doing from project(':foo') shoudld o it to get the artifacts from that project.

However if I assume what you mean by repository is what we would understand as a repository in Gradle, you can also do things like

from configurations.CONFIG_NAME.files, {
  include '**/foo.jar'
}

which would hopefully avoid the need for 50 from declarations.

Just to be clear: in Gradle terms repositories are external stores (including local filesystems) where you can obtain artifacts from, using (mostly) maven coordinates. Configurations are where you actually tell Gradle what you need.

@Schalk_Cronje, With my function resourceFile I have solve my problem. The problem with your solution is:

  • I need to declare the file names 2 times.
  • The name is changed. For example to foo_2.2.jar. I need to add a rename. This means I must declare the file name 3 times. The file names with version broke many links. This would require to change hundreds of files every day if a version change.

I’m glad you solved it. You resourceFile is a neat little closre.