Skip to content

Instantly share code, notes, and snippets.

@ligun
Last active November 20, 2015 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ligun/1294b32ddf6fbdabfce3 to your computer and use it in GitHub Desktop.
Save ligun/1294b32ddf6fbdabfce3 to your computer and use it in GitHub Desktop.
コマンド形式で実行できるJarファイルを作成できる
apply plugin: 'java'
apply plugin: 'application'
// プロジェクト設定
archivesBaseName = 'projectName'
group = 'packagename'
mainClassName = 'packagename.MainClass'
/*
依存Jarを全部突っ込んだJarファイルを
作成してシェルスクリプトをくっつけるタスク
*/
task jarsh(type: Jar, dependsOn: jar) {
baseName = "${project.name}-fullpack" //作成するJarファイルの名前
//マニフェストに記載する内容を指定
manifest {
attributes 'Main-Class': mainClassName //メインクラスを指定
}
//依存ライブラリをすべて固める
from configurations.compile.collect{ it.isDirectory()? it : zipTree(it) }
from "$buildDir/classes/main"
from "$buildDir/resources/main"
//タスクの最後に実行
doLast {
def jarshDir = new File("${buildDir}/jarsh")
jarshDir.mkdir()
def shell = new File("${jarshDir}/${archivesBaseName}")
def bat = new File("${jarshDir}/${archivesBaseName}.bat")
def jar = new File("${buildDir}/libs/${baseName}-${version}.jar")
//Unixシェルのブートストラップ
shell.text = """\
#!/bin/sh
java -jar "\$0" "\$@"
exit \$?
"""
//Jarをくっつける
shell << jar.bytes
//Windowsバッチのブートストラップ
bat.text = """\
@echo off
java -jar "%0" %*\r\n\
exit /B 1\r\n\
"""
//Jarをくっつける
bat << jar.bytes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment