buildr:将依赖项打包到单个 jar 中

发布于 2024-08-02 03:12:27 字数 706 浏览 7 评论 0原文

我有一个使用 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 文件。

我怎么做?

(有一个合乎逻辑的后续问题: 我怎样才能删除所有包含的依赖项中未使用的代码,仅打包我实际使用的类?)

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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傻比既视感 2024-08-09 03:12:27

这就是我现在正在做的事情。 这使用 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) }}

(警告:我对构建器知之甚少,这可能是完全错误的方法。不过,它对我有用)

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)

夏见 2024-08-09 03:12:27

我也在学习 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 认为的内容。

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.

卖梦商人 2024-08-09 03:12:27

我将使用 Cascading 作为示例:

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

I'm going to use Cascading for my example:

cascading_dev_jars = Dir[_("#{ENV["CASCADING_HOME"]}/build/cascading-{core,xml}-*.jar")]
#...
package(:jar).include cascading_dev_jars, :path => "lib"
只是一片海 2024-08-09 03:12:27

以下是我如何使用 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

还有一个版本 通过连接所有spring.schemas来支持Spring

Here is how I create an Uberjar with Buildr, this customization of what is put into the Jar and how the Manifest is created:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文