How Easy is It to migrate Dependencies of pom.xml to build.gradle

In Part 1 The Age Of Gradle, we go throw about Ant, Maven and Gradle.

I mentioned that we can use the combination of Maven and Gradle so I will share you how to do it.

The combination will be complex and depend on various use cases so I cannot cover all in one post. In this post, I just share Gradle script to import Maven dependencies from pom.xml file to without any modification in pom.xml file.

GitHub Repository

This is my origin pom file.


<project xmlns=""

    <name>Ogirin Maven Project</name>



And this is customized build.gradle file. You can see under dependencies block, I implemented script to read pom.xml file, get dependencies and version. Then load into gradle project.


group ''
version '1.0'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {

dependencies {

    // Load dependencies from old pom.xml file
    def pomXml = new XmlSlurper().parse('pom.xml')
    def pomDependencies = pomXml.dependencies.dependency
    pomDependencies.each { dependency ->
        // Get Dependency version
        def dependencyVersion = "${dependency.version}"
        if (dependencyVersion.matches("^\$\{\S*\}$")){
            dependencyVersion = dependencyVersion.replaceAll("[\$\{\}]", "");
            dependencyVersion ="${dependencyVersion}"
            dependencyVersion = "${dependency.version}"
        def dependencySpec = "${dependency.groupId}:${dependency.artifactId}:${dependencyVersion}"
        println dependencySpec
        switch (dependency.scope){
            case 'test':
                dependencies.add 'testCompile', dependencySpec
            case 'runtime':
                dependencies.add 'runtime', dependencySpec
                dependencies.add 'compile', dependencySpec

    // Define dependencies with Gradle syntax
    compile group: '', name: 'gson', version: '2.8.2'

Is it easy? Give me feedback if there are any to help it better.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.