Gradle – Multiple start script examples

Few build.gradle examples to show you how to create multiple start scripts or executable Java application.

1. Single Start Script

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

build.gradle

apply plugin: 'application'

mainClassName = "com.mkyong.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.mkyong.analyzer.run.threads.MainRunApp
--- mainApp.bat #windows	-> com.mkyong.analyzer.run.threads.MainRunApp

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

2. Multiple Start Scripts

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

build.gradle

apply plugin: 'application'

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

applicationName = 'mainApp'

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

task createExtraRunApp(type: CreateStartScripts) {
    mainClassName = "com.mkyong.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.mkyong.analyzer.run.threads.MainRunApp	 
---mainApp.bat			-> com.mkyong.analyzer.run.threads.MainRunApp
---hostingExtraApp		-> com.mkyong.analyzer.run.UpdateHostingExtraRunApp
---hostingExtraApp.bat	-> com.mkyong.analyzer.run.UpdateHostingExtraRunApp

3. Multiple Start Script – Again

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
Remember to declare the exclude duplicatesStrategy to avoid duplicate start script.


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

References

  1. Gradle – The Application Plugin
  2. Gradle Application Plugin – APP_HOME

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar