如何将Maven项目添加为Gradle依赖项?

如何将Maven项目添加为Gradle依赖项?

问题描述:

如何将Maven项目添加为Gradle依赖项?我有正在从事的Gradle项目,还有一些想作为代码依赖项导入到我的Gradle项目中的多模块Maven项目。

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?

您不能真正将Maven多模块项目结构直接添加为依赖项。但是,您可以使用 mvn install 构建多模块项目,以将项目jar安装到本地存储库中。

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.

然后,在您的 build.gradle 中,您需要进行以下配置:

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

repositories {
  mavenLocal()
}

这将添加您的本地Maven存储库到Gradle将为您的工件查找的代码存储库列表。然后,您可以声明对Gradle项目所需的模块的依赖性。

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'
}

当多模块项目更新为新发行版时,对该版本运行 mvn install 并更新您的 build.gradle

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

除非您是两个项目的唯一开发人员,否则最好使用私有存储库例如 Nexus Artifactory 来托管Maven项目,并配置Gradle也从那里获取依赖项。

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.

参考:

Gradle中的Maven本地存储库: https ://docs.gra dle.org/2.4/userguide/dependency_management.html#sub:maven_local

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

Gradle中的Maven依赖关系:
https://docs.gradle.org/2.4/userguide/dependency_management.html#sub:module_dependencies

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