Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the gradle equivalent of maven's exec plugin for running Java apps?

Tags:

gradle

With maven I can create a project, set up my pom with its dependencies, write a class with a main method and then to run it type:

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

What is the gradle equivalent of doing this?

I can do gradle build, which builds a jar file for me, but if the main class has any dependencies on other jars, just running the jar won't work without setting up the classpath. Can the gradle java plugin run the app and set up the classpath for me?

I'm looking for a command line solution for simple one-off uses, not IDE integration (I know how to do that).

like image 479
quux00 Avatar asked May 03 '13 02:05

quux00


People also ask

What is Java Gradle plugin?

The Java Gradle Plugin development plugin can be used to assist in the development of Gradle plugins. It automatically applies the Java Library plugin, adds the gradleApi() dependency to the api configuration and performs validation of plugin metadata during jar task execution.

Which Gradle plugin to use?

Android Gradle plugin 7.2.

What does Gradle application plugin do?

The Application plugin facilitates creating an executable JVM application. It makes it easy to start the application locally during development, and to package the application as a TAR and/or ZIP including operating system specific start scripts.

Can Gradle use Maven plugins?

You can't use a Maven plugin as-is in Gradle; you'll have to port it to a Gradle plugin. How difficult this is depends on how many Maven APIs the plugin is using. Another strategy might be to call into Maven via Gradle's Exec task.


2 Answers

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

apply plugin: "application"

mainClassName = System.getProperty("mainClass")

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

like image 135
Peter Niederwieser Avatar answered Oct 04 '22 03:10

Peter Niederwieser


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

I did 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 the build.outputDirectory and before I had a pom.xml like this, which ran code obfuscation in the build 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 boiled it down to this thing in Gradle:

apply plugin: "java"

// ...

task obfuscate(type: JavaExec) {
    // Make sure it runs after compilation and resources.
    // Skip this if that's 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 parameterized execution like in the Maven example above, then add a few lines to the task:

task obfuscate(type: JavaExec) {
    // ... (as above)
    
    // Set PARAM1 to a default value when it's 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 the assemble task on obfuscate (put this line anywhere below the obfuscate task definition):

assemble.dependsOn obfuscate

Or let the (earlier) jar task depend on it. See the graph at the bottom of this docs section here for more ideas on where to inject this.

You can then call gradle like gradle build -PPARAM1=HELLO to run a parameterized build.

like image 35
grandchild Avatar answered Oct 04 '22 05:10

grandchild