天天看點

将依賴的jar包inlining到另外一個jar包的ant插件

https://code.google.com/p/jarjar/

Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution. This is useful for two reasons:

  • You can easily ship a single jar file with no external dependencies.
  • You can avoid problems where your library depends on a specific version of a library, which may conflict with the dependencies of another library.

spring中https://github.com/spring-projects/spring-framework/blob/3.2.x/build.gradle建構spring-core包,将asm和cglib包inling到spring-core包中。

         task asmRepackJar ( type: Jar ) { repackJar ->                  repackJar . baseName = "spring-asm-repack"                  repackJar . version = asmVersion                    doLast () {                          project . ant {                                  taskdef name: "jarjar" , classname: "com.tonicsystems.jarjar.JarJarTask" ,                                          classpath: configurations . jarjar . asPath                                  jarjar ( destfile: repackJar . archivePath ) {                                          configurations . asm . each { originalJar ->                                                  zipfileset ( src: originalJar )                                          }                                          rule ( pattern: "org.objectweb.asm.**" , result: "[email protected]" )                                  }                          }                  }          }            task cglibRepackJar ( type: Jar ) { repackJar ->                  repackJar . baseName = "spring-cglib-repack"                  repackJar . version = cglibVersion                    doLast () {                          project . ant {                                  taskdef name: "jarjar" , classname: "com.tonicsystems.jarjar.JarJarTask" ,                                          classpath: configurations . jarjar . asPath                                  jarjar ( destfile: repackJar . archivePath ) {                                          configurations . cglib . each { originalJar ->                                                  zipfileset ( src: originalJar )                                          }                                          // repackage net.sf.cglib => org.springframework.cglib                                          rule ( pattern: "net.sf.cglib.**" , result: "[email protected]" )                                          // as mentioned above, transform cglib"s internal asm dependencies from                                          // org.objectweb.asm => org.springframework.asm. Doing this counts on the                                          // the fact that Spring and cglib depend on the same version of asm!                                          rule ( pattern: "org.objectweb.asm.**" , result: "[email protected]" )                                  }                          }                  }          }