1. 序章

このチュートリアルでは、GradleビルドファイルをMavenPOMファイルに変換する方法を見ていきます。 We’ll use Gradle version 7.2 for our example and we’ll also explore a few available customization options.

2. Gradleビルドファイル

Let’s start with a standard Gradle Java project, gradle-to-maven, with the following build.gradle file:

repositories {
    mavenCentral()
}

group = 'com.baeldung'
version = '0.0.1'

apply plugin: 'java'

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.25'
    testImplementation 'junit:junit:4.12'
}

3. Mavenプラグイン

GradleにはMavenプラグインが付属しており、GradleファイルをMavenPOMファイルに変換するためのサポートが追加されています。 また、アーティファクトをMavenリポジトリーにデプロイすることもできます。

To use this, let’s add the Maven Publish plugin to our build.gradle file:

apply plugin: 'maven-publish'

プラグインは、Gradleファイルにあるgroupversionを使用して、それらをPOMファイルに追加します。 また、ディレクトリ名からArtifactIdを自動的に取得します。

The plugin automatically adds the publish task as well. So to convert, let’s add a basic definition for our publishing to the POM file:

publishing {
    publications {
        customLibrary(MavenPublication) {
            from components.java
        }
    }

    repositories {
        maven {
            name = 'sampleRepo'
            url = layout.buildDirectory.dir("repo")
        }
    }
}

Now we can publish our customLibrary to a local directory-based repository for demonstration purpose:

gradle publish

Running the above command creates a build directory with these sub-directories:

  • libs – $ {artifactId}-$ {version}.jarという名前のjarが含まれています
  • publications/customLibrarycontaining the converted POM file with the name pom-default.xml
  • tmp / jar –マニフェストを含む
  • repo – the file system location used as the repository containing the published artifact

生成されたPOMファイルは次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.baeldung</groupId>
  <artifactId>gradle-to-maven</artifactId>
  <version>0.0.1</version>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

Note that test scope dependencies are not included in the POM and the default runtime scope is assigned to all other dependencies.

The publish task also uploads this POM file and the JAR to the specified repository.

4. Mavenプラグインのカスタマイズ

場合によっては、生成されたPOMファイルのプロジェクト情報をカスタマイズすると便利なことがあります。 見てみましょう。

4.1. groupId、 ArtifactId、およびバージョン

Changing the groupId, artifactId and the version of the POM can be handled in the publications/{publicationName} block:

publishing {
    publications {
        customLibrary(MavenPublishing) {
            groupId = 'com.baeldung.sample'
            artifactId = 'gradle-maven-converter'
            version = '0.0.1-maven'
        }
    }
}

Running the publish task now produces the POM file with the information provided above:

<groupId>com.baeldung.sample</groupId>
<artifactId>gradle-maven-converter</artifactId>
<version>0.0.1-maven</version>

4.2. 自動生成されたコンテンツ

Mavenプラグインを使用すると、生成されたPOM要素を簡単に変更することもできます。 For example, to change the default runtime scope to compile, we can add the below closure to the pom.withXml method:

pom.withXml {
    asNode()
      .dependencies
      .dependency
      .findAll { dependency ->
        // find all dependencies with runtime scope
        dependency.scope.text() == 'runtime'
      }
      .each { dependency ->
        // set the scope to 'compile'
        dependency.scope*.value = 'compile'
      }
}

This will change the scope for all the dependencies in the generated POM file:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.25</version>
  <scope>compile</scope>
</dependency>

4.3. 追加情報

Finally, if we want to add additional information, we can include such Maven-supported elements to the pom function.

いくつかのライセンス情報を追加しましょう:

...
pom {
    licenses {
        license {
            name = 'The Apache License, Version 2.0'
            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
        }
    }
}
...

これで、POMに追加されたライセンス情報を確認できます。

... 
<licenses>
  <license>
    <name>The Apache License, Version 2.0</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
  </license>
</licenses>
...

5. 結論

このクイックチュートリアルでは、GradleビルドファイルをMavenPOMに変換する方法を学びました。

いつものように、この記事のソースコードはGitHubにあります。