简体   繁体   English

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

[英]buildr: package dependencies into a single jar

I have a java project that is built with buildr and that has some external dependencies: 我有一个使用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

I want this to build a single standalone jar file that includes all these dependencies. 我希望这个构建一个包含所有这些依赖项的独立jar文件。

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 ?) (这是一个逻辑上的后续问题: 如何从包含的依赖项中删除所有未使用的代码,只打包我实际使用的类 ?)

This is what I'm doing right now. 这就是我现在正在做的事情。 This uses autojar to pull only the necessary dependencies: 这使用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

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) (警告:我对buildr知之甚少,这可能是完全错误的方法。但它对我有用)

I'm also learning Buildr and currently I'm packing Scala runtime with my application this way: 我也在学习Buildr,目前我用这种方式用我的应用程序打包Scala运行时:

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. 不知道这是否不如autojar(欢迎评论),但似乎只是一个简单的例子。 Takes 4.5 minutes to package that scala-library.jar thought. 需要4.5分钟才能打包scala-library.jar。

Here is how I create an Uberjar with Buildr, this customization of what is put into the Jar and how the Manifest is created: 以下是我使用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

There is also a version that supports Spring , by concatenating all the spring.schemas 通过连接所有spring.schemas ,还有一个支持Spring的版本

I'm going to use Cascading for my example: 我将使用Cascading作为我的例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM