Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Application Plugin - Building multiple start scripts / dists for the same project with different mainClassName

Tags:

gradle

We have a Gradle project using the java plugin that has a couple of command line tools it needs to build. The project is just packaged up into a jar with its dependencies. We'd then like a couple of start scripts to kick off the various entry points in that project for each of these tools.

Naturally the application plugin is a good choice. And so we changed java to application and provided a mainClassName to create start scripts and tar distributable's. This worked to create a single application jar, but only one set of start scripts that used the mainClassName specified.

How can we create multiple start scripts for different entry points? (different mainClassName's?)

One approach I tried was creating some subprojects that had the application plugin applied and specified the individual mainClassNames seperatately

allprojects {
    apply plugin: 'java'

    repositories { 
        // maven repos           
    }

    dependencies {
        compile 'com.thirdparty:somejar:1.0'
    }

    sourceCompatibility = 1.7
}


subprojects {
    apply plugin: 'application'
}


project(':tools:csvLoader') {
    mainClassName = 'com.demo.tools.csvLoader.Loader'
}

project(':tools:summariser') {
    mainClassName = 'com.demo.tools.summary.Summarise'
}

And referenced in the root projects settings.gradle

include "tools","tools:csvLoader","tools:summariser"

This worked - but each subproject creates an identical jar (just named with the name of the subproject) and each subdir build folder holds a copy of that jar plus another copy of all the dependencies. Thats feels a little wasteful. It could also be confusing to a new developer seeing the subprojects there with all these tasks and no code whatsoever.

Is there a better way go about telling gradle to make multiple application related tasks but changing the mainClassName for each without having to resort to creating empty subprojects?

Thanks!

like image 815
neversleepz Avatar asked Dec 23 '13 03:12

neversleepz


People also ask

Can I have multiple build Gradle?

Java Prime Pack Small projects have a single build file and a source tree. It is very easy to digest and understand a project that has been split into smaller, inter-dependent modules. Gradle perfectly supports this scenario that is multi-project build.

What is multi module Gradle project?

A multi-project build in Gradle consists of one root project, and one or more subprojects. A basic multi-project build contains a root project and a single subproject. This is a structure of a multi-project build that contains a single subproject called app : Example 1. Basic multi-project build.

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, binary plugins and script plugins.


2 Answers

Here's one way to generate multiple start scripts with Gradle's application plugin:

mainClassName = "com.example.PrimaryEntryPoint"

startScripts {
  applicationName = 'primary'
}

// For convenience, define a map of additional start scripts.
// Key is script name and value is Java class.
def extraStartScripts = [
    'secondary' : 'com.example.SecondaryEntryPoint',
    'tertiary'  : 'com.example.TertiaryEntryPoint'
]

// Create a task for each additional entry point script.
extraStartScripts.each { scriptName, driverClass ->
  Task t = task(scriptName + "-script",
       group: 'CLI Script Generation',
       type: CreateStartScripts) {
    mainClassName = driverClass
    applicationName = scriptName
    outputDir = startScripts.outputDir
    classpath = startScripts.classpath
  }
  startScripts.dependsOn(t)
}
like image 75
dnault Avatar answered Sep 25 '22 15:09

dnault


You can always drop down to the task level and declare/configure/wire the necessary tasks yourself, or declare additional tasks on top of what apply plugin: "application" provides. See the application plugin chapter in the Gradle user guide for which related tasks are available.

like image 21
Peter Niederwieser Avatar answered Sep 22 '22 15:09

Peter Niederwieser