Convert Maven Build To Gradle

Convert Maven Build To Gradle

Gradle is an open source build automation system and introduces a Groovy-based domain-specific language (DSL) instead of the XML for declaring the project configuration. Gradle is more feature rich than maven and more modern java build tool. If you are planning to port from maven to gradle build system, this article may help you.

Step 1: You must have gradle installed on your machine
Gradle requires a Java JDK or JRE to be installed, version 7 or higher (to check, open command prompt and run java -version). Gradle ships with its own Groovy library, therefore Groovy does not need to be installed. Any existing Groovy installation is ignored by Gradle. Alternatively, you can set the JAVA_HOME environment variable to point to the installation directory of the desired JDK.
You can download one of the Gradle distributions from here. To check if Gradle is properly installed just type gradle –v. The output shows the Gradle version and also the local environment configuration.

Step 2: Run gradle init in the directory containing the (master) pom.xml
gradle init automatically detects the pom.xml and creates a gradle build with the java and maven plugin loaded. Existing Maven dependencies are automatically converted and added to your gradle build file.
This will convert the maven build to a gradle build, generating one or more build.gradle files and a settings.gradle file.
For simpler maven builds, this is all we need to do. For more complex Maven builds, it may be necessary to manually add functionality on the Gradle side that couldn't be converted automatically.

What is build.gradle file?
This build file defines a project and it’s tasks. Gradle is a general purpose build system hence this build file can perform any task.

What is settings.gradle file?
Gradle reads the settings.gradle file to figure out which subprojects are to take part in a build. It is usually placed in the root project parallel to build.gradle.

Step 3: Run gradlew build in the directory containing build.gradle files
This task compiles, tests, and assembles the code into a JAR file. After a few seconds, "BUILD SUCCESSFUL" indicates that the build has completed.

Let’s take an example

Step 1: I run the gradle –v command into project’s root directory. I get the following output:

Now make sure gradle installed successfully on your machine.

Step 2: I run the gradle init command into project’s root directory containing the (master) pom.xml. I got the following output:

Step 3: I run gradlew build in the directory containing build.gradle files.

Exaple pom.xml file:


<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.addapps</groupId>
   <artifactId>server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>server</name>
   <description>TTGenerator web app Auth Service</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.4.2.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <querydsl.version>4.0.4</querydsl.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-zuul</artifactId>
         <version>1.0.0.RELEASE</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-mobile</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-security</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-websocket</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jersey</artifactId>
      </dependency>
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
         <version>5.1.17</version>
      </dependency>
      <dependency>
         <groupId>com.querydsl</groupId>
         <artifactId>querydsl-apt</artifactId>
         <version>${querydsl.version}</version>
      </dependency>
      <dependency>
         <groupId>com.querydsl</groupId>
         <artifactId>querydsl-jpa</artifactId>
         <version>${querydsl.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>io.jsonwebtoken</groupId>
         <artifactId>jjwt</artifactId>
         <version>0.6.0</version>
      </dependency>
      <dependency>
         <groupId>org.json</groupId>
         <artifactId>json</artifactId>
      </dependency>
      <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-client</artifactId>
         <version>1.8</version>
      </dependency>
      <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-io</artifactId>
         <version>1.3.2</version>
      </dependency>
      <dependency>
         <groupId>org.codehaus.jackson</groupId>
         <artifactId>jackson-mapper-asl</artifactId>
         <version>1.9.13</version>
      </dependency>
      <dependency>
         <groupId>com.google.code.gson</groupId>
         <artifactId>gson</artifactId>
         <version>2.8.0</version>
      </dependency>
      <dependency>
         <groupId>com.googlecode.json-simple</groupId>
         <artifactId>json-simple</artifactId>
         <version>1.1</version>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.SR5</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <finalName>server</finalName>
      <plugins>
         <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
               <execution>
                  <goals>
                     <goal>process</goal>
                  </goals>
                  <configuration>
<outputDirectory>${project.build.directory}/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                  </configuration>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Build.gradle file:

apply plugin: 'java'
apply plugin: 'maven'
group = 'com.addapps'
version = '0.0.1-SNAPSHOT'
description = """server"""
sourceCompatibility = 1.5
targetCompatibility = 1.5
tasks.withType(JavaCompile) {
   options.encoding = 'UTF-8'
}

repositories {
     maven { url "http://repo.maven.apache.org/maven2" }
}

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version:'1.0.0.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-mobile', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.4.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jersey', version:'1.4.2.RELEASE'
    compile group: 'com.querydsl', name: 'querydsl-apt', version:'4.0.4'
    compile group: 'com.querydsl', name: 'querydsl-jpa', version:'4.0.4'
    compile group: 'io.jsonwebtoken', name: 'jjwt', version:'0.6.0'
    compile group: 'org.json', name: 'json', version:'20140107'
    compile group: 'com.sun.jersey', name: 'jersey-client', version:'1.8'
    compile group: 'org.apache.commons', name: 'commons-io', version:'1.3.2'
    compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version:'1.9.13'
    compile group: 'com.google.code.gson', name: 'gson', version:'2.8.0'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version:'1.1'
    runtime group: 'mysql', name: 'mysql-connector-java', version:'5.1.17'
    runtime group: 'org.springframework.boot', name: 'spring-boot-devtools', version:'1.4.2.RELEASE'
    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.4.2.RELEASE') {
exclude(module: 'commons-logging')
    }
}

settings.gradle file:

rootProject.name = 'server'