Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven project as dependency in gradle project

Tags:

maven

gradle

I have a project which is using Gradle as build tool and a second subproject which is using Maven's POM. I don't have the freedom of changing build tool on the subproject.

What I want to achieve is to add my project with Maven POM as dependency on my Gradle project.

Where root (current dir) is my project with Gradle and contains the build.gradle, the Maven project is under vendor/other-proj/ with POM file just under that directory.

I have tried these variations on my build.gradle file:

1st try:

include("vendor/other-proj/")
project(':other-proj') {
    projectDir = new File("vendor/other-proj/pom.xml")
}

dependencies {
    compile project(':other-proj')
}

2nd try:

dependencies {
    compile project('vendor/other-proj/')
}

3rd try:

dependencies {
    compile project('vendor/other-proj/pom.xml')
}

4th try:

dependencies {
    compile files 'vendor/other-proj/pom.xml'
}

I can't find anything related on the web, it seems most Gradle/Maven use cases are affected by publishing to Maven or generating POM, but I dont want to do any of those.

Can anybody point me to right direction?

like image 834
mohamnag Avatar asked Mar 04 '16 11:03

mohamnag


2 Answers

you can "fake" including a Maven project like this:

dependencies {
    compile files("vendor/other-proj/target/classes") {
        builtBy "compileMavenProject"
    }
}

task compileMavenProject(type: Exec) {
    workingDir "vendor/other-proj/"
    commandLine "/usr/bin/mvn", "clean", "compile"
}

This way Gradle will execute a Maven build (compileMavenProject) before compiling. But be aware that it is not a Gradle "project" in the traditional sense and will not show up, e.g. if you run gradle dependencies. It is just a hack to include the compiled class files in your Gradle project.

Edit: You can use a similar technique to also include the maven dependencies:

dependencies {
    compile files("vendor/other-proj/target/classes") {
        builtBy "compileMavenProject"
    }
    compile files("vendor/other-proj/target/libs") {
        builtBy "downloadMavenDependencies"
    }
}

task compileMavenProject(type: Exec) {
    workingDir "vendor/other-proj/"
    commandLine "/usr/bin/mvn", "clean", "compile"
}

task downloadMavenDependencies(type: Exec) {
    workingDir "vendor/other-proj/"
    commandLine "/usr/bin/mvn", "dependency:copy-dependencies", "-DoutputDirectory=target/libs"
}
like image 126
tomasulo Avatar answered Oct 21 '22 11:10

tomasulo


You cannot "include" a maven project in gradle settings.gradle. The simplest way would be to build the maven project and install it to your local repo using mvn install(can be default .m2, or any other custom location) and then consume it from your gradle project using groupname:modulename:version

repositories{
    mavenLocal()
}

dependencies{
    compile 'vendor:otherproj:version'
}

It is possible to depend directly on the jar of the maven project using compile files but this isn't ideal because it wont fetch transitive dependencies and you'll have to add those manually yourself.

like image 12
RaGe Avatar answered Oct 21 '22 12:10

RaGe