How can I merge several files extracted from jars into one


(richard murray) #1

Hi, in our application JAR file we have a spring.schemas file in the META-INF directory. The spring.schemas file has been manually created by copying the contents of similar files in several Spring JAR dependencies. I thought it would be better to generate this file during the build e.g.

configurations {
 springSchemas
 springBeans
 springContext
}
  dependencies {
               springSchemas 'org.springframework:spring-beans:3.2.2.RELEASE'
    springSchemas 'org.springframework:spring-context:3.2.2.RELEASE'
          springBeans ('org.springframework:spring-beans:3.2.2.RELEASE') {
     transitive = false
    }
          springContext ('org.springframework:spring-context:3.2.2.RELEASE') {
      transitive = false
    }
}
  jar {
            from({ zipTree(configurations.springBeans.singleFile) }) {
  include 'META-INF/spring.schemas'
    }
    from({ zipTree(configurations.springContext.singleFile) }) {
  include 'META-INF/spring.schemas'
    }
    //from (zipTree (configurations.springSchemas.files { it.name.startsWith('spring') }) ){
 // include '**/spring.schemas'
 //}
 }

This results in 2 spring.schemas files in my JAR. Is there any easy way I can merge these 2 files? Also I’d like to use a single configuration (see the commented out code) but I couldn’t get this to work.

Thanks.

FYI I’m running Gradle 1.5.


(richard murray) #2

I managed to solve this with the following code. I just had to use a bit of groovy to create a new file and read the contents of the other files into it. If there’s an easier/better way let me know (as I’m not that familiar with Groovy)

configurations {
 springSchemas
 }
  dependencies {
               springSchemas 'org.springframework:spring-beans:3.2.2.RELEASE'
    springSchemas 'org.springframework:spring-context:3.2.2.RELEASE'
       }
  def springSchemasFile = new File("$buildDir/spring.schemas")
  task createSchemaFile << {
     def files = configurations.springSchemas.files { it.name.startsWith('spring') }
 files.each { File file ->
     def zipFile = new java.util.zip.ZipFile(file)
     zipFile.entries().findAll { it.name =~ "spring.schemas" }.each {
         springSchemasFile << zipFile.getInputStream(it).text
  }
     }
 }
  jar {
    metaInf {
        from files(springSchemasFile)
    }
    }
  jar.dependsOn createSchemaFile