引子
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
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 4.0.0
5 com.angei
6 gradle-first
7 1.0.0
8
9
10 junit
11 junit
12 4.12
13 test
14
15
16
方式二:
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 <
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内容如下:
4.0.0
com.angei
gradle-first
1.0.0
com.feidee.laoyeye
feidee-laoyeye-service-dto
1.0.0-SNAPSHOT
compile
com.feidee.finance
finance-lib-javaweb
1.0.0-SNAPSHOT
compile
org.springframework.boot
spring-boot-starter-web
compile
org.springframework.boot
spring-boot-starter-cache
compile
org.springframework.boot
spring-boot-starter-data-jpa
compile
org.springframework.boot
spring-boot-starter-data-redis
compile
org.springframework.boot
spring-boot-starter-aop
compile
org.springframework.boot
spring-boot-starter-validation
compile
org.springframework.boot
spring-boot-starter-security
compile
org.springframework.boot
spring-boot-starter-freemarker
compile
org.apache.kafka
kafka-clients
1.1.0
compile
org.springframework.kafka
spring-kafka
1.3.7.RELEASE
compile
kafka-clients
org.apache.kafka
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.codehaus.groovy
groovy-all
test
org.testng
testng
6.13.1
test