gradle.png

Advantages of Gradle

There are many advantages of using Gradle over Maven. A full comparison of features and benefits are described in this chart:  http://gradle.org/maven_vs_gradle/

Installation

Default version found in our server's repository was 1.4, instead you can install the latest version with these steps:

	sudo add-apt-repository ppa:cwchien/gradle
	sudo apt-get update
	sudo apt-get install gradle-ppa

Latest version (or any version after 2.12) is nice to use because it has automatic migration from Maven project to Gradle using only gradle init in the Maven's project root folder.

Migrating a simple webapp project: 

Quick Way - Automatic migration

http://gradle.org/migrating-a-maven-build-to-gradle/

To let Gradle handle most of the migration from Maven, just need to go to the Maven project's root folder (the one that has the main/parent pom.xml file), and run:

	gradle init

Gradle will move the correct configuration files for the project (settings.gradle, which defines the project structure, and build.gradle, which defines the tasks, dependencies and build process it will use), importing project's information and dependencies defined in pom.xml into build.gradle.

Review build.gradle to make sure the non-supported scopes (provided and optional) are well specified in the dependencies section of the build file. (Changed 'compileProvided' for just 'compile').

Additionally, you will notice at the top that Gradle uses the Java and Maven plugins, which allow recreating the exact same build process that Maven uses without more manual work

It also specifies the repository to the default one of the maven version migrated from. Gradle can support Ant repositories (Ivy), Maven 2 and 3.

Longer Way: Manually defining build script

https://docs.gradle.org/current/userguide/tutorial_java_projects.html

In a non-Maven project folder, execute  gradle init  to initialize a new empty Gradle project.

The default build.gradle script comes pre-loaded with some basic information for any project. These are:

  • Plugins to use  -  Like the Java plugin, or Maven plugin, as mentioned before
  • Group  -  Organization ID  (equivalent of groupId)
  • Version
  • Source and Target SDK compatibility
  • Repositories   -  Repositories where to pull the dependencies from (default one is  jcenter() )
  • Dependencies

In both cases, settings.gradle specifies the root-name with the same name as the directory where it's initialized. No need to change it here.

Finally, copy the Maven project's  src folder, including all tests and resources, into the new Gradle project.

Building the Migrated Project

Following either of the 2 ways, the use of Java plugin allows having the same build process Maven has, so just need to run:

	gradle build
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'war'
apply plugin: 'jetty'
group = 'org.roberto.simpleweb'
version = '1.0-SNAPSHOT'
description = """simple-web Maven Webapp"""
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
     maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
    testCompile group: 'junit', name: 'junit', version:'3.8.1'
    compile group: 'javax.servlet', name: 'servlet-api', version:'2.4'
}
jettyRun {
    httpPort = 8081
}

Integrating with Nexus

To Manage Dependencies

To use Nexus as the repository where to pull the dependencies from, it can be done in 2 ways:  Per gradle project,  or as a global setting.

If you only want an specific gradle project to connect to Nexus, add the following to the project's build.gradle:

repositories {
     // Let's pull from our Nexus repository
     maven {
		 // Use the url to your nexus repository here
         url "https://roberto.codefactori.net/nexus/content/groups/public"
     }
}

After adding that, gradle simply pulls the required dependencies from there as needed.

roberto@roberto-bamboo:~/test-project# gradle clean build
:clean UP-TO-DATE
:compileJava
Download https://roberto.codefactori.net/nexus/content/groups/public/org/roberto/mvnsimple/mvn-simple/1.3-SNAPSHOT/mvn-simple-1.3-20160609.203642-1.pom
Download https://roberto.codefactori.net/nexus/content/groups/public/org/roberto/mvnsimple/mvn-simple/1.3-SNAPSHOT/mvn-simple-1.3-20160609.203642-1.jar
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
 
BUILD SUCCESSFUL
 
Total time: 10.123 secs
 
roberto@roberto-bamboo:~/test-project#

Global Setup: If you want all of your projects that are build from this server to use Nexus as their repository for dependencies, add the following to your file  ~/.gradle (create it if it doesn't exists)

allprojects {
  ext.RepoConfigurator = {
    maven {
      url = uri('https://roberto.codefactori.net/nexus/content/groups/public')      // Again, use your Nexus url here
    }
  }
  buildscript.repositories RepoConfigurator
  repositories RepoConfigurator
}

To upload built packages/artifacts to Nexus:

To upload your project's artifacts to Nexus, we need to specify the url and authentication combination in your project's gradle.properties file (create it if it doesn't exist):

nexusUrl=https://roberto.codefactori.net/nexus
nexusUsername=admin
nexusPassword=adminPassword

NOTE: The admin username and password used here are the ones used for login through the UI, not necessarily the same as the deployment login used in Maven's POM

Then, add the following to your build.gradle file:

apply plugin: 'maven'
 
uploadArchives {
  repositories {
    mavenDeployer {
       repository(url: "${nexusUrl}/content/repositories/releases") {
           authentication(userName: nexusUsername, password: nexusPassword)
       }
       snapshotRepository(url: "${nexusUrl}/content/repositories/snapshots") {
           authentication(userName: nexusUsername, password: nexusPassword)
       }
    }
  }
}

To upload / deploy from gradle, use this task (it will upload whether you have a snapshot or release version to its correct repository):

gradle upload

oie_WwwfccH0xMZ3.png

Want to Learn More About Migrating to Gradle?

Contact Us Today