Gradle - Plusieurs exemples de script de démarrage

Gradle - Exemples de scripts de démarrage multiples

Quelques exemplesbuild.gradle pour vous montrer comment créer plusieurs scripts de démarrage ou une application Java exécutable.

1. Script de démarrage unique

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. Scripts de démarrage multiples

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. Script de démarrage multiple - Encore une fois

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
N'oubliez pas de déclarer l'exclusionduplicatesStrategy pour éviter la duplication du script de démarrage.

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