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).
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.
Android Gradle plugin 7.2.
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With