Gradle - 複数の起動スクリプトの例

Gradle –複数の起動スクリプトの例

複数の開始スクリプトまたは実行可能なJavaアプリケーションを作成する方法を示すいくつかのbuild.gradleの例。

1. シングルスタートスクリプト

1.1 In Gradle, you can use the application plugin to create an executable Java application :

build.gradle

apply plugin: 'application'

mainClassName = "com.example.analyzer.run.threads.MainRunApp"

applicationName = 'mainApp'

applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1024m"]

1.2 Create the executable Java application with gradle distZip command.

$ gradle :analyzer:distZip

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:startScripts
:distZip

1.3 A zip file will be created in $project/build/distributions/xxx.zip

$project/build/distributions/project.zip
--- mainApp     #*nix     -> com.example.analyzer.run.threads.MainRunApp
--- mainApp.bat #windows    -> com.example.analyzer.run.threads.MainRunApp

P.S Alternatively, try gradle distTar to create a Tar file.

2. 複数の起動スクリプト

2.1 To create multiple start scripts, create a custom CreateStartScripts type :

build.gradle

apply plugin: 'application'

mainClassName = "com.example.analyzer.run.threads.MainRunApp"

applicationName = 'mainApp'

applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1024m"]

task createExtraRunApp(type: CreateStartScripts) {
    mainClassName = "com.example.analyzer.run.UpdateHostingExtraRunApp"
    classpath = startScripts.classpath
    outputDir = startScripts.outputDir
    applicationName = 'hostingExtraApp'
    defaultJvmOpts = ["-Xms1024m", "-Xmx2048m"]
}

applicationDistribution.into("bin") {
    duplicatesStrategy= DuplicatesStrategy.EXCLUDE
    from(createExtraRunApp)
    fileMode = 0755
}

2.2 This script will create two executable Java applications :

$project/build/distributions/project.zip
---mainApp              -> com.example.analyzer.run.threads.MainRunApp
---mainApp.bat          -> com.example.analyzer.run.threads.MainRunApp
---hostingExtraApp      -> com.example.analyzer.run.UpdateHostingExtraRunApp
---hostingExtraApp.bat  -> com.example.analyzer.run.UpdateHostingExtraRunApp

3. 複数の起動スクリプト–再び

3.1 This build.gradle is using in my project, it creates three executable Java application :

build.gradle

apply plugin: 'application'
mainClassName = "com.hostingcompass.analyzer.run.threads.HydraRunApp"
applicationName = 'hydra'

applicationDefaultJvmArgs = ["-Dlogback.configurationFile=MY_APP_HOME/logback-hydra.xml",
                             "-Dconfig=MY_APP_HOME/config.properties",
                            "-Djava.net.preferIPv4Stack=true", "-Dapp.home=MY_APP_HOME/", "-Xms512m", "-Xmx1024m"]

startScripts {
    doLast {
        unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME')
        windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..')
    }
}

task updateHostingExtraRunApp(type: CreateStartScripts) {
    mainClassName = "com.hostingcompass.analyzer.run.UpdateHostingExtraRunApp"
    classpath = startScripts.classpath
    outputDir = startScripts.outputDir
    applicationName = 'hostingExtra'
    defaultJvmOpts = ["-Dlogback.configurationFile=MY_APP_HOME/logback-hosting-extra.xml",
                      "-Dconfig=MY_APP_HOME/config.properties", "-Dapp.home=MY_APP_HOME/", "-Xms512m", "-Xmx1024m"]

    updateHostingExtraRunApp {
        doLast {
            unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME')
            windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..')
        }
    }
}

task updateWhoisExtraRunApp(type: CreateStartScripts) {
    mainClassName = "com.hostingcompass.analyzer.run.UpdateWhoisExtraRunApp"
    classpath = startScripts.classpath
    outputDir = startScripts.outputDir
    applicationName = 'whoisExtra'
    defaultJvmOpts = ["-Dlogback.configurationFile=MY_APP_HOME/logback-whois-extra.xml",
                      "-Dconfig=MY_APP_HOME/config.properties", "-Dapp.home=MY_APP_HOME/", "-Xms512m", "-Xmx1024m"]

    updateWhoisExtraRunApp {
        doLast {
            unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME')
            windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..')
        }
    }
}

applicationDistribution.into("bin") {
    duplicatesStrategy= DuplicatesStrategy.EXCLUDE
    from(updateHostingExtraRunApp)
    from(updateWhoisExtraRunApp)
    fileMode = 0755
}

distZip {
    archiveName 'analyzer.zip'
}

3.2 Output

$project/build/distributions/analyzer.zip
---hydra
---hydra.bat
---hostingExtra
---hostingExtra.bat
---whoisExtra
---whoisExtra.bat

Note
開始スクリプトの重複を避けるために、除外duplicatesStrategyを宣言することを忘れないでください。

applicationDistribution.into("bin") {
    duplicatesStrategy= DuplicatesStrategy.EXCLUDE
    from(updateHostingExtraRunApp)
    from(updateWhoisExtraRunApp)
    fileMode = 0755
}