Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

java - IntelliJ gradle add module dependency

Using IntelliJ 2016.2.
Using Gradle 2.14.1

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

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):

- myLib
- sandbox

Simple request ... I want to use myLib within the sandbox app. I want both modules in the same project in order to develop both.

I've tried adding a module dependency to sandbox for myLib within IntelliJ. No dice. I've tried adding a jar reference, no dice.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Local Modules

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

 - app/
    - build.gradle
    - src/main/java  # where your main class would be 
 - library/
    - build.gradle
    - src/main/java  # dependencies of the app module 
 - settings.gradle
 - build.gradle

In that top-level settings.gradle you have

include ':library', ':app'

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

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

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.

This is what you already doing when you add these lines to your project

implementation("org:artifact:version")

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.

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.

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

Then add the compile or implementation sources, as normal


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...