Last active
November 20, 2015 08:35
-
-
Save ligun/1294b32ddf6fbdabfce3 to your computer and use it in GitHub Desktop.
コマンド形式で実行できるJarファイルを作成できる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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