Migration from Maven

As the size and complexities of Ant files started to increase for enterprise software, developers started searching for better solutions. Maven easily fitted as a solution, as it introduced the concept of conventions over configurations. If you follow certain conventions, it saves a lot of time by skipping boilerplate code. Maven also provided a dependency management solution that was one of the major drawbacks of the Ant tool. Ant didn't provide any dependency management solution whereas Maven came with a built-in dependency manager.

When we discussed migration strategies from Ant to Gradle, you learned that the simplest solution is to import the Ant build.xml file and use it as it is. For Maven migration, we do not have such a feature. Maven users might find it easy to migrate from Maven to Gradle, as both follow these common principles:

  • Convention over configurations
  • Dependency management solution
  • Repositories configuration

To migrate from Maven to Gradle, we will need to write a new Gradle script that mimics the functionality. If you have already worked on Maven, you might have noticed that Gradle uses most of the Maven concepts; thus, it would not be very difficult to migrate from Maven to Gradle. One of the main differences is that Gradle uses Groovy as a build script language, whereas Maven uses XML. In this section, we will discuss some of the most common tasks to convert a Maven script to a Gradle script.

Build filename and project properties

The GroupId, artifactId, and version are the minimum required properties that the user needs to provide in the Maven pom file, whereas in Gradle these are not mandatory. Default values are assumed if the user does not configure them. It is always recommended to specify these values to avoid any conflicts:

Maven way

Gradle way

  • <groupId>ch8.example</groupId>
  • <artifactId>SampleMaven</artifactId>
  • <version>1.0</version>
  • <packaging>jar</packaging>
  • groupid: Not required.
  • artifactId: Default is the project directory name.
  • version: If not mentioned, artifact will be created without a version.
  • Packaging depends on the plugin that you apply in the build script.

Tip

If packaging is not mentioned in Maven, by default it will be JAR. The other core packaging are: pom, jar, maven-plugin, ejb, war, ear, rar, and par.

Properties

The following are the ways in which you can define properties in Maven and Gradle:

Maven way

Gradle way

<properties>
     <src>src/main/java
     </src>
    <build>build</build>
     <classes>
       build/classes
    </classes>
    <libs>
        build/libs
    </libs>
<distributions>
     build/distributions
     </distributions>
    <version>
        1.0
     </version>
</properties>

def src="src/main/java"
def build="build"
def lib="lib"
def dist="dist"
def version=1.0

Dependency management

Both Maven and Gradle provide a dependency management feature. They manage dependency on their own. You do not need to worry about any second- or third-level dependencies for your project. Similar to the Maven scope (such as compile and runtime), Gradle also provides different configurations such as compile, runtime, testCompile, and so on. The following table lists the scope supported by both Maven and Gradle:

Maven scopes

Gradle scopes

  • compile
  • provided
  • runtime
  • test
  • system
  • compile
  • providedCompile
  • runtime
  • providedRuntime
  • testCompile
  • testRutime

As compared to provided scope in Maven, the Gradle war plugin adds two additional scopes, providedCompile and providedRuntime. For test cases also Gradle provides two scopes, testCompile and testRuntime. The following is an example of how you use scope while defining dependencies:

Maven way

Gradle way

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
<scope>compile</scope>
</dependency>

dependencies {
    compile group:  'org.apache.commons', name: 'commons-lang3', version:'3.1'
}

Exclude transitive

In Chapter 5, Dependency Management, you learned how to work with dependency management. Maven doesn't differ much in terms of dependency management features. Here is a sample to exclude transitive dependencies in both the build tools:

Maven way

Gradle way

<dependencies>
    <dependency>
      <groupId>
          commons-httpclient
      </groupId>
      <artifactId>
          commons-httpclient
       </artifactId>
      <version>3.1</version>
      <exclusions>
        <exclusion>
          <groupId>
            commons-codec
           </groupId>
          <artifactId>
             commons-codec
          </artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    ...
  </dependencies>

dependencies{

compile('commons-httpclient:commons-httpclient:3.1') {
 exclude group:'commons-codec',
module:'commons-codec'
}

}

Plugin declaration

A maven plugin is a collection of one or more goals that can be applied to a project using the plugins element. A goal in a plugin is executed by the mvn [plugin-name]:[goal-name] command. In Maven, generally we have two types of plugins: build plugins and reporting plugins. Build plugin will executed during build process, and should be configured in the <build/> element of pom.xml. Reporting plugins will be executed during site generation, and are configured using the <reporting/> element. Examples of reporting plugins are Checkstyle, PMD, and so on. In Gradle, you just need to apply a plugin statement in the Gradle script or you need to define it in the buildscript closure.

The following is a sample code, which describes how to include plugins in Maven and how we define the same in Gradle:

Maven way

Gradle way

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

  • apply plugin:'<pluginid>'
  • For custom plugin:

buildscript {
ext {
springBootVersion = '1.2.2.RELEASE'
}
repositories {
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE")
}
}

Repository configuration

Repositories are used to download assets and artifacts, and also to publish the artifacts. In Maven, you can define repositories in pom.xml or in settings.xml. In Gradle, you can add repositories in the init script (init.gradle) or in the build.gradle file:

Maven way

Gradle way

<repositories>
    <repository>
      <id>rep1</id>
      <name>org repo1</name>
      <url>
       http://company.repository1
       </url>
    </repository>
</repositories>

repositories {
maven {
url "http://company.repository1"
}
}

Multi-module declaration

Maven and Gradle both provide ways to create multi-module projects, as follows:

Maven way

Gradle way

<project>
  <groupId>
    com.test.multiproject
   </groupId>
   <artifactId>
     rootproject
  </artifactId>
  <version>
     1.0
  </version>
  <packaging>
     Pom
  </packaging>
  <modules>
    <module>subproject1</module>
    <module>subproject2</module>
  </modules>
</project>

Add settings.gradle under root project and include subprojects as follows:

include 'subproject1', 'subproject2',

Default values

Gradle and Maven both provide default values for certain properties. You can use them as they are, if you want to follow convention and update them if required:

 

Maven way

Gradle way

Project directory

${project.basedir}

${project.rootDir}

Build directory

${project.basedir}/target

${project.rootDir}/build

Classes directory

${project.build.directory}/classes

${project.rootDir}/build/classes

JAR name

${project.artifactId}-${project.version}

${project.name}

Test output directory

${project.build.directory}/test-classes

${project.testResultsDir}

Source directory

${project.basedir}/src/main/java

${project.rootDir}/src/main/java

Test source directory

${project.basedir}/src/test/java

${project.rootDir}/src/test/java

Gradle init Plugin

Build init plugin can be used to generate the build.gradle file from a pom file. Command gradle init --type pom or gradle init creates Gradle build files and other artifacts if executed from a project or directory that has a valid pom.xml file.

We created one project, SampleMaven, that contains Java files in the srcmainjavach8 directory and the pom.xml file under the root project directory. The following is the content of the pom.xml file:

<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>ch8.example</groupId>
   <artifactId>SampleMaven</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
   <dependencies>
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.1</version>
       <scope>compile</scope>
     </dependency>
   </dependencies>
 </project>

Now, execute the following command:

$ gradle init --type pom
:wrapper
:init
Maven to Gradle conversion is an incubating feature.

BUILD SUCCESSFUL

Note that Maven to Gradle is an incubating feature. Current DSL and other configurations might change in future. On executing the preceding command, you will find build.gradle, settings.gradle, and Gradle wapper files created in the project directory. The auto-generated build.gradle file has the following content. It automatically adds plugin details, and group and version information.

The content of a system-generated build file is shown in the following code snippet:

apply plugin: 'java'
apply plugin: 'maven'

group = 'ch8.example'
version = '1.0'
description = """"""

sourceCompatibility = 1.5
targetCompatibility = 1.5

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

dependencies {
    compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.1'
}

This plugin also supports multiproject build, repository configuration, dependency management, and a lot of other features.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.111