Skip to content

Migration of maven projects to gradle

James Phelps edited this page Oct 28, 2021 · 15 revisions

Introduction

A good entry point for get started you can find on my medium.com post.

When you have CI like travis.com you have also commit the two files in the gradle directory which is in the main project directory:

/gradle/wrapper/gradle-wrapper.jar
/gradle/wrapper/gradle-wrapper.properties

add them to the git repository

git add -f gradle/wrapper/gradle-wrapper.jar
git add -f gradle/wrapper/gradle-wrapper.properties

and make gradlew file and gradle-wrapper.jar executable

chmod +x gradlew
chmod +x gradle/wrapper/gradle-wrapper.jar

To simple up this process you can create a migration shell script for this issue that looks as follows:

#!/usr/bin/env sh
git add -f gradle/wrapper/gradle-wrapper.jar
git add -f gradle/wrapper/gradle-wrapper.properties
chmod +x gradlew
chmod +x gradle/wrapper/gradle-wrapper.jar

Don't forget to add any Environment Variables at https://travis-ci.org/username/project/settings that can break your build

clean up maven files

Now you can delete the maven related files and of course eclipse launch scripts from maven. I have my launch scripts in the directory /src/launch

pom.xml
/target
/src/launch

After that is done you can push the feature branch and see if the travis builds successfully. If that's the case than you can merge you feature branch with develop branch.

Migrate license-maven-plugin

In maven we are using the great maven plugin: com.mycila:license-maven-plugin:3.0 To migrate this we can now use the gradle plugin: com.github.hierynomus.license:0.15.0

plugins {
...
    id "com.github.hierynomus.license" version "0.15.0"
}
...

license {
    ext.year="2015"
    ext.owner="Asterios Raptis"
    header rootProject.file('src/main/resources/LICENSE.txt')
    excludes(["**/README",
              "**/README.md",
              "**/LICENSE",
              "**/NOTICE",
              "**/*.xml",
              "**/*.xsl",
              "**/*.xsd",
              "**/*.dtd",
              "**/*.html",
              "**/*.jsp",
              "**/*.jpa",
              "**/*.sql",
              "**/*.properties",
              "**/*.bat",
              "**/*.gradle",
              "**/*.MF",
              "**/*.txt",
              "**/*.vm",
              "**/*.log",
              "**/*.map",
              "**/*.js.map",
              "**/*.tmpl",
              "**/*.js.tmpl",
              "**/*.editorconfig",
              "**/*.lombok.config",
              "src/test/resources/**",
              "src/main/resources/**",
              "out/**",
              "build/**"])
}

For check if the license header need an update execute the command:

gradle licenseFormatTest

For update the license header execute the command:

gradle licenseFormat

For more information go to the gradle plugin page

Migrate the Versions Maven Plugin

In maven we are using the great maven plugin: org.codehaus.mojo:versions-maven-plugin:2.7 To migrate this we can now use the gradle plugin: com.github.ben-manes:gradle-versions-plugin:0.27.0

plugins {
...
    id "com.github.ben-manes.versions" version "0.27.0"
}

For check if dependency versions needs an update execute the command:

gradle dependencyUpdates

For more information go to the gradle plugin page

Migrate from maven-deploy-plugin

In maven we are using the great maven plugin: maven-scm-publish-plugin That executes everything automatically.

In gradle we have to execute the following command:

gradle publish

To publish the project see the documentation of the user guide from Maven Publish Plugin from the gradle home page.

Note: with gradle we have also close and release in sonatype in two steps after the command above.

After this two steps is the release in the maven repository available.

Migrate back to maven

If you want to revert your gradle project back to maven project for what reason ever, or you have a requirement for it then you have to import the maven-plugin into your build.gradle file.

apply plugin: 'maven'

This enables gradle to run the 'install' command.

gradle install

That generates the corresponding files like the jar file, pom-default.xml file and a manifest file in the build directory of the gradle project.

  • The jar file is in the build/libs directory
  • The pom file is in the build/poms directory
  • The manifest files is in the build/tmp/jar directory

So for now you have everything you need to migrate back to maven

But you can also configure what the maven-plugin to generate by configure it in the build.gradle file in the 'install' task

Alternative you can use the maven-publish plugin.

It does have similar features and is the recommended way to do it.

Clone this wiki locally