프로젝트 간 의존성
하위 프로젝트 간 의존성을 관리하고 올바른 빌드 순서와 아티팩트 해결을 보장합니다.
프로젝트 간 의존성은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Groovy & Gradle: JVM Automation and Build Engineering 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Introduction to Project Dependencies
In a multi-project Gradle build, different subprojects often need to work together. One subproject might produce a library that another subproject uses.
This is where inter-project dependencies come in! They define how subprojects rely on each other, ensuring the correct build order and artifact sharing.
- Inter-project dependencies: How one subproject uses code or outputs from another subproject.
- Crucial for modular applications.
Declaring a Basic Dependency
Declaring that one subproject depends on another is straightforward in Gradle. You use the project() function within your dependencies block.
For example, if your app subproject needs code from your lib subproject, you'd add this to app/build.gradle:
// app/build.gradle
dependencies {
implementation project(':lib')
}The :lib part refers to the path of the lib subproject relative to the root.
Setting Up Our Example
Let's imagine a simple multi-project setup. We'll have a root project, an app subproject, and a lib subproject.
Your settings.gradle would look like this:
// settings.gradle
rootProject.name = 'my-multi-project'
include 'app', 'lib'This tells Gradle about our two subprojects, app and lib.
`implementation` vs. `api`
When declaring inter-project dependencies, you'll typically use configurations like implementation or api.
implementation: The most common choice. The dependency is used internally by the subproject and is not exposed to consumers of that subproject.api: Exposes the dependency to consumers. Ifappdepends onlibviaapi, andotherAppdepends onapp, thenotherAppwill also seelib's API.
For internal use between subprojects, implementation is generally preferred for better encapsulation and faster builds.
The Library Subproject (`lib`)
First, let's create a simple class in our lib subproject that the app subproject will use. This class will provide a basic utility.
Inside lib/src/main/groovy/com/coddykit/LibUtils.groovy:
package com.coddykit
class LibUtils {
static String getGreeting() {
return "Hello from LibUtils!"
}
}App Subproject Using `lib`'s Code
Now, let's make our app subproject use the LibUtils class from our lib subproject. Remember, we declared implementation project(':lib') in app/build.gradle.
Try running this example:
package com.coddykit
import com.coddykit.LibUtils
class AppMain {
static void main(String[] args) {
String message = LibUtils.getGreeting()
System.out.println(message)
}
}Automatic Build Order
One of the biggest advantages of declaring inter-project dependencies is that Gradle automatically figures out the correct build order.
- If
appdepends onlib, Gradle ensures thatlibis compiled and its artifacts are available beforeappstarts its compilation. - This saves you from manually managing build sequences, especially in complex multi-project setups.
When you run gradle build from the root, Gradle will build lib first, then app.
Understanding Transitive Effects
What if your lib subproject itself depends on another subproject, say common?
// lib/build.gradle
dependencies {
implementation project(':common')
}
// app/build.gradle
dependencies {
implementation project(':lib')
}If app depends on lib, and lib depends on common, Gradle will ensure that common is also built and its artifacts are available transitively to app's classpath for compilation, but not necessarily for its API if implementation is used for lib.
Maintain Clean Dependencies
To keep your multi-project builds manageable and efficient, follow these best practices:
- Minimal Dependencies: Only declare dependencies on subprojects you truly need.
- Use
implementation: Preferimplementationoverapifor internal subproject dependencies to improve encapsulation and reduce rebuilds. - Clear Boundaries: Design your subprojects with clear responsibilities to avoid circular dependencies.
- Avoid Deep Paths: Keep subproject paths concise (e.g.,
:module:submoduleis fine, but avoid excessively long paths).
Dependency Declaration Check
You have a multi-project build with a web subproject and a core subproject. The web subproject needs to use classes from the core subproject for its internal logic, but not expose core's API to any further consumers of web.
Inter-Project Dependencies Recap
Great job! In this lesson, you learned how to manage dependencies between subprojects in a Gradle multi-project build.
- We covered how to declare dependencies using
project(':subproject'). - We discussed the importance of
implementationfor internal use. - You saw how Gradle automatically handles build order.
- We explored best practices for clean dependency management.
Mastering inter-project dependencies is key to building large, modular applications efficiently with Gradle!
자주 묻는 질문
“프로젝트 간 의존성” 강의는 무료인가요?
네 — “프로젝트 간 의존성” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Groovy & Gradle: JVM Automation and Build Engineering 강의 전체를 잠금 해제할 수 있습니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
“프로젝트 간 의존성”에서 뭘 배우나요?
하위 프로젝트 간 의존성을 관리하고 올바른 빌드 순서와 아티팩트 해결을 보장합니다. 브라우저에서 직접 실행하는 실습 코드로 Groovy & Gradle: JVM Automation and Build Engineering을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Groovy & Gradle: JVM Automation and Build Engineering을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Groovy & Gradle: JVM Automation and Build Engineering은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“프로젝트 간 의존성” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Groovy & Gradle: JVM Automation and Build Engineering 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Groovy & Gradle: JVM Automation and Build Engineering 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 모노레포 프로젝트 구조
- 하위 프로젝트와 구성
- 프로젝트 간 의존성
- 합성 빌드와 빌드 조합