buildr:将依赖包封装到一个jar中

时间:2009-08-14 18:30:32

标签: java maven-2 jar packaging buildr

我有一个用buildr构建的java项目,它有一些外部依赖项:

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

我希望这个构建一个包含所有这些依赖项的独立jar文件。

我该怎么做?

(有一个合乎逻辑的后续问题:How can I strip all the unused code from the included dependencies and only package the classes I actually use?)

4 个答案:

答案 0 :(得分:8)

这就是我现在正在做的事情。这使用autojar来仅提取必要的依赖项:

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

以后:

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

(警告:我对buildr知之甚少,这可能是完全错误的方法。但它对我有用)

答案 1 :(得分:7)

我也在学习Buildr,目前我用这种方式用我的应用程序打包Scala运行时:

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

不知道这是否不如autojar(评论是受欢迎的),但似乎只是一个简单的例子。需要4.5分钟才能打包scala-library.jar。

答案 2 :(得分:0)

我将使用Cascading作为我的例子:

cascading_dev_jars = Dir[_("#{ENV["CASCADING_HOME"]}/build/cascading-{core,xml}-*.jar")]
#...
package(:jar).include cascading_dev_jars, :path => "lib"

答案 3 :(得分:0)

以下是我如何使用Buildr创建一个Uberjar,这个定制了Jar中的内容以及如何创建Manifest:

assembly_dir = 'target/assembly'
main_class = 'com.something.something.Blah'

artifacts = compile.dependencies

artifacts.each do |artifact|
    Unzip.new( _(assembly_dir) => artifact ).extract
end

# remove dirs from assembly that should not be in uberjar
FileUtils.rm_rf( "#{_(assembly_dir)}/example/package" )
FileUtils.rm_rf( "#{_(assembly_dir)}/example/dir" )

# create manifest file
File.open( _("#{assembly_dir}/META-INF/MANIFEST.MF"), 'w') do |f| 
    f.write("Implementation-Title: Uberjar Example\n")
    f.write("Implementation-Version: #{project_version}\n") 
    f.write("Main-Class: #{main_class}\n")
    f.write("Created-By: Buildr\n")                 
end

present_dir = Dir.pwd
Dir.chdir _(assembly_dir)
puts "Creating #{_("target/#{project.name}-#{project.version}.jar")}" 
`jar -cfm #{_("target/#{project.name}-#{project.version}.jar")} #{_(assembly_dir)}/META-INF/MANIFEST.MF .`
Dir.chdir present_dir

还有一个版本supports Spring,通过连接所有 spring.schemas