Gradle Application Plugin - Starting two applications from batch file?


(jks) #1

Hello,

I’m currently using Gradle in a multi-module build. Two of the sub-modules contain their own main methods and need to run concurrently in separate JVMs. My initial solution packaged up both applications into individual executable jar files using the Gradle java plugin. Then application A started application B in it’s own process. Something like this…

public class ServiceManagerProcess implements Runnable {
   public static Process _process;
   public void run() {
      Properties prop = new Properties();
      prop.load(ServerPlatformManager.class.getClassLoader()
         .getResourceAsStream("services-mgr.properties"));
        String java = prop.getProperty("java");
      String jarCmd = prop.getProperty("jar-cmd");
      String jarFile = prop.getProperty("jar-file");
              ProcessBuilder processBuilder = new ProcessBuilder(java, jarCmd, jarFile);
      Process _process = processBuilder.start();
      ...
      _process.waitFor();
    }
}

Then I came across the Gradle application plugin, and decided it would be better to package everything up as zip files, with the dependencies jarred (rather than multiple executable uber jars).

However, I’m a bit unsure of the best way to transition the startup sequence so that application A starts application B. I think this could be accomplished using the batch file created via the application plugin. Currently each sub-module is zipped into it’s own distribution and contains it’s own batch file.

Ideally it seems like I want both sub-modules in one zip file and the batch file to start both of them. Can this be configured in my build.gradle script? Where would I start?

I could post snippets of my gradle script, but right now the piece that uses the application plugin is pretty vanilla.

Regards,

John


(Peter Niederwieser) #2

What exactly do you want to end up with? One zip distribution containing two independent apps and a batch script to start both of them?


(jks) #3

Yes. That’s exactly what I want to end up with. Also including their jarred dependencies. Sorry if I wasn’t clear enough.


(Peter Niederwieser) #4

One option would be to use the application plugin for both projects, then have another task that merges the zips into separate subdirectories and adds an “uber” batch script that invokes both start scripts.


(jks) #5

I’m already using the application plugin for both projects. And that gets me two distinct zip files.

Just a couple of questions to get headed in the right direction…

I just noticed the Zip class in the gradle api and the DSL reference. Would you’d suggest using this to merge the zips? What would end up with one zip file with, all the jars for each project in their own distinct subdirectories?

Also, is there anything within the DSL for augmenting behavior of the application plugin with respect to modifying/creating the batch script? Is looks like I might have to write this external to the plugin.

Thanks.


(Peter Niederwieser) #6

Yes, you’d use another ‘Zip’ task, possibly together with the ‘project.zipTree()’ method, to create the merged distribution.

What would end up with one zip file with, all the jars for each project in their own distinct subdirectories?

I can’t parse that sentence. It’s up to you how to merge the Zips, but keeping their contents in separate subdirectories seems like a good choice, unless you need to optimize for archive size.

Instead of changing or merging the generated scripts, you could add a tiny, hand-written uber script that invokes both of them.


(jks) #7

Thanks, that helps a lot. Sorry about the typo - you got the gist.