Skip to content

Instantly share code, notes, and snippets.

@ywjno
Created June 17, 2014 04:09
Show Gist options
  • Save ywjno/77b7e6e73f5443309cdd to your computer and use it in GitHub Desktop.
Save ywjno/77b7e6e73f5443309cdd to your computer and use it in GitHub Desktop.
build_jar
# encoding: utf-8
require "json"
require "fileutils"
conf = JSON.load(File.open(File.expand_path("../conf.json", __FILE__), 'r'))
tmp_path = conf['tmp_path'] || File.expand_path("../tmp", __FILE__)
FileUtils.rm_rf(tmp_path) if File.exist?(tmp_path)
FileUtils.mkdir_p(tmp_path)
conf['projects'].map do |project|
if project['is_maven']
FileUtils.cp_r("#{project['path']}/target/classes/.", tmp_path)
else
FileUtils.cp_r("#{project['path']}/bin/.", tmp_path)
end
end
if conf['main_class']
FileUtils.mkdir_p("#{tmp_path}/META-INF")
File.open("#{tmp_path}/META-INF/MANIFEST.MF", 'w') do |file|
file.puts 'Manifest-Version: 1.0'
file.puts "Main-Class: #{conf['main_class']}"
file.puts
end
end
require 'zip'
jar_name = conf['jar_name'] || 'jar_name'
build_path = conf['build_path'] || File.dirname(__FILE__)
jar_file = "#{build_path}/#{jar_name}.jar"
File.delete jar_file if File.exist?(jar_file)
Zip::File.open(jar_file, Zip::File::CREATE) do |zipfile|
Dir[File.join(tmp_path, '**', '**')].each do |file|
zipfile.add(file.sub(tmp_path, '')[1..-1], file)
end
end
FileUtils.rm_rf(tmp_path) if File.exist?(tmp_path)
{
"projects": [
{
"path": "D:/nutzEclipse370/workspace/build-jar"
},
{
"path": "D:/github/nutz",
"is_maven": true
},
{
"path": "D:/github/mongo-java-driver",
"is_maven": true
},
{
"path": "D:/nutzEclipse370/workspace/nutzmongo"
}
],
"main_class": "com.nutz.extension.build.BuildJar",
"jar_name": "build_jar"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment