2013年7月9日火曜日

GradleからJSmoothを呼び出して実行可能jarをexe化する

GradleからJSmoothを呼び出して実行可能jarをexe化する手順は、以下の通り。

前提

  • JSmoothがインストールされている


手順

以下のbuild.gradleを用意する。

apply plugin: 'groovy'
apply plugin: 'eclipse'
archivesBaseName = 'myapp'
ext.jsmoothHome = 'C:/program Files/JSmooth 0.9.9-7'
configurations { jsmooth }
repositories{
flatDir dirs: "${jsmoothHome}/lib"
}
dependencies{
groovy localGroovy()
jsmooth 'net.charabia:jsmoothgen-ant:@jar'
}
jar {
copy {
from configurations.runtime
into "lib"
}
def manifestClasspath = configurations.compile.collect{ 'lib/' + it.getName() }.join(' ')
manifest {
attributes "Main-Class" : "com.example.Main"
attributes 'Class-Path': manifestClasspath
}
from (configurations.compile.resolve().collect { it.isDirectory() ? it : fileTree(it) }) {
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
exclude '**/*.jar'
}
}
build << {
ant.copy(file: 'build/libs/myapp.jar', todir: '.')
}
task jsmooth << {
def basedir = '.'
ant.taskdef(name:'jsmoothgen'
,classpath:configurations.jsmooth.asPath
, classname:"net.charabia.jsmoothgen.ant.JSmoothGen"
)
ant.jsmoothgen(project:"myapp.jsmooth"
,skeletonroot:"${jsmoothHome}/skeletons"
)
}
jsmooth.dependsOn(build)
view raw build.gradle hosted with ❤ by GitHub

6行目に、JSmoothのインストールディレクトリを指定する。JSmoothのAntタスクを呼び出すために、jsmoothという名前のconfigurationを定義し(8行目)、そのconfigurationにJSmoothのantタスクのjarを指定(16行目)する。

jarタスク(19行目)で、依存関係のあるjarをlibフォルダにコピー(20-23行目)した上で、実行可能jarを作成している。実行可能jarの作成については、下記サイトの内容をもとにしている。


jsmoothタスク(42行目)で、JSmoothのAntタスクを呼び出し、実行可能jarをexe化している。Antタスク呼び出すjsmoothの設定ファイルは、あらかじめjsmoothを使って保存しておく必要がある。jsmoothの設定については、下記を参照。


なお、この例で使用したjsmoothの設定ファイルは以下の通り。

<?xml version="1.0" encoding="ISO-8859-1"?>
<jsmoothproject>
<JVMSearchPath>registry</JVMSearchPath>
<JVMSearchPath>javahome</JVMSearchPath>
<JVMSearchPath>jrepath</JVMSearchPath>
<JVMSearchPath>jdkpath</JVMSearchPath>
<JVMSearchPath>exepath</JVMSearchPath>
<JVMSearchPath>jview</JVMSearchPath>
<classPath>lib\groovy-all-1.8.6.jar</classPath>
<embeddedJar>true</embeddedJar>
<executableName>myapp.exe</executableName>
<initialMemoryHeap>-1</initialMemoryHeap>
<jarLocation>myapp.jar</jarLocation>
<mainClassName>com.example.Main</mainClassName>
<maximumMemoryHeap>-1</maximumMemoryHeap>
<maximumVersion></maximumVersion>
<minimumVersion></minimumVersion>
<skeletonName>Console Wrapper</skeletonName>
<skeletonProperties>
<key>Message</key>
<value>This program needs Java to run.
Please download it at http://www.java.com</value>
</skeletonProperties>
<skeletonProperties>
<key>PressKey</key>
<value>0</value>
</skeletonProperties>
<skeletonProperties>
<key>Debug</key>
<value>0</value>
</skeletonProperties>
</jsmoothproject>
view raw myapp.jsmooth hosted with ❤ by GitHub

jsmoothタスクを実行すると、実行可能jarがexe化される。なお、このexeはlibフォルダとともに配布する必要がある点に注意。


関連リンク

0 件のコメント:

コメントを投稿