Java IntelliJ gradle 添加模块依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38877989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:42:54  来源:igfitidea点击:

IntelliJ gradle add module dependency

javaintellij-ideagradle

提问by emiles

Using IntelliJ 2016.2.
Using Gradle 2.14.1

使用 IntelliJ 2016.2。
使用 Gradle 2.14.1

I have 2 projects, each with their own build.gradle files and separate directory structures:

我有 2 个项目,每个项目都有自己的 build.gradle 文件和单独的目录结构:

myLib (meant to be jarred and used by others)
  - build.gradle
  - settings.gradle
  - src/main/java/...

sandbox (spring boot web app)
  - build.gradle
  - settings.gradle
  - src/main/java/...
  - src/main/resources/...

Hopefully, you get the picture. Within IntelliJ, I have the following module structure, at the same level (no subprojects):

希望你能得到这张照片。在 IntelliJ 中,我有以下模块结构,处于同一级别(无子项目):

- myLib
- sandbox

Simple request ... I want to use myLibwithin the sandboxapp. I want both modules in the same project in order to develop both.

简单的请求......我想myLibsandbox应用程序中使用。我希望在同一个项目中同时开发两个模块。

I've tried adding a module dependency to sandboxfor myLibwithin IntelliJ. No dice. I've tried adding a jar reference, no dice.

我尝试在 IntelliJ 中为sandboxfor添加模块依赖项myLib。没有骰子。我试过添加一个 jar 引用,没有骰子。

I believe I need to add a dependency in the build.gradle file but cannot figure out how. I've tried compile files '<path to myLib.jar>', etc. No dice.

我相信我需要在 build.gradle 文件中添加一个依赖项,但无法弄清楚如何。我试过compile files '<path to myLib.jar>',等等。没有骰子。

采纳答案by OneCricketeer

Local Modules

本地模块

This is a pattern followed by most Gradle projects where there is a library, then a sample app that uses that library

这是大多数 Gradle 项目遵循的模式,其中有一个库,然后是使用该库的示例应用程序

 - module/
    - build.gradle
    - src/main/java
 - library/
    - build.gradle
    - src/main/java
 - settings.gradle
 - build.gradle

In that top-level settings.gradleyou have

在那个顶级settings.gradle你有

include ':library', ':module'

And in the module/build.gradle, you compile that included project

在 中module/build.gradle,您编译包含的项目

dependencies {
    compile project(':library')
}

Basically, the top-level build.gradle, is a wrapper for all common configs of the sub projects and variables. For example, it's most commonly used for a repositories { }section for Maven urls, for example. Full details on that are at Gradle - Multi-project builds

基本上,顶级build.gradle, 是子项目和变量的所有常见配置的包装器。例如,它最常用于repositories { }Maven url的部分。对全部细节在摇篮-多项目构建

Remotes Modules

遥控器模块

The above is fine for working locally, but let's say you wanted to share your repo with many other developers without making them download extra source code. Then your would publish the other libraries to a remote server.

以上适用于本地工作,但假设您想与许多其他开发人员共享您的存储库,而无需让他们下载额外的源代码。然后您将其他库发布到远程服务器。

If your projects are public on GitHub, use a service like jitpack.io. You can also setup an account on Bintray OSS or Maven Central to have your libraries be available like most others.

如果您的项目在 GitHub 上公开的,请使用jitpack.io 之类的服务。您还可以在 Bintray OSS 或 Maven Central 上设置一个帐户,以使您的库像大多数其他人一样可用。

If your projects are private within your company, you will need some Maven type server, whether that is a generic web server, or Nexus or Artifactory, you can add that with an addition to the repositories block.

如果您的项目在您的公司内私有的,您将需要一些 Maven 类型的服务器,无论是通用 Web 服务器,还是 Nexus 或 Artifactory,您都可以将其添加到存储库块中。

repositories {
    maven { url "http://some.maven.site/" }
}

Then add the compileor implementationsources, as normal

然后像往常一样添加compileimplementation

回答by darwinbaisa

Finally Gradle 3.1 has sorted out this issue. Composite builds are now supported natively. More here. In short add this line to sandbox settings.gradle file-

Gradle 3.1 终于解决了这个问题。现在原生支持复合构建。更多在这里。简而言之,将此行添加到 sandbox settings.gradle 文件中-

includeBuild '<PATH>/myLib'

If you can't upgrade Gradle, then the only hope for you is to publish mylib artifact to local maven repo and add mavenLocal() to sandbox/build.gradle.

如果你不能升级Gradle,那么你唯一的希望就是将mylib artifact 发布到本地maven repo 并将mavenLocal() 添加到sandbox/build.gradle。

回答by Richard Tingle

This answer is outdated in modern gradle and intellij, please refer to darwinbaisa's answer

这个答案在现代 gradle 和 intellij 中已经过时,请参考darwinbaisa 的答案

Original answer

原答案

Its discussed in the article https://blog.jetbrains.com/idea/2016/10/intellij-idea-2016-3-eap-gradle-composite-builds-and-android-studio-2-2/

它在文章https://blog.jetbrains.com/idea/2016/10/intellij-idea-2016-3-eap-gradle-composite-builds-and-android-studio-2-2/ 中讨论过

Basically you need to use composite builds. So View > Tool windows > Gradle > Right Click on the project using the library > Composite Build Configuration > Tick the library project you want to use the local version of.

基本上你需要使用复合构建。所以查看>工具窗口> Gradle>右键单击使用库的项目>复合构建配置>勾选要使用本地版本的库项目。

Do a rebuild and then the local dependancy should be used

做一个重建,然后应该使用本地依赖