问题
I have a Gradle project which uses Spring's dependency management plugin to define a list of dependency versions. I am also using the Maven plugin to deploy the project to a Maven repository.
I would like to be able to deploy this as a Maven bill of materials (BOM) so that I can use it in other Gradle projects to define my dependency versions. I have been able to get this to work so long as I also deploy a JAR file. However, the JAR is completely empty and superfluous. My goal is to generate and deploy just the POM file, like I would be able to do if this were a Maven project with a "pom" packaging.
If I manually exclude the JAR from the list of artifacts to be published, then nothing gets installed, not even the POM file.
This is a test build to demonstrate the issue:
group 'test'
version '1.0.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE' //Matches the Spring IO version
}
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
dependencyManagement {
dependencies {
dependency 'cglib:cglib-nodep:3.2.4'
dependency 'junit:junit:4.12'
}
}
////Uncommenting this causes nothing at all to be deployed:
//jar.enabled = false
//configurations.archives.artifacts.with { archives ->
// archives.removeAll { it.type == 'jar' }
//}
The above correctly produces and installs the following POM file into my local Maven repo:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>gradle-pom-packaging-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
However, it also installs a JAR file that is empty save for the MANIFEST.MF file.
I was able to successfully get this working using the maven-publish
plugin. However, I'm also making use of the Gradle Sonatype Nexus plugin to publish the artifact to a Nexus instance. As this builds upon the maven
plugin, the maven-publish
plugin will not work for my needs. The following is all I needed to add to get it working with the maven-publish
plugin:
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
}
}
}
Is there a way to generate and deploy just the POM file using the maven
Gradle plugin, like I would be able to do if this were a Maven project with a "pom" packaging?
回答1:
You should consider that plugin which has a DSL to create BOMs the gradle way :
https://github.com/xvik/gradle-pom-plugin
Julien
回答2:
You could have a build.gradle
such as:
apply plugin: 'maven-publish'
apply plugin: 'signing'
publishing {
repositories {
maven {
def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username ossrhUsername
password ossrhPassword
}
}
}
publications {
maven(MavenPublication) {
groupId = 'com.example.id'
artifactId = 'my-artifact-id'
version = '1.0.0'
pom.withXml {
asNode().children().last() + {
resolveStrategy = Closure.DELEGATE_FIRST
name 'My Lib Name'
description 'My Lib Description'
url 'https://example.com/id/lib'
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
scm {
connection 'scm:git:git@example.com:acdcjunior/lib/id.git'
developerConnection 'scm:git:git@example.com:acdcjunior/lib/id.git'
url 'git@example.lib/id.git'
}
developers {
developer {
id 'someone'
name 'Someone Name'
email 'someone@example.com'
}
}
dependencies {
dependency {
groupId 'com.example.other'
artifactId 'some-dependency'
version '1.0.0'
}
dependency {
groupId 'org.apache.commons'
artifactId 'commons-lang3'
version '3.9'
}
}
}
}
}
}
}
signing {
sign publishing.publications.maven
}
Example of a project using this: https://github.com/acdcjunior/domain-id/blob/master/domain-id-all/build.gradle
来源:https://stackoverflow.com/questions/43374613/can-i-create-a-maven-pom-only-bom-build-using-the-gradle-maven-plugin-to-be-de