サブプロジェクトと設定
サブプロジェクトを定義・設定し、マルチプロジェクトビルド内でどのように連携するかを理解します。
「サブプロジェクトと設定」はCoddyKit上の無料Groovy & Gradle: JVM Automation and Build Engineeringレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはGroovy & Gradle: JVM Automation and Build Engineering学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Groovy & Gradle: JVM Automation and Build Engineeringコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Welcome to Subprojects!
In large software projects, keeping everything in one giant build file can get messy. This is where subprojects come in handy!
Subprojects allow you to break down a big project into smaller, manageable modules. Think of them as mini-projects living within your main project.
- Modularity: Organize code logically.
- Reusability: Share common logic across modules.
- Faster Builds: Gradle can optimize builds by only working on changed subprojects.
Declaring Subprojects
To tell Gradle about your subprojects, you use the settings.gradle file. This file lives in your root project directory.
You use the include keyword, followed by the path to your subproject's directory. Each subproject usually resides in its own folder.
/* settings.gradle */
rootProject.name = 'my-multi-project'
include 'app'
include 'library'Subproject Directory Structure
Once declared in settings.gradle, your project structure will typically look like this:
my-multi-project/(root project)settings.gradlebuild.gradle(root build file)app/(subproject 1)build.gradle- ... (source code, resources)
library/(subproject 2)build.gradle- ... (source code, resources)
Each subproject gets its own build.gradle file.
Subproject Build Files
Just like your root project, each subproject has its own build.gradle file. This allows you to define specific configurations, dependencies, and tasks for that particular module.
For example, your app subproject might build a runnable application, while your library subproject might produce a reusable JAR.
/* library/build.gradle */
plugins {
id 'java'
}
group 'com.coddykit'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}Defining a Subproject Task
You can define tasks within a subproject's build.gradle file. These tasks are then specific to that subproject.
Let's add a simple task to our library subproject that prints a message.
/* library/build.gradle */
plugins {
id 'java'
}
group 'com.coddykit'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
task helloLibrary {
doLast {
println "Hello from the Library subproject!"
}
}Executing Subproject Tasks
To run a task belonging to a specific subproject from the root directory, you use a colon : to separate the subproject name and the task name.
For example, to run the helloLibrary task from our library subproject:
public class Main {
public static void main(String[] args) {
System.out.println("To run the task: gradle :library:helloLibrary");
System.out.println("Output will be: Hello from the Library subproject!");
}
}Common Configuration: allprojects
Sometimes, you want to apply the same configuration to all projects in your multi-project build (the root project and all its subprojects).
You can do this using the allprojects { ... } block in your root build.gradle file.
/* build.gradle (root) */
allprojects {
group 'com.coddykit.common'
version '1.0.0'
repositories {
mavenCentral()
}
}Targeting Specific Subprojects
While allprojects is great for common settings, you often need to configure individual subprojects from the root build file.
You can use the project(':subprojectName') { ... } block to apply configuration specifically to one subproject.
/* build.gradle (root) */
project(':app') {
apply plugin: 'application'
mainClassName = 'com.coddykit.app.MainApp'
}
project(':library') {
apply plugin: 'java-library'
}Sharing Properties
You can also share properties or variables from your root project to all subprojects. This is useful for things like consistent version numbers or build flags.
Use the ext block (for "extra properties") in the root build.gradle and access it with rootProject.ext.propertyName in subprojects.
/* build.gradle (root) */
ext {
groovyVersion = '3.0.9'
springBootVersion = '2.7.5'
}
allprojects {
// Access properties in subprojects
println "Groovy Version: ${rootProject.ext.groovyVersion}"
}Multi-Select Check
Which of the following statements about Gradle subprojects and their configuration are TRUE?
Subprojects Recap
Great job! You've learned the basics of setting up and configuring Gradle subprojects.
- Subprojects help organize large builds.
- They are declared in
settings.gradleusinginclude. - Each subproject has its own
build.gradle. - You can configure all projects using
allprojects { ... }or specific ones usingproject(':name') { ... }. - Tasks are run with the
:subproject:tasksyntax.
Next, we'll explore how subprojects can depend on each other!
よくある質問
「サブプロジェクトと設定」レッスンは無料ですか?
はい。「サブプロジェクトと設定」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Groovy & Gradle: JVM Automation and Build Engineeringコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Groovy & Gradle: JVM Automation and Build Engineeringコースには全4レッスンが含まれています。
「サブプロジェクトと設定」で何を学びますか?
サブプロジェクトを定義・設定し、マルチプロジェクトビルド内でどのように連携するかを理解します。 ブラウザで直接実行するハンズオンコードでGroovy & Gradle: JVM Automation and Build Engineeringを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Groovy & Gradle: JVM Automation and Build Engineeringを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのGroovy & Gradle: JVM Automation and Build Engineeringは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「サブプロジェクトと設定」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このGroovy & Gradle: JVM Automation and Build Engineeringレッスンでコードを書いて実行できますか?
はい。すべてのGroovy & Gradle: JVM Automation and Build Engineeringレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- モノレポのプロジェクト構成
- サブプロジェクトと設定
- プロジェクト間の依存関係
- コンポジットビルドとビルドの合成