How to add a Maven project as a Gradle dependency?

后端 未结 2 1584
無奈伤痛
無奈伤痛 2021-01-17 10:40

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

相关标签:
2条回答
  • 2021-01-17 11:06

    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.

    0 讨论(0)
  • 2021-01-17 11:09

    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

    0 讨论(0)
提交回复
热议问题