Maven2 to Gradle Convertor – Take II - No Fluff Just Stuff

Maven2 to Gradle Convertor – Take II

Posted by: Baruch Sadogursky on October 11, 2010

Well, it’s time to another solution for something that I see as the biggest absent feature of Gradle - decent migration tool from Maven2. Gradle provides some cool Maven2 integration features – you can use Maven repositories, Gradle (well, Ivy inside Gradle) understand your dependencies’ poms in terms of transitive dependencies, you can even generate pom for your artifact and deploy it to Maven repo, but what about the build itself? For now it should it be trashed over and rewritten completely. That is a show-stopper for a lot of projects. They worked so hard to make their Maven work (you know what I mean… Maven == working hard), and now I have to say them to just throw it away and rewrite? No way! Some time ago I took @psynikal’s script for generating Gradle like dependencies from Maven like ones and improved it a bit to generate usable Gradle build file out of pom. The full story is here. That solution, while definitely is better than void is far from being perfect for number of reasons:

  1. It is fragile.
  2. It uses maven-help-plugin. Did I say fragile?
  3. Changes in pom.xml aren’t reflected in your build – you need to regenerate the gradle build files (writing them over, destroying all changes you made – the script isn’t perfect in that sense).
  4. Probably some annoying bugs.

Now it’s time for something more serious – the m2metadata plugin. In an essence, it takes metadata from Maven’s Project Object Model and builds Gradle project out of it.

More specifically it does the following:

  1. Ask Maven to parse poms and settings xmls as it does during regular Maven build.
  2. Set group, version and status (snapshot/release) for Gradle project.
  3. Apply Gradle plugins according to packaging (jar -> java, war -> war). Currently those two are the only supported, but more are coming.
  4. Get some metadata from well-known Maven plugins and configure Gradle plugins with it. This step currently includes setting Java compiler level and configuring sources and resources directories.
  5. Add repositories.
  6. Add dependencies (both external and inter-project).

That’s about it.

Now for the dark side. Currently, the m2metada-plugin clashes with maven-plugin (classloading issues). It can be worked around, but:

  1. Maven-plugin is bundled, so it must be explicitly removed by deleting jars from Gradle’s lib directory.
  2. The true power of m2metadata plugin is using it together with maven-plugin. M2metadata-plugin retrieves metadata part of maven build, while maven-plugin runs Maven’s runtime to execute goals like generating poms and deploying to maven repositories.

Yet another, more methodological than technical downside of m2metadata-plugin is that it preserves the usage of pom.xml. It works, so you don’t touch it, and it stays forever instead of being replaced with fully-blown build.gradle. For that concern, I see clear benefits in using the script solution, which trashes the pom.xml, leaving you with pure-gradle solution, and in conjunction with the idea-plugin gives you all you need to start going.

All in all, once the classloading issues will be sorted out, It looks to me that the mission of creating migration tool can be considered as accomplished.

You can find my work here (Usage guide in Wiki, TODOs in issues). I am going to present it (together with the script, which,as mentioned, has it own benefits) at The Project Automation Experience 2010 in the Java Build Automation Tools Jungle session. The presentation will be posted here once it will be ready.


Filed under: Build Tagged: build, gradle, maven2, project automation experience 2010
Baruch Sadogursky

About Baruch Sadogursky

Baruch Sadogursky (@jbaruch) did Java before it had generics, DevOps before there was Docker, and DevRel before it had a name. He started DevRel at JFrog when it was ten people and took it all the way to a successful $6B IPO by helping engineers solve problems. Now Baruch keeps helping engineers solve problems but also helps companies help engineers solve problems. He is a co-author of the “Liquid Software” and “DevOps Tools for Java Developers” books, serves on multiple conference program committees, and regularly speaks at numerous most prestigious industry conferences, including Kubecon, JavaOne (RIP), Devoxx, QCon, DevRelCon, DevOpsDays (all over), DevOops (not a typo) and others. After a tenure of eleven years in JFrog DevRel, Baruch is the Principal Developer Productivity Engineering Advocate at Gradle.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »