将 Gradle 项目转换成 Maven 需要的POM
引子
Gradle是一个很好用的编译工具。稍微有点不太方便的地方在于,有的开发环境只提供的Maven。所以有时候,我们需要利用 Gradle 生成 Maven 需要 POM.xml 文件。下面的方法可以把gradle转成maven项目,前提gradle项目目录结构保持跟maven一样的约定,即/src/main/java这一套。
生成 pom.xml
方式一:
1、打开 build.gradle 文件,在build.gradle中增加以下内容(group,version可自行修改,artifactId默认为目录名称)
apply plugin: 'java' apply plugin: 'maven' group = 'com.angei' version = '1.0.0' sourceCompatibility = 1.8
plugins的格式可以如下:
plugins { id 'java' id 'maven' } group = 'com.angei' version = '1.0.0' sourceCompatibility = 1.8
2、在终端下执行
gradle install
成功后,会在 build目录下的 poms 文件夹下生成 pom-default.xml,将其改名为 pom.xml 拷贝到项目的根目录下即可。
生成的pom-default.xml如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.angei</groupId> 6 <artifactId>gradle-first</artifactId> 7 <version>1.0.0</version> 8 <dependencies> 9 <dependency> 10 <groupId>junit</groupId> 11 <artifactId>junit</artifactId> 12 <version>4.12</version> 13 <scope>test</scope> 14 </dependency> 15 </dependencies> 16 </project>
方式二:
1、在build.gradle的文件中添加着色部分的内容:
1 buildscript { 2 repositories { 3 mavenCentral() 4 } 5 dependencies { 6 classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE") 7 } 8 } 9 10 apply plugin: 'java' 11 apply plugin: 'jacoco' 12 apply plugin: 'spring-boot' 13 apply plugin: 'maven' 14 15 sourceCompatibility = 1.8 16 compileJava.options.encoding = 'UTF-8' 17 18 19 group = 'com.101tec' 20 // Options for naming the JAR file 21 archivesBaseName = 'trackr-backend' 22 version = '1.0' 23 if(project.hasProperty('teamcity')) { 24 version += '-build-' + project.teamcity['build.number'] 25 } else { 26 version += '-localbuild' 27 } 28 29 30 sourceCompatibility = 1.8 31 32 repositories { 33 mavenCentral() 34 } 35 36 springBoot { 37 executable = true 38 } 39 40 dependencies { 41 compile "org.springframework.boot:spring-boot-starter-data-rest" 42 compile "org.springframework.boot:spring-boot-starter-data-jpa" 43 compile "org.springframework.boot:spring-boot-starter-mail" 44 compile "org.springframework.boot:spring-boot-starter-integration" 45 compile "org.springframework.boot:spring-boot-starter-security" 46 47 // not included in boot 48 compile "org.springframework.integration:spring-integration-mail:4.2.5.RELEASE" 49 compile "org.springframework.security.oauth:spring-security-oauth2:2.0.11.RELEASE" 50 51 compile "com.h2database:h2" 52 compile "postgresql:postgresql:9.1-901.jdbc4" 53 compile "org.flywaydb:flyway-core" 54 55 compile("org.xhtmlrenderer:flying-saucer-pdf-itext5:9.0.6") 56 compile("org.thymeleaf:thymeleaf:2.1.3.RELEASE") 57 58 compile "org.projectlombok:lombok:1.12.4" 59 60 compile "org.glassfish:javax.json:1.0" 61 62 63 testCompile "org.springframework.boot:spring-boot-starter-test" 64 testCompile("org.echocat.jomon:testing:1.4.3") { 65 exclude group: "org.mockito" 66 } 67 testCompile "org.mockito:mockito-core:1.9.5" 68 testCompile "com.jayway.jsonpath:json-path" 69 testCompile "org.apache.httpcomponents:httpclient" 70 } 71 72 73 task wrapper(type: Wrapper) { 74 gradleVersion = '2.2' 75 } 76 77 task createPom << { 78 pom { 79 project { 80 groupId 'com.angei' 81 artifactId 'gradle-first' 82 version '1.0.0' 83 } 84 }.writeTo("$buildDir/pom.xml") 85 }
其中这个createPom的task改成任何你自己喜欢的名字,如createPom、mavenPom、testPom等,然后只需要执行。
2、在终端执行:
gradle createPom
执行成功如图
生成的Pom.xml内容如下:
<?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.angei</groupId>
<artifactId>gradle-first</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.feidee.laoyeye</groupId>
<artifactId>feidee-laoyeye-service-dto</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.feidee.finance</groupId>
<artifactId>finance-lib-javaweb</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>1.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>1.3.7.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>kafka-clients</artifactId>
<groupId>org.apache.kafka</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>