我可以使用要部署到Nexus的Gradle maven插件创建Maven POM(BOM)版本吗?

时间:2017-04-12 16:09:19

标签: maven gradle nexus spring-boot-gradle-plugin maven-bom

我有一个Gradle项目,它使用Spring's dependency management plugin来定义依赖项版本列表。我还使用Maven plugin将项目部署到Maven存储库。

我希望能够将其部署为Maven物料清单(BOM),以便我可以在其他Gradle项目中使用它来定义我的依赖项版本。只要我还部署了一个JAR文件,我就能够使用它。但是,JAR完全是空的,是多余的。我的目标是生成和部署POM文件,就像我可以做的那样,如果这是一个带有" pom"的Maven项目。包装

如果我从要发布的工件列表中手动排除JAR,则不会安装任何内容,甚至不会安装POM文件。

这是一个用于演示此问题的测试版本:

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' }
//}

以上正确生成并将以下POM文件安装到我当地的Maven仓库中:

<?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>

但是,除了MANIFEST.MF文件外,它还会安装一个空的JAR文件。

我能够使用maven-publish插件成功实现此功能。但是,我也使用Gradle Sonatype Nexus plugin将工件发布到Nexus实例。由于这是基于maven插件,maven-publish插件无法满足我的需求。以下是我需要添加以使用maven-publish插件:

apply plugin: 'maven-publish'
publishing {
  publications {
    maven(MavenPublication) {
    }
  }
}

有没有办法使用maven Gradle插件生成和部署POM文件,就像我可以做的那样,如果这是一个带有&#34; pom&#34;的Maven项目。包装

4 个答案:

答案 0 :(得分:4)

在 Gradle 6+ 版本中,我们可以使用 Gradle Java Platform Plugin 发布一个 maven-bom,而无需很多配置和脚本。

group 'test.platform.simple.bom'
version '1.0.0-SNAPSHOT'

repositories {
    maven {
        mavenCentral()
    }        
}

apply plugin: 'java-platform'
apply plugin: 'maven-publish'

javaPlatform {
    allowDependencies()
}

dependencies {
    constraints {
        api 'cglib:cglib-nodep:3.2.4'
        api 'junit:junit:4.12'
        // runtime 'org.postgresql:postgresql:42.2.5' <-- runtime constraint
        // api project(":core") <-- constraint from local project
        // api platform('com.fasterxml.jackson:jackson-bom:2.9.8') <-- constraint from another platform
    }
}

publishing {
  publications {
    maven(MavenPublication) {
      from components.javaPlatform
    }
  }
}

要管理的依赖项可以在依赖项下定义为 api or runtime constraints约束可用于管理依赖项 from a local projectfrom another platform/bom。请注意,我们需要配置一个使用 javaPlatform 组件的 Maven 发布以将其发布为 Maven bom 工件。

答案 1 :(得分:3)

acdcjunioranswer可以有所改善。 build.gradle中的依赖项可以在标准dependencies部分中声明。另外,在pom.xml的BOM表中,版本应在dependencyManagement部分中声明:

plugins {
    id 'java-library'
    id 'maven-publish'
}

group = 'com.example'
version = '1.0.0'

repositories {
    mavenCentral()
}

dependencies {
    api 'org.apache.commons:commons-lang3:3.9'
    api 'org.postgresql:postgresql:42.2.11'
}

publishing {
    repositories {
        maven {
            url = "$nexusUrl"
            credentials {
                username = "$nexusUsername"
                password = "$nexusPassword"
            }
        }
    }

    publications {
        maven(MavenPublication) {
            groupId = "${project.group}"
            artifactId = "${project.name}"
            version = "${project.version}"

            pom.withXml {
                asNode().children().last() + {
                    resolveStrategy = Closure.DELEGATE_FIRST

                    name 'My BOM'
                    description 'My Bill of Materials (BOM)'

                    dependencyManagement {
                        dependencies {
                            project.configurations.each { conf ->
                                conf.dependencies.each { dep ->
                                    dependency {
                                        groupId "${dep.group}"
                                        artifactId "${dep.name}"
                                        version "${dep.version}"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

生成的pom.xml可以使用以下命令发布到Nexus

./gradlew clean build publish -i

或本地Maven仓库(~/.m2/repository

./gradlew clean build pTML -i

该符号不仅更短,而且允许处理依赖性。例如,使用OWASP Dependency-Check插件执行漏洞扫描:

plugins {
    //...
    id 'org.owasp.dependencycheck' version '5.3.0'
}

dependencyCheck {
    failBuildOnCVSS = 9 //Critical Severity
}

check.dependsOn dependencyCheckAnalyze

答案 2 :(得分:1)

您应该考虑具有DSL的插件以gradle方式创建BOM:

https://github.com/xvik/gradle-pom-plugin

于连

答案 3 :(得分:0)

您可以使用build.gradle,例如:

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
}

使用以下示例的项目:https://github.com/acdcjunior/domain-id/blob/master/domain-id-all/build.gradle