# Java ライブラリを Maven リポジトリに公開する

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/ja-jp/help/idea/llms.txt).

このチュートリアルの目的は、Gradle プロジェクトで作成された Java ライブラリをローカルの Maven リポジトリに公開し、次に IntelliJ IDEA を使用してリモートリポジトリに公開する方法を示すことです。

Gradle プロジェクトの作成から始めましょう。

Procedure: 新規 Gradle プロジェクトを作成する

1. IntelliJ IDEA ホーム画面で、  `新規 | プロジェクト`  を選択します。

![ホーム画面から新しいプロジェクトを作成する](https://resources.jetbrains.com/help/img/idea/2026.3/ij_home_screen_new_project.png)

開始点が IntelliJ IDEA ですでに開かれているプロジェクトである場合は、メインメニューの  `ファイル | 新規 | プロジェクト`  に移動します。

2. 開いたページで、左側のオプションリストから「   Java   」を選択します。 プロジェクト名を入力します。 ここでは  gradle-publish  とし、   Gradle   を選択し、残りのオプションはデフォルトのままにして、   作成   をクリックします。

![新規プロジェクトダイアログ: Gradle を選択](https://resources.jetbrains.com/help/img/idea/2026.3/new_project_gradle.png)

IntelliJ IDEA は Gradle プロジェクトを作成し、   Gradle   ツールウィンドウを有効にします。

次に、Java ライブラリのサポートを追加してプロジェクトをビルドするため、   ビルド.gradle.kts   ファイルを少し調整します。

Procedure: build.gradle.kts を編集する

1. プロジェクト   ツールウィンドウで  `ビルド.gradle.kts`  ファイルをダブルクリックして、エディターで開きます。

この時点で、  `ビルド.gradle.kts`  には次のコードが含まれています。

```KOTLIN
plugins {
    id("java")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:6.0.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.test {
    useJUnitPlatform()
}

```

2. `plugins`  セクションで、  `"java"`  を  `"java-library"`  に変更します。

```KOTLIN
plugins {
    id("java-library")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:6.0.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.test {
    useJUnitPlatform()
}

```

3. エディターで ![the Sync Gradle Changes icon](https://resources.jetbrains.com/help/img/idea/2026.3/gradle.icons.gradleLoadChanges.svg) (   Gradle の変更を同期   ) をクリックして、変更をプロジェクトと同期します。

4. `src/main/java`  ディレクトリを使用して、ライブラリのコードを追加します。

5. 次に、   Gradle   ツールウィンドウでプロジェクトノードをクリックし、   タスク   をクリックして、   ビルド   。

6. 開いたリストで   ビルド   をダブルクリックして、  `.jar`  ファイルを生成するビルドタスクを実行します。

![Gradle ツールウィンドウ](https://resources.jetbrains.com/help/img/idea/2026.3/gradle_build_task.png)

その結果、生成された  `.jar`  ファイルが   プロジェクト   ツールウィンドウの  `build/libs`  ディレクトリ内に配置されます。

![プロジェクトツールウィンドウ](https://resources.jetbrains.com/help/img/idea/2026.3/gradle_publish_jar.png)

それでは、Maven の規則に従って、ライブラリの Maven 座標を指定しましょう。 プロジェクト作成時に IntelliJ IDEA によって  `GroupId`  と  `バージョン`  がすでに追加されているため、変更する必要があるのは  `ArtifactId`  のみです。

Procedure: ArtifactId を変更して JAR ファイルを生成する

1. プロジェクト   ツールウィンドウで  `settings.gradle.kts`  ファイルをダブルクリックして、エディターで開きます。  `rootProject.name`  を  `gradle-publish`  から  `my-artifact-id`  に変更します。

```KOTLIN
rootProject.name = "my-artifact-id"

```

2. 変更を同期するには、 ![the Sync Gradle Changes icon](https://resources.jetbrains.com/help/img/idea/2026.3/gradle.icons.gradleLoadChanges.svg)   Gradle の変更を同期   をクリックします。

3. Gradle   ツールウィンドウで、   タスク   をクリックします。

4. ビルド   ディレクトリで、最初に   クリーン   タスクをダブルクリックして実行し、次に   ビルド   タスクを実行します。

IntelliJ IDEA は、Maven の命名規則および更新されたアーティファクト名に沿った情報を含む  `.jar`  ファイルを生成します。

![プロジェクトツールウィンドウ](https://resources.jetbrains.com/help/img/idea/2026.3/my_artifact_jar.png)

次に、ビルドスクリプトをさらに操作して、ライブラリをローカルの Maven リポジトリに公開します。

Procedure: ライブラリをローカル Maven リポジトリに公開する

1. ビルド.gradle.kts   ファイルを開き、  `id("maven-publish")`  を  `plugins`  セクションに追加します。

2. ![the Sync Gradle Changes icon](https://resources.jetbrains.com/help/img/idea/2026.3/gradle.icons.gradleLoadChanges.svg) (   Gradle の変更を同期   ) をクリックして、変更をプロジェクトと同期します。

3. Gradle   ツールウィンドウの   公開   セクションで、   Maven ローカルに公開   をダブルクリックしてタスクを実行します。

IntelliJ IDEA は JAR ファイルを作成し、オペレーティングシステムに応じて次のディレクトリに追加します:

macOS:
`/Users/<user_name>/.m2/[Artifact GroupID]/[Artifact Name]/[Artifact Version]`

Linux:
`/home/<user_name>/.m2/[Artifact GroupID]/[Artifact Name]/[Artifact Version]`

Windows:
`C:\Users\<User_Name>\.m2\[Artifact GroupID]\[Artifact Name]\[Artifact Version]`

`build.gradle`  ファイルをさらに編集して、ライブラリをリモートリポジトリに公開できます。

Procedure: リモートリポジトリに公開する

1. ビルド.gradle.kts   ファイルに次のセクションを追加します。

```KOTLIN
publishing {
    publications {
        create<MavenPublication>("myLib") {
            from(components["java"])
        }
    }
    repositories {
        maven {
            name = "MyRepo" // optional target repository name
            url = uri("http://my.org.server/repo/url")
            credentials {
                username = "alice"
                password = "my-password"
            }
        }
    }
}
                
```

2. ![the Sync Gradle Changes icon](https://resources.jetbrains.com/help/img/idea/2026.3/gradle.icons.gradleLoadChanges.svg) (   Gradle の変更を同期   ) をクリックして、プロジェクトへの変更をロードします。

3. Gradle   ツールウィンドウで   公開   セクションを開き、   すべての公開物を MyRepository に公開   をダブルクリックしてタスクを実行します。

![Gradle ツールウィンドウ: タスクを公開する](https://resources.jetbrains.com/help/img/idea/2026.3/gradle_tool_window_publish_task.png)

POM ファイルのカスタマイズ、別のスナップショットの使用、リポジトリの解放の詳細については、「 [Gradle のドキュメント(英語)](https://docs.gradle.org/current/userguide/publishing_maven.html)」を参照してください。

