How to add a Maven project as a Gradle dependency?

Viewed 11964

How to add a Maven project as a Gradle dependency? I have Gradle project I am working on and some multi-module Maven project I would like to import into my Gradle project as a code dependency. How to do it?

3 Answers

The question does not mention whether the maven repository was local or remote. I'm going to assume that it's a remote maven repository and will answer the question according to that so that it's helpful to anyone who had the case of an online repository.

As an example, suppose you want to install Unirest ( https://github.com/Kong/unirest-java )


This is what their github page says: you can install the repository using a maven command but it doesn't offer any jar file for you to download.

This is what their github page says: you can install the repository using a maven command


You can come across several github repositories that don't offer any jar file. Instead they offer a maven code like 'mvm install unirest'. This can be very confusing for a beginner.


What it means: What this essentially means is that that particular unirest github code is available in the online repository (also named "maven") and the tool "maven" that is usually bundled along with your IDE can be used to install this from github directly.

So we're looking at this:

online project Unirest (maven repository which you want to import)
/workspace folder/project B (gradle) --> uses project A

So you open project B in Intellij (or your IDE). Open build.gradle and set the following settings:

repositories {
    mavenCentral()
}

dependencies {
    compileOnly group: 'com.konghq', name: 'unirest-java', version: '3.11.11'
}

How do you get those values? Group / name / version? You take it from the maven's install instructions. From the maven xml that is given in the github code.

Here was my structure:

/workspace folder/project A (maven)
/workspace folder/project B (gradle) --> uses project A

What worked for me was to run mvn install on my maven Project A. Then I copied the jar folders/file from .m2 repository into .gradle/caches/modules-2

Then I added in build.gradle of Project B:

dependencies {
    compile "groupId:artifactId:version"
}

In my case, it was further complicated by project A having an older different version in a nexus repository. I was able to override that with the steps above.

Related