Top > Blog Index > Gradle で 実行可能(executable) jar をつくる場合 と コマンドライン処理

Gradle で 実行可能(executable) jar をつくる場合 と コマンドライン処理

ターミナルで使用するCUIなツールをつくる場合、コマンドラインオプションの処理は必須です。 groovy は Jakarta Commons CLI を使った コマンドラインオプション処理を簡単できるようになっているのが便利だが、 gradle で executable jar をビルドするときに適切に commons-cli を依存する jar として指定しておかないと java -jar foo.jar で実行できる jar を生成できない・・・という 問題にいつも直面しているのでメモ。

ロングオプションが未サポートっぽい

--debug のような長い名前のオプションが使えないと思う。 最新の groovy では修正されているかもしれないが...

コマンドラインオプションを処理する groovy

groovy のコマンドラインオプション処理は簡単に書けるのだが、 -i でインプットファイル -o でアウトプットファイルを指定、 というパターンが多いので、それを毎回書かないですますための便利クラス。

CmdProc.groovy

class CmdProc {

        def args
        def cli 
        def CmdProc( def args ){
                this.args = args

                cli = new CliBuilder()
                cli.i(argName:'input',    required:true  ,args:1 , 'input file')
                cli.o(argName:'output',   required:true  ,args:1 , 'output file')
        }   
        void usage(){
                cli.usage()
        }   

        private def inputf = null
        def getInputFile() throws CmdProcException{
                parse()
                inputf
        }   
        private def outputf = null
        def getOutputFile() throws CmdProcException{
                parse()
                outputf
        }   

        private boolean 処理済み
        private void parse() throws CmdProcException{
                if( 処理済み )
                        return

                def options = null
                try{
                        options=cli.parse(args)

                        if( options.i )
                                inputf  = new File(options.i)

                        if( options.o )
                                outputf = new File(options.o)
                }   
                catch(Exception ex){
                        throw new CmdProcException()
                }   

                処理済み = true
        }   
}
class CmdProcException extends Exception{
}

main.groovy

CmdProc.groovy を使用する例。

def inputfile  = null
def outputfile = null

try{
    def cmdp = new CmdProc(args)
    inputfile  = cmdp.inputFile
    outputfile = cmdp.outputFile
}
catch(Exception e){
    System.exit(0)
}

gradle で execultable jar に変換

例によって gradle のお約束のディレクトリレイアウトにファイルを配置。

myproject.jar をビルド。

$ cd myproject
$ gradle

結果は myproject/build/libs/myproject.jar に出力される。 jarのファイル名は、プロジェクトのルートディレクトリ名と同じになるので注意。

build.gradle

//
// executable jar を生成する build ファイル. (Gradle ,version 0.9)
//

defaultTasks 'build'
//defaultTasks 'build','javadoc','groovydoc'


//apply plugin: 'java'
apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.7.0'
    groovy group: 'commons-cli', name : 'commons-cli', version: '1.2'
}


//
// 以降は
// executable jar をつくる場合に必要な記述が必要です
//

jar {
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

manifest.mainAttributes("Main-Class" : "main")

依存関係で commons-cli を指定しているところがポイント。

groovy group: 'commons-cli', name : 'commons-cli', version: '1.2'