Skip to content

Migration of maven projects to gradle

Asterios Raptis edited this page Feb 3, 2023 · 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

extract version into gradle.properties

As first step you have to download the repository gradle-migration-data. Then go to the unit test GradleRunConfigurationsCopierTest#testCopyIdeaRunConfigurations and configure the source and the target paths.

The sourceProjectName you can let it as it is.

Set the targetProjectName to your project name that have to be the same as the folder name from your new migrated gradle project. Set the sourceGithubUser to the parent directory from repository 'gradle-migration-data'. For instance if you downloaded it to the directory: /home/foouser/git/githubfoouser/gradle-migration-data

Then the parent directory is 'githubfoouser'

Set the sourceProjectDirNamePrefix to the rest of you path, on our example above the sourceProjectDirNamePrefix is '/home/foouser/git/'

The same for the targetGithubUser and the targetProjectDirNamePrefix for the target project.

There are also flags for runConfigurationsInSameFolder and onlyRunConfigurations.

If all set run the unit test GradleRunConfigurationsCopierTest#testCopyIdeaRunConfigurations. This will generate the gradle.properties with the corresponding versions

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.

The legacy maven gradle plugin is only till version 6.x available and is removed from version 7.x and above.

Alternative you can use the maven-publish plugin. It does have similar features and is the recommended way to do it.

You can import the maven-publish plugin with the following statement into your build.gradle file.

apply plugin: 'maven-publish'

Here is an simple example for the configuration:

publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'io.github.astrapi69'
            artifactId = 'jgeohash'
            version = '2.5'

            from components.java
        }
    }
}

You can extract all values to the gradle.properties

projectVersion=2.5
groupPackage=io.github.astrapi69

and then it looks like the following:

publishing {
    publications {
        maven(MavenPublication) {
            groupId = '$groupPackage'
            artifactId = '$rootProject.name'
            version = '$projectVersion'

            from components.java
        }
    }
}

This enables gradle to run the 'publish' and the 'publishMavenJavaPublicationToMavenLocal' command.

gradle publish

or

gradle publishMavenJavaPublicationToMavenLocal

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

The complete documentation is on the docs.gradle.org site.

Clone this wiki locally