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フォルダとともに配布する必要がある点に注意。


関連リンク

2013年7月3日水曜日

Gradleを使ってEclipseとEclipse Pluginをインストール

2014.02.04 追記:Eclipse 4.3対応のスクリプトは、『dev-xconnecting: Gradleを使ってEclipse 4.3+Groovy Eclipse+Gradle IDEをインストール』を参照。

Gradleを使ってEclipse本体とEclipseプラグイン(フィーチャー)をインストールするためのbuild.gradleは、以下の通り。なお、動作確認はWindows上で行っているが、適宜書き換えればUnix系のOSでも動くはず。

project.ext.tempDir = 'tmp'
project.ext.targetEclipseDir = 'd:/eclipse'
ant.condition(property: "os", value: "windows") { os(family: "windows") }
ant.condition(property: "os", value: "unix" ) { os(family: "unix") }
task clean << { delete 'tmp' }
task installEclipse << {
new File(tempDir).mkdirs()
ant.get(dest:tempDir, skipexisting:true){
url(url:"http://ftp.jaist.ac.jp/pub/eclipse/technology/epp/downloads/release/juno/SR2/eclipse-rcp-juno-SR2-win32.zip")
}
ant.unzip(dest:tempDir, overwrite:"false"){
fileset(dir: project.tempDir){ include (name:'*.zip')}
}
ant.move(toDir: project.targetEclipseDir){
fileset(dir: "${tempDir}/eclipse")
}
ant.copy(file:'files/eclipse.ini', toDir: targetEclipseDir)
}
def getCommandList(String command){
def commandList = [
"${targetEclipseDir}/eclipse",
"-application",
"org.eclipse.equinox.p2.director",
"-noSplash"
]+
command.replaceAll(/,[^\\]\n/, ',').replaceAll(/[^\\]\n/, ' ').split(/\s+/) +
[
"-vmargs",
"-Dlogback.configurationFile=logback.xml"
]
if(ant.properties.os == 'windows'){
commandList = ['cmd', '/c']+ commandList
}
commandList.flatten()
}
task installPlugins(type:Exec) {
ext.command = """\
-repository http://dist.springsource.org/release/GRECLIPSE/e4.2/,^
http://dist.springsource.com/release/TOOLS/gradle^
-installIUs org.codehaus.groovy.eclipse.feature.feature.group,^
org.springsource.ide.eclipse.gradle.feature.feature.group^
-d ${targetEclipseDir}"""
commandLine getCommandList(command)
}
installPlugins.mustRunAfter installEclipse
task wrapper(type: Wrapper) { gradleVersion = '1.6' }
view raw build.gradle hosted with ❤ by GitHub

2行目で、Eclipseのインストール先ディレクトリを指定している。

Eclipse本体のインストールは、installEcliseタスク(9行目)で行っている。単にEclipseのダウンロードサイトにあるアーカイブをダウンロード(11行目)して解凍しているだけ。必要なら、インストールしたいEclipseのバージョンや対象プラットフォームに合わせて適宜書き換える。eclipse.iniの置き換え(20行目)はお好みで。

Eclipseプラグインのインストールには、上記でダウンロードしたEclipse本体に含まれるp2 director (org.eclipse.equinox.p2.director) を利用する(27行目)。プラグインをインストールする際に、やたら細かいDebugログが出力されるのを抑止するために、logbackの設定ファイルを指定している(33行目)。

インストールするEclipseプラグインは、p2 directorのコマンドオプションとして指定する。p2 directorを使ったEclipseプラグインのインストールについては、下記リンクを参照。


installPluginsタスク(42行目)で、インストールに必要な更新サイトをp2 directorのrepositoryオプション(44, 45行目)に、インストール対象のプラグイン(フィーチャー)をinstallIUsオプション(46, 47行目)に指定している。

repositoryオプションおよびinstallIUsオプションでは、複数の更新サイト、プラグインをカンマ区切りで指定できるが、途中にスペースを入れると実行時エラーとなる点に注意。なお、インストールするプラグインがひとつの場合は、installIUsの代わりにinstallIUオプションを使用する。オプションの詳細については、下記を参照(ただし、installIUsについては記載がない)。


上記のbuild.gradleや関連するファイル(eclipse.iniやlogback.xml)を、任意のディレクトリに以下のように配置する。
build.gradle
logback.xml
│
└─files
     └─eclipse.ini
build.gradleが含まれるディレクトリで以下のコマンドを実行すれば、build.gradleで指定したEclipse本体とEclipseプラグイン(上記の例では、Groovy-EclipseとGradle IDE)がインストールされるはず。
gradle installEclipse installPlugins
なお、mustRunAfter(53行目)は、タスクを直列実行するためにGradle 1.6から追加された機能なので、Gradle 1.5以前で実行する場合は、depandsOnなどに書き換える。

上記ファイルをgradlewとともにgitリポジトリなどに公開しておけば、幸せになれそう。

関連リンク

2013年7月1日月曜日

Gradle Flyway PluginでGradleからGroovy Migration

FlywayのGroovy MigrationをGradleで試す』では、build.gradleからFlywayのクラスを直接呼び出しているが、Gradle Flyway Pluginを使うと、より簡単にGradleからFlywayを実行できる。

前提

手順

FlywayのGroovy MigrationをGradleで試す』のbuild.gradleを、以下のように書き換える。

apply plugin: 'groovy'
apply plugin: 'flyway'
repositories { mavenCentral() }
dependencies {
compile localGroovy()
compile 'com.googlecode.flyway:flyway-core:2.1.1'
}
buildscript {
repositories { mavenCentral() }
dependencies {
classpath 'com.h2database:h2:1.3.171'
classpath 'com.github.ben-manes:gradle-flyway-plugin:0.6'
}
}
flyway {
dependsOnTasks(compileGroovy)
databases {
main {
url = "jdbc:h2:file:sampledb"
driver = 'org.h2.Driver'
user = 'SA'
password = ''
locations = [
'classpath:db.migration',
"filesystem:${projectDir}/build/classes/main"
]
}
}
}
task wrapper(type: Wrapper) { gradleVersion = '1.6' }
view raw build.gradle hosted with ❤ by GitHub

flywayタスクの設定で、Groovy Migrationに使用するGroovyクラスのパッケージ名(29行目)、およびGroovyクラスのクラスパス(30行目)を指定するのがポイント。

このbuild.gradleにより、flywayMigrateなど、Gradle Flyway Pluginで提供されているタスクが実行できる。使用可能なタスクは、gradle tasksで確認できる。