使用gradle init命令将pom.xml转换为build.gradle
只要在执行gradle init的目录中存在pom.xml文件,就可以基于此文件转换为build.gradle和settings.xml。但是正如日志中提到的“Maven to Gradle conversion is an incubating feature.”,相信这个功能信任度较低,使用时应小心谨慎。
环境
Gradle版本为8.3。
源代码等
使用Spring Initializr创建一个基本的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>
运行gradle init命令。运行时的日志如下所示。
>gradle init
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
Found a Maven build. Generate a Gradle build from this? (default: yes) [yes, no] yes
Select build script DSL:
1: Kotlin
2: Groovy
Enter selection (default: Kotlin) [1..2] 2
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
> Task :init
Maven to Gradle conversion is an incubating feature.
For more information, please refer to https://docs.gradle.org/8.3/userguide/migrating_from_maven.html in the Gradle documentation.
BUILD SUCCESSFUL in 24s
2 actionable tasks: 2 executed
以下是生成的settings.xml文件。
/*
* This file was generated by the Gradle 'init' task.
*/
rootProject.name = 'demo'
以下是build.gradle文件
/*
* This file was generated by the Gradle 'init' task.
*/
plugins {
id 'java-library'
id 'maven-publish'
}
repositories {
mavenLocal()
maven {
url = uri('https://repo.maven.apache.org/maven2/')
}
}
dependencies {
api 'org.springframework.boot:spring-boot-starter:3.1.3'
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.1.3'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
description = 'demo'
java.sourceCompatibility = JavaVersion.VERSION_1_8
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
tasks.withType(Javadoc) {
options.encoding = 'UTF-8'
}
使用与Gradle无关的Spring Boot插件确实不会被自动转换。虽然在转换之后需要手动修正,但如果将其作为从Maven迁移到Gradle的起点,它可能是有用的。