Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3627

Gradle Equivalents for Maven Commands

$
0
0
start here featured

1. Introduction

Maven and Gradle are the two most popular build automation and dependency management tools that simplify the developer’s job. While Maven is a widely used build automation tool, Gradle is more modern and flexible.

As someone who’s transitioning from Maven to Gradle, it’s crucial to understand equivalent commands in Gradle.

In this tutorial, we’ll explore Maven commands and their equivalents in Gradle. We’ll also take a look at how to publish artifacts of one project in the Maven local repository so that it will be available for other projects to use.

2. Maven Commands and Their Gradle Equivalents

In this section, we’ll explore some of the most popular Maven commands and their equivalent commands in Gradle.

2.1. Updating Dependencies

Both Maven and Gradle allow us to update project dependencies to ensure we have the latest versions of them.

In Maven, the following is the typical command used to update the project dependencies:

mvn clean install -U

The mvn clean install with the -U (uppercase) option will force Maven to check for updated versions of dependencies and download them from the remote repository (even if they’re already present in the local repository).

However, this command doesn’t update the non-snapshot dependencies or release versions. If you want to update non-snapshot dependencies, you still need to modify the pom.xml file, or alternatively, you can use the following command:

mvn versions:use-latest-releases

This command will replace any release versions that are not snapshots and do not have a year-month-day suffix with the latest release version.

In Gradle, we use the below command to force refresh dependencies from remote repositories.

gradle build --refresh-dependencies

This command ignores the locally cached versions of dependencies and forces Gradle to do a fresh lookup in the configured repositories. It’s important to note that Gradle checks the remote repositories for updates of dynamic versions like SNAPSHOT, latest.release, and 1.+, and if a newer version is available, it’ll download it.

However, even though Gradle ignores the cache, it doesn’t blindly re-download every artifact. Instead, it compares the hash values of the files in the local and remote repositories, and if they match, it uses the existing file instead of re-downloading it.

2.2. Building and Installing the Project

In Maven, to clean, compile, test, package, and install the project, we use the below command:

mvn clean install

We can achieve the same in Gradle using the below command:

gradle clean build

This command deletes the build/ directory and then compiles, tests, and packages the project.

2.3. Other Common Commands

Let’s now take a look at some of the other commands that are quite commonly used in Maven and their equivalent commands in Gradle:

Maven Command Gradle Command Purpose
mvn clean gradle clean Removes the build directory
mvn compile gradle compileJava Compiles Java code
mvn test gradle test Runs tests
mvn package gradle jar / gradle war Creates JAR/WAR file
mvn verify gradle check Runs all verification tasks
mvn deploy gradle publish Publishes the artifact to a remote repository
mvn site No direct equivalent Maven generates a project site; Gradle doesn’t have this feature built-in
mvn dependency:tree gradle dependencies Shows project dependencies
mvn dependency:purge-local-repository gradle –refresh-dependencies Forces a re-download of dependency

3. Working with Multiple Projects

While working on multiple projects, we often have projects that are dependent on each other. We need to make the artifact of one project available in the local repository for another project to use.

In Maven, we run the command mvn install to make the artifact available in the local repository. But how do we accomplish the same in the case of Gradle? Let’s take a look.

For Gradle versions prior to 7, we use the maven plugin as follows in the build.gradle file:

apply plugin: "java"
apply plugin: "maven"
group = 'com.example'
version = '1.0.0'

And then run:

gradle install

This command installs the artifact into ~/.m2/repository, making it available for dependent projects.

For Gradle 7 and later, we use the maven-publish plugin to publish the artifact to the local repository:

plugins {
  id 'java'
  id 'maven-publish'
}
group = 'com.example'
version = '1.0.0'
publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java
    }
  }
  repositories {
    mavenLocal()
  }
}

The above script defines two plugins. The Java plugin helps in performing standard Java compilation and packaging, and the Maven-publish plugin helps publish the artifact in the local repository.

Next, we have the group and version properties, which define the project’s unique identifier and version.

The publishing block helps us define how the project’s artifacts should be published. The mavenJava publication, of type MavenPublication, is created using the from components.java statement.

This will ensure that the compiled Java components, including the JAR file and other metadata, are included in the publication.

Finally, as part of the repositories, we defined mavenlocal(), which refers to the maven local repository.

Once we define these, we can then run mvn install to install the artifact in the local Maven repository. The artifact is now available for other local projects to use.

4. Conclusion

In this article, we compared commands for both Maven and Gradle. As we’ve seen, they’re quite similar.

What can be done in Maven can also be done in Gradle; however, they differ in terms of configurations and commands we need to run. While it takes some time to get used to Gradle, the transition is easier if we compare and understand the similarities between the two.

The post Gradle Equivalents for Maven Commands first appeared on Baeldung.
       

Viewing all articles
Browse latest Browse all 3627

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>