如何将 Maven 项目添加为 Gradle 依赖项? [英] How to add a Maven project as a Gradle dependency?
问题描述
如何将 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这样的私有存储库a> 或 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.gradle.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
这篇关于如何将 Maven 项目添加为 Gradle 依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!