我有一个Gradle项目,需要将其所有依赖项传输并与另一个Maven项目一起使用。换句话说,我该如何从build.gradle生成pom.xml(或者我能否生成)?
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenCentral()
}
dependencies {
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
runtimeOnly group: 'ch.qos.logback', name:'logback-classic', version:'1.2.3'
testImplementation group: 'junit', name: 'junit', version: '4.12'
}
// the GAV of the generated POM can be set here
publishing {
publications {
maven(MavenPublication) {
groupId = 'edu.bbte.gradleex.mavenplugin'
artifactId = 'gradleex-mavenplugin'
version = '1.0.0-SNAPSHOT'
from components.java
}
}
}
gradle publishToLocalRepo
,然后您将在build/publications/maven子文件夹中找到一个名为pom-default.xml的文件。此外,构建的JAR与POM将在您的Maven本地存储库中。更确切地说,如果要省略发布到您的Maven本地存储库,则gradle generatePomFileForMavenPublication
任务执行实际生成。我不想在本地存储库中安装任何东西,所以在阅读文档后,我做了以下操作。将以下内容添加到您的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
由于Gradle使用Groovy编写,可以尝试在“}”项目块结束后添加 after
块。
build{
plugins{
plugin{
groupId 'org.apache.maven.plugins'
artifactId 'maven-compiler-plugin'
configuration{
source '1.8'
target '1.8'
}
}
}
}
没有尝试,猜测而已!
当你没有安装Gradle时,“编写Gradle任务完成此操作”并不是很有用。我制作了过滤器,将Gradle依赖项转换为Maven依赖项,而不是安装带有依赖项的100MB怪物:
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>
其余的pom.xml文件应该手动创建。
最常用的解决方案可能是使用 Maven 插件中的 archiveTask
任务,该任务将在您的构建目录中生成 poms
文件夹中的 pom 文件。详见http://www.gradle.org/docs/current/userguide/maven_plugin.html#sec:maven_pom_generation
archiveTask
任务。 - Stealth Rabbibuild.gradle
文件中。我将其粘贴到了我的末尾:apply plugin: 'maven'
apply plugin: 'java'
task writeNewPom doLast {
pom {
project {
groupId 'org.example'
artifactId 'test'
version '1.0.0'
inceptionYear '2008'
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}.writeTo("$buildDir/newpom.xml")
}
$ gradle writeNewPom
或者
$ /.gradlew writeNewPom
文件已生成并放置在$buildDir/newpom.xml
中
从这个gist中获取
maven-publish
插件。 - pirateofebay