Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple start scripts using Gradle

Tags:

maven

gradle

I have a working Maven build (shown below) that prepares a couple of executables to launch as two separate processes.

Although this works fine, how can this be done using Gradle? I see that Gradle provides a plugin called application, but I have a hard time finding a nice example on how to tell it that when typing: gradle stage, it should create 2 executables.

Right now when I call stage it only provides an executable on the "root" mainclass defined in my gradle script:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'SpringLauncher'
applicationName = 'foo'
compileJava.options.encoding = 'UTF-8'
targetCompatibility = '1.7'
sourceCompatibility = '1.7'

task stage(dependsOn: ['clean', 'installApp', 'hello'])

And the Maven build:

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
      <artifactId>appassembler-maven-plugin</artifactId>
      <version>1.1.1</version>
      <configuration> 
        <assembleDirectory>target</assembleDirectory> 
        <programs>
            <program>
                <mainClass>foo.bar.scheduler.SchedulerMain</mainClass>
                <name>scheduler</name>
            </program>
            <program>
                <mainClass>SpringLauncher</mainClass>
                <name>web</name>
            </program>
        </programs>
      </configuration>
      <executions>
          <execution>
              <phase>package</phase><goals><goal>assemble</goal></goals>
          </execution>            
      </executions>
  </plugin>
</plugins>

like image 636
Jes Chergui Avatar asked Jan 20 '14 18:01

Jes Chergui


2 Answers

Unfortunately the gradle application plugin does not provide first class support for multiple executable scripts.

Luckily though, because gradle scripts are groovy, you can change what the application plugin does reasonably easily.

The documentation for the Application plugin show that the startScripts task is of type CreateStartScripts, so try creating yourself a second task of the same type

task schedulerScripts(type: CreateStartScripts) {
    mainClassName = "foo.bar.scheduler.SchedulerMain"
    applicationName = "scheduler" 
    outputDir = new File(project.buildDir, 'scripts')
    classpath = jar.outputs.files + project.configurations.runtime
}

then include the output of that task in your distribution

applicationDistribution.into("bin") {
            from(schedulerScripts)
            fileMode = 0755
}
like image 109
Perryn Fowler Avatar answered Sep 28 '22 12:09

Perryn Fowler


It might be better to use JavaExec

task scheduler(type: JavaExec) {
   main = "foo.bar.scheduler.SchedulerMain"
   classpath = sourceSets.main.runtimeClasspath
}

task web(type: JavaExec) {
   main = "SpringLauncher"
   classpath = sourceSets.main.runtimeClasspath
}

You can then run gradle scheduler web

like image 33
First Zero Avatar answered Sep 28 '22 13:09

First Zero