Can you convert a configuration into a fileset?

Can you convert a configuration into a fileset?

I’m using OneJar (by calling the Ant task for OneJar - see below). The fileset’s are picking up jars from a local ‘lib’ directory.

This is working great, but I am in the process of removing jars from local lib directories and using Apache Ivy. So I’d like to

pick up these jars using a configuration e.g. fileset (configurations.oneJarLibs.getAllArtifactFiles())

Is anything like this possible? Or do I need a separate step to copy the JAR’s from the configuration into a local directory?

  ant.taskdef(name: 'oneJar', classname: 'com.simontuffs.onejar.ant.OneJarTask', classpath: "$projectDir/one-jar-ant-task-0.96.jar")



ant.oneJar(destfile: "$buildDir/libs/OrchardOpenAdaptor.jar") {





 main(manifest: "manifest.mf") {







  fileset(dir: sourceSets.main.classesDir)





 }





 lib() {







  fileset( dir: "${project(':uwm/core').libsDir}", includes : "core_client.jar")







  //I want to replace this with a configuration and get the JAR's from Ivy

  fileset( dir: "$rootDir/../uwm/lib", includes: "openadaptor.jar, regexp.jar, commons-net.jar, fscontext.jar")











}



}  

Hi Rich,

For future reference, if you use code instead of pre to wrap code blocks in this forum you will get cool syntax highlighting for the code snippet.

Hi Luke, thanks for the formatting tip.

For anyone interested I couldn’t get the ant fileset to work with the files from a configuration, so I ended up temporarily copying the jars into a directory then using that directory for the fileset.

If anyone has a better way please let me know!

task copyJars(type: Copy) {
       from configurations.oneJarLibs.files
       into 'runtimeLibs'
   }
                 task oneJar(dependsOn: [copyTimestampFile, copyJars]) << {
       ant.taskdef(name: 'oneJar', classname: 'com.simontuffs.onejar.ant.OneJarTask', classpath: "$projectDir/one-jar-ant-task-0.96.jar")
       ant.oneJar(destfile: "$buildDir/libs/OrchardOpenAdaptor.jar") {
           main(manifest: "manifest.mf") {
               fileset(dir: sourceSets.main.classesDir)
           }
                      lib {
               fileset ( dir: "${project(':uwm/core').libsDir}", includes : "core_client.jar")
                                      fileset ( dir: 'runtimeLibs')
                                                            }
       }

This wasn’t your original question, Rich. But I’ve written a onejar plugin for my own purposes that’s shared on github:

https://github.com/curious-attempt-bunny/gradle-onejar

Apart from the github repository declaration (which would be improved by GRADLE-1653) and the apply plugin line, this is all you then need in your build script:

task oneJar(type: OneJar) {
 mainClass = 'com.curiousattemptbunny.onejar.ExampleMain'
}

Adam, thanks! Just what I needed.

Merlyn thanks for the link to your plug-in, I’ll try it out. Can you specify a configuration for the jars or does it just use the runtime configuration by default?

Hey Rich,

The configuration defaults to project.configurations.runtime. You should be able to override the default by setting the “runtime” properly, although I haven’t tested it:

task oneJar(type: OneJar) {
    runtime = configurations.someOtherConfig
    mainClass = 'com.curiousattemptbunny.onejar.ExampleMain'
}

Cheers, Merlyn

Configuration objects are actually FileCollections.

http://gradle.org/current/docs/javadoc/org/gradle/api/artifacts/Configuration.html

So you just need:

task copyJars(type: Copy) {
  from configurations.oneJarLibs
  into 'runtimeLibs'
}

This has some added advantages of using the files property of configuration in that it delays resolution of the configuration until the latest possible time.

Gradle’s FileCollection type, which Configuration extends, have an addToAntBuilder() method:

  ant.someTask {



configurations.someConfig.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)  }  

This will add a ‘fileset’ element of (Ant) type FileSet, which contains the files of the configurations.