Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a Maven project as a Gradle dependency?

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?

like image 239
Prostitutor Avatar asked Mar 17 '16 10:03

Prostitutor


People also ask

How do I add a Gradle project in Maven?

To convert Gradle to Maven, first, Maven plugin need to be added in the build. gradle file. Then, simply run Gradle install in the directory. Lastly, simply run gradle install and the directory containing build.

Can Gradle use Maven dependency?

Gradle can consume dependencies available in the local Maven repository.

Can we convert Maven project to Gradle?

The first step is to run gradle init in the directory containing the (master) POM. This will convert the Maven build to a Gradle build, generating a settings. gradle file and one or more build. gradle files.


1 Answers

You can't really add the Maven multi-module project structure as a dependency directly. You can, however, build the multi-module project using mvn install to install the project jars to your local repository.

Then, in your build.gradle, you need the following configuration:

repositories {
  mavenLocal()
}

This will add your local Maven repository to the list of code repositories that Gradle will look through for your artifacts. You can then declare a dependency on the module(s) that your Gradle project requires.

dependencies {
    compile 'my-group:my-artifact:version',
            'my-group:my-other-artifact:version'
}

When the multi-module project updates to a new release version, run mvn install for that release and update your build.gradle as needed.

Unless you are the only developer on both projects, it would be better to use a private repository like Nexus or Artifactory to host the maven project and configure Gradle to pull dependencies from there as well.

References:

Maven Local Repository in Gradle: https://docs.gradle.org/2.4/userguide/dependency_management.html#sub:maven_local

Maven Dependencies in Gradle: https://docs.gradle.org/2.4/userguide/dependency_management.html#sub:module_dependencies

like image 195
DuckPuppy Avatar answered Sep 28 '22 04:09

DuckPuppy