Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

buildr: package dependencies into a single jar

I have a java project that is built with buildr and that has some external dependencies:

repositories.remote << "http://www.ibiblio.org/maven2"
repositories.remote << "http://packages.example/"

define "myproject" do
  compile.options.target = '1.5'
  project.version = "1.0.0"
  compile.with 'dependency:dependency-xy:jar:1.2.3'
  compile.with 'dependency2:dependency2:jar:4.5.6'

  package(:jar)
end

I want this to build a single standalone jar file that includes all these dependencies.

How do I do that?

(there's a logical followup question: How can I strip all the unused code from the included dependencies and only package the classes I actually use?)

like image 394
levinalex Avatar asked Aug 14 '09 18:08

levinalex


2 Answers

This is what I'm doing right now. This uses autojar to pull only the necessary dependencies:

def add_dependencies(pkg)
  tempfile = pkg.to_s.sub(/.jar$/, "-without-dependencies.jar")
  mv pkg.to_s, tempfile

  dependencies = compile.dependencies.map { |d| "-c #{d}"}.join(" ")
  sh "java -jar tools/autojar.jar -baev -o #{pkg} #{dependencies} #{tempfile}"
end

and later:

package(:jar)
package(:jar).enhance { |pkg| pkg.enhance { |pkg| add_dependencies(pkg) }}

(caveat: I know little about buildr, this could be totally the wrong approach. It works for me, though)

like image 83
levinalex Avatar answered Oct 23 '22 15:10

levinalex


I'm also learning Buildr and currently I'm packing Scala runtime with my application this way:

package(:jar).with(:manifest => _('src/MANIFEST.MF')).exclude('.scala-deps')
  .merge('/var/local/scala/lib/scala-library.jar')

No idea if this is inferior to autojar (comments are welcome), but seems to work with a simple example. Takes 4.5 minutes to package that scala-library.jar thought.

like image 44
user272735 Avatar answered Oct 23 '22 15:10

user272735