Gradle build.gradle到Maven pom.xml

我有一个Gradle项目,我需要将其所有依赖关系转移到另一个Maven项目中使用。 换句话说,我怎样才能从build.gradle生成(或者我可以生成)pom.xml?

最内置的解决scheme可能是使用Maven插件中的archiveTask任务,这将在您的构build目录中的poms文件夹中生成一个POM。 http://www.gradle.org/docs/current/userguide/maven_plugin.html#sec:maven_pom_generation

当使用Gradle的Maven插件时 , 安装任务会自动添加到您的任务中,调用它将始终生成一个POM文件。

所以,如果你的build.gradle文件如下所示:

 apply plugin: 'java' apply plugin: 'maven' group = 'myGroup' // artifactId is taken by default, from folder name version = '0.1-SNAPSHOT' dependencies { compile 'commons-lang:commons-lang:2.3' } 

你可以在其文件夹中调用gradle install ,你会在build / poms子文件夹中find一个名为pom-default.xml的文件,它将包含依赖关系。 另外,与POM一起构build的JAR将在您的Maven本地回购中。

因为我不想在我的本地回购中安装任何东西,所以在阅读文档之后,我确实遵循了这个规定。 添加你的build.gradle

 apply plugin: 'maven' group = 'com.company.root' // artifactId is taken by default, from folder name version = '0.0.1-SNAPSHOT' task writeNewPom << { pom { project { inceptionYear '2014' licenses { license { name 'The Apache Software License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' } } } }.writeTo("pom.xml") } 

运行它gradle writeNewPom

@a_horse_with_no_name

使用groovy制作的gradle可以尝试在结束}项目块之后添加

 build{ plugins{ plugin{ groupId 'org.apache.maven.plugins' artifactId 'maven-compiler-plugin' configuration{ source '1.8' target '1.8' } } } } 

没有尝试,狂猜!

当你没有安装gradle的时候,“编写gradle任务来完成这个任务”并不是很有用,而是安装了这个具有依赖性的100MB野兽,而不是将gradle依赖关系转换成maven dependencies:

 cat build.gradle\ | awk '{$1=$1};1'\ | grep -i "compile "\ | sed -e "s/^compile //Ig" -e "s/^testCompile //Ig"\ | sed -e "s/\/\/.*//g"\ | sed -e "s/files(.*//g"\ | grep -v ^$\ | tr -d "'"\ | sed -e "s/\([-_[:alnum:]\.]*\):\([-_[:alnum:]\.]*\):\([-+_[:alnum:]\.]*\)/<dependency>\n\t<groupId>\1<\/groupId>\n\t<artifactId>\2<\/artifactId>\n\t<version>\3<\/version>\n<\/dependency>/g" 

这转换

 compile 'org.slf4j:slf4j-api:1.7.+' compile 'ch.qos.logback:logback-classic:1.1.+' compile 'commons-cli:commons-cli:1.3' 

 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.+</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.+</version> </dependency> <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.3</version> </dependency> 

手工创buildpom.xml的其余部分。