Search code examples
javamavengradlegroovy

What is the equivalent of Maven's <execution> in Gradle?


When I searched to change Javax to Jakarta, I found something like this, so I tried to refer to it. The problem is that I have to use Gradle. How can I use this code in Gradle?

         <plugin>
            <groupId>org.eclipse.transformer</groupId>
            <artifactId>org.eclipse.transformer.maven</artifactId>
            <version>0.2.0</version>
            <executions>
                <execution>
                    <id>jakarta-ee</id>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <classifier>jakarta</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>

I still don't get it how to use task in gradle

task javaxToJakarta(type:){

}


Solution

  • I'm not a maven person, but I think it's just running a class or script. In Gradle you define a task of type Exec to run a script. For example:

    task runScript( type: Exec ) {
       commandLine './start.sh'
    }
    

    if you want to run a java class you use JavaExec:

    task doSomeJava( type: JavaExec ) {
       classpath = sourceSets.main.runtimeClasspath
       mainClass = "com.host.lib.MyCoolLib"
    }
    

    In this example, I'm using the classpath of my codebase (ie sourceSets.main.runtimeClasspath). You could specify your own classpath that doesn't use the classpath's of your project (ie file(someJar.jar)) The way I'm showing it leverages your dependencies you have in Gradle.

    Using the closure syntax of task can be tricky to new gradle people. I think most people think of a task as a set of instructions to run like in ant goes inside the task. That's not exactly right. The code inside the task is configuring the task which runs everything the gradle file is read. The code that runs when you execute a task does not run then. A plain vanilla task would look like this:

    task someTask {
       // we configure the task here
       group = 'analysis'
       doLast {
         // Groovy code goes here to execute the task
       }
    }
    

    But when we use type parameter we are switching the object class of our task. Typically we aren't running code in this situation and we are only configuring the our task Here is a Zip task type.

    task makeAZip(type: Zip) {
       from file("${srcDir}/some")
       to file("${buildDir}")
       include "*.txt"
    }
    

    We're just configuring the task either through task type instance variables, or calling instance methods. We wouldn't write code to produce the zip file because the Zip task already does that for us. We just need to configure it. And that is fundamental concept of how Gradle tasks work.

    In your case you'll want your javaxToJakarta task to run before you make a jar file. When building a java app you add the java plugin which adds predefined tasks for building java projects. In gradle the java project has several phases it goes through when building. These phases are nothing more than a set of predefined tasks. You just need to attach your task to that chain of work.

    tasks.named("jar") {
        dependsOn(javaxToJakarta)
    }
    

    So now your javaxToJakarta task will run before jar because jar dependsOn javaxToJakarta now. For more information on default tasks in java builds see this: https://gradlehero.com/gradle-assemble-task-essentials/#:~:text=Assemble%20is%20a%20lifecycle%20task,artifact%20into%20a%20single%20task.