ビルドツール
Javaのビルドツール
Section titled “Javaのビルドツール”Javaプロジェクトでは、依存関係の管理、コンパイル、テスト、パッケージングなどの作業を自動化するためにビルドツールを使用します。この章では、主要なビルドツールについて解説します。
ビルドツールとは
Section titled “ビルドツールとは”ビルドツールは、以下の作業を自動化するツールです:
- 依存関係の管理: 外部ライブラリのダウンロードと管理
- コンパイル: Javaソースコードのコンパイル
- テスト: ユニットテストの実行
- パッケージング: JARやWARファイルの作成
- デプロイ: アーティファクトの配布
主要なビルドツール
Section titled “主要なビルドツール”1. Maven
Section titled “1. Maven”Mavenは、XMLベースの設定ファイル(pom.xml)を使用するビルドツールです。
特徴:
- 設定がXMLベースで標準化されている
- 豊富なプラグインエコシステム
- 中央リポジトリ(Maven Central)へのアクセス
- プロジェクト構造が標準化されている
pom.xmlの例:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0.0</version> <packaging>jar</packaging>
<name>My Application</name> <description>Spring Boot application</description>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.0</version> <relativePath/> </parent>
<properties> <java.version>17</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>Mavenの主要コマンド:
# プロジェクトのコンパイルmvn compile
# テストの実行mvn test
# パッケージの作成mvn package
# クリーンビルドmvn clean package
# 依存関係のインストールmvn install
# Spring Bootアプリケーションの実行mvn spring-boot:runMavenのディレクトリ構造:
my-app/├── src/│ ├── main/│ │ ├── java/│ │ └── resources/│ └── test/│ ├── java/│ └── resources/└── pom.xml2. Gradle
Section titled “2. Gradle”Gradleは、GroovyまたはKotlin DSLを使用するビルドツールです。Mavenよりも柔軟で、ビルドスクリプトが簡潔です。
特徴:
- GroovyまたはKotlin DSLによる柔軟な設定
- インクリメンタルビルドによる高速化
- 豊富なプラグイン
- Mavenリポジトリとの互換性
build.gradleの例(Groovy DSL):
plugins { id 'java' id 'org.springframework.boot' version '3.1.0' id 'io.spring.dependency-management' version '1.1.0'}
group = 'com.example'version = '1.0.0'sourceCompatibility = '17'
repositories { mavenCentral()}
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'org.postgresql:postgresql' testImplementation 'org.springframework.boot:spring-boot-starter-test'}
tasks.named('test') { useJUnitPlatform()}build.gradle.ktsの例(Kotlin DSL):
plugins { java id("org.springframework.boot") version "3.1.0" id("io.spring.dependency-management") version "1.1.0"}
group = "com.example"version = "1.0.0"java.sourceCompatibility = JavaVersion.VERSION_17
repositories { mavenCentral()}
dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-data-jpa") runtimeOnly("org.postgresql:postgresql") testImplementation("org.springframework.boot:spring-boot-starter-test")}
tasks.withType<Test> { useJUnitPlatform()}Gradleの主要コマンド:
# プロジェクトのビルド./gradlew build
# テストの実行./gradlew test
# クリーンビルド./gradlew clean build
# Spring Bootアプリケーションの実行./gradlew bootRun
# 依存関係の表示./gradlew dependenciesGradleのディレクトリ構造:
my-app/├── src/│ ├── main/│ │ ├── java/│ │ └── resources/│ └── test/│ ├── java/│ └── resources/├── build.gradle└── settings.gradleMavenとGradleの比較
Section titled “MavenとGradleの比較”| 項目 | Maven | Gradle |
|---|---|---|
| 設定ファイル | XML (pom.xml) | Groovy/Kotlin (build.gradle) |
| 学習曲線 | 比較的緩やか | やや急 |
| ビルド速度 | 標準的 | 高速(インクリメンタルビルド) |
| 柔軟性 | 標準的 | 高い |
| プラグイン | 豊富 | 豊富 |
| 可読性 | XMLは冗長 | DSLは簡潔 |
| 企業での採用 | 広く採用 | 増加傾向 |
どちらを選ぶべきか
Section titled “どちらを選ぶべきか”Mavenを選ぶ場合
Section titled “Mavenを選ぶ場合”- 標準化を重視: XMLベースの標準的な設定
- 学習コストを抑えたい: 多くの開発者がMavenに慣れている
- 既存プロジェクト: 既にMavenを使用しているプロジェクト
- シンプルなプロジェクト: 複雑なビルドロジックが不要
Gradleを選ぶ場合
Section titled “Gradleを選ぶ場合”- ビルド速度を重視: インクリメンタルビルドによる高速化
- 柔軟性が必要: 複雑なビルドロジックを実装したい
- 大規模プロジェクト: マルチモジュールプロジェクト
- 最新技術: Kotlin DSLを使用したい
Spring Bootでの推奨
Section titled “Spring Bootでの推奨”Spring Bootは、MavenとGradleの両方をサポートしています。Spring Initializr(https://start.spring.io/)では、どちらも選択できます。
Spring Boot + Maven:
- 標準的で安定している
- 多くのチュートリアルやドキュメントが利用可能
- 企業での採用実績が豊富
Spring Boot + Gradle:
- ビルド速度が速い
- 設定が簡潔
- モダンな開発スタイル
依存関係のスコープ
Section titled “依存関係のスコープ”Mavenのスコープ
Section titled “Mavenのスコープ”<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> <!-- 実行時のみ必要 --></dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> <!-- テスト時のみ必要 --></dependency>主なスコープ:
compile: デフォルト、すべてのクラスパスで利用可能provided: コンパイル時とテスト時に必要、実行時は提供されるruntime: 実行時のみ必要test: テスト時のみ必要
Gradleの設定
Section titled “Gradleの設定”dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' // コンパイル時と実行時 compileOnly 'org.projectlombok:lombok' // コンパイル時のみ runtimeOnly 'org.postgresql:postgresql' // 実行時のみ testImplementation 'junit:junit' // テスト時のみ}主な設定:
implementation: コンパイル時と実行時に必要compileOnly: コンパイル時のみ必要runtimeOnly: 実行時のみ必要testImplementation: テスト時のみ必要
ビルドツールのインストール
Section titled “ビルドツールのインストール”Mavenのインストール
Section titled “Mavenのインストール”macOS:
brew install mavenWindows:
choco install mavenLinux:
sudo apt install mavenGradleのインストール
Section titled “Gradleのインストール”macOS:
brew install gradleWindows:
choco install gradleLinux:
sudo apt install gradle確認:
mvn -versiongradle -versionJavaのビルドツールの選択:
- Maven: XMLベースの標準的なビルドツール、広く採用されている
- Gradle: Groovy/Kotlin DSLによる柔軟なビルドツール、高速なビルド
Spring Bootプロジェクトでは、どちらも使用できますが、プロジェクトの要件やチームの経験に応じて選択することが重要です。一般的には、Mavenは標準的で安定しており、Gradleは柔軟で高速です。