Converting Gradle Build File to Maven POM – 将Gradle构建文件转换为Maven POM

最后修改: 2020年 2月 29日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll take a look at how to convert a Gradle build file to a Maven POM file. We’ll use Gradle version 7.2 for our example and we’ll also explore a few available customization options.

在本教程中,我们将看看如何将Gradle构建文件转换为Maven POM文件。我们将以Gradle 7.2版为例,探讨一些可用的定制选项。

2. Gradle Build File

2.Gradle构建文件

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

让我们从一个标准的Gradle Java项目开始,gradle-to-maven,其build.gradle文件如下。

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 Plugin

3.Maven Plugin

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories.

Gradle带有Maven插件,它支持将Gradle文件转换为Maven POM文件。它还可以将工件部署到Maven资源库。

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

为了使用它,我们把Maven Publish插件添加到我们的build.gradle文件中。

apply plugin: 'maven-publish'

The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

该插件使用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:

该插件也会自动添加publish任务。所以为了转换,让我们在POM文件中为我们的发布添加一个基本定义。

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:

现在我们可以把我们的customLibrary发布到一个基于本地目录的资源库,以达到演示的目的。

gradle publish

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

运行上述命令会创建一个build目录,其中有这些子目录。

  • libs – containing the jar with the name ${artifactId}-${version}.jar
  • publications/customLibrarycontaining the converted POM file with the name pom-default.xml
  • tmp/jar – containing the manifest
  • repo – the file system location used as the repository containing the published artifact

The generated POM file will look like this:

生成的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.

注意,测试范围的依赖不包括在POM中,默认的runtime范围被分配给所有其他依赖

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

publish任务也将这个POM文件和JAR上传到指定的存储库。

4. Customizing the Maven Plugin

4.定制Maven插件

In some cases, it may be useful to customize the project information in the generated POM file. Let’s take a look.

在某些情况下,定制生成的POM文件中的项目信息可能是有用的。让我们来看看。

4.1. groupId, artifactId, and version

4.1.groupId, artifactId,version

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

改变POM的groupIdartifactIdversion可以在publications/{publicationName}块中处理。

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:

运行 publish任务现在会产生带有上述信息的POM文件。

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

4.2. Auto-generated Content

4.2.自动生成的内容

The Maven plugin also makes it straightforward to change any of the generated POM elements. For example, to change the default runtime scope to compile, we can add the below closure to the pom.withXml method:

Maven插件还可以直接改变生成的任何POM元素。例如,为了将默认的runtime范围改为compile,我们可以在pom.withXml方法中添加以下闭包。

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:

这将改变生成的POM文件中所有依赖关系的范围。

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

4.3. Additional Information

4.3.其他信息

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

最后,如果我们想添加额外的信息,我们可以在pom函数中加入此类Maven支持的元素。

Let’s add some license information:

让我们添加一些许可证信息。

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

We can now see license information added to the POM:

现在我们可以看到许可证信息被添加到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. Conclusion

5.总结

In this quick tutorial, we learned how to convert a Gradle build file to Maven POM.

在这个快速教程中,我们学习了如何将Gradle构建文件转换为Maven POM。

As always, the source code from this article can be found over on GitHub.

像往常一样,本文的源代码可以在GitHub上找到over