What is the gradle equivalent of maven exec plugin to run Java applications?

With maven, I can create a project, configure my pom with its dependencies, write a class with the main method, and then run its type:

mvn compile exec:java -Dexec.mainClass=thornydev.App 

What is the gradle equivalent of this?

I can do a gradle build that creates a jar file for me, but if the main class has dependencies on other jars, just starting jar will not work without setting up the class. Can the gradle java plugin run the application and set the class path for it?

I am looking for a command line solution for simple, one-time applications, and not for IDE integration (I know how to do this).

+15
gradle
May 03 '13 at 2:39
source share
2 answers

The simplest solution is to use Application Plugin , which, among other things, provides a run task. To make the main class custom from the command line, you need to set mainClassName value of some property of the system (or project), and then pass this property from the command line:

 apply plugin: "application" mainClassName = System.getProperty("mainClass") 

Now you can start the application with gradle run -DmainClass=thornydev.App .

+21
May 3, '13 at 3:19
source share

I needed to run a Java program as part of the build process, and there was too much baggage in the application plugin.

I made fidde with the application plugin, but in the end I used the much less "invasive" JavaExec plugin .

I have a class file MyObfuscator.class in build.outputDirectory , and before that I had a pom.xml that performed code obfuscation in the assembly directory with two parameters:

 <project> ... <build> ... <plugins> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <id>obfuscate</id> <phase>package</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>java</executable> <workingDirectory>${project.build.outputDirectory}</workingDirectory> <arguments> <argument>-Djava.library.path=.</argument> <argument>-classpath</argument> <argument>${project.build.outputDirectory}:lib.jar</argument> <argument>MyObfuscator</argument> <argument>HELLO</argument> <argument>GOODBYE</argument> </arguments> </configuration> </execution> </executions> </plugin> ... </plugins> </build> ... </project> 

I welded this in this article in Gradle:

 apply plugin: "java" // ... task obfuscate(type: JavaExec) { // Make sure it runs after compilation and resources. // Skip this if that not a requirement. dependsOn classes // run in the buildDir (this requirement was what // made a simple "doLast" infeasible) workingDir = file(buildDir) classpath = files([ "${buildDir}/classes", "${buildDir}/resources/main/lib.jar" ]) main = "MyObfuscator" } 

If you need to perform parameterized execution, as in the Maven example above, add a few lines to the task:

 task obfuscate(type: JavaExec) { // ... (as above) // Set PARAM1 to a default value when it not // defined on the command line if(!project.hasProperty("PARAM1")) { ext.PARAM1 = "HELLO" } // PARAM1 is dynamic and PARAM2 is static, always "GOODBYE" args = [PARAM1, "GOODBYE"] } 

Then depend on the assemble task on obfuscate (put this line somewhere below the obfuscate task definition):

 assemble.dependsOn obfuscate 

Or let the task (earlier) jar depend on it. See the chart below this section of the docs here for more ideas on where to do this.

Then you can call gradle as gradle build -PPARAM1=HELLO to start the parameterized build.

0
May 22 '17 at 21:36
source share



All Articles