gpt4 book ai didi

maven - 如何在 Gradle 中将包装设置为 pom 而不是默认为 jar

转载 作者:行者123 更新时间:2023-12-02 04:31:08 35 4
gpt4 key购买 nike

我有一个生成 Material list (BOM) 的项目。当我执行 gradle build 时,它会生成一个空 jar,其中仅包含一个 META-INF 文件夹。

但是我能够正确地将 pom (BOM) 发布到 Nexus,副作用是还上传了空 jar。

根据 maven 插件文档 https://docs.gradle.org/current/userguide/maven_plugin.html我们应该能够设置包装:

packaging archiveTask.extension

Here, uploadTask and archiveTask refer to the tasks used for uploading and generating the archive

如何将包装设置为pom?

Gradle 上传 pom 示例:

<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>com.ttt.a</groupId>
<artifactId>my-bom</artifactId>
<version>Something-SNAPSHOT</version>

当我用maven而不是gradle上传的时候,还有一个额外的:

  <packaging>pom</packaging>

更新:

完整的 build.gradle 配置:

buildscript {
repositories {
maven {
url "http://myrepo"
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.4.RELEASE"
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5"
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.7'
} }

apply plugin: 'java' apply plugin: 'maven' apply plugin: "io.spring.dependency-management" apply plugin: "jacoco" apply plugin: 'org.asciidoctor.convert' apply plugin: 'org.sonarqube'

group = project.properties['groupId'] version = project.properties['version'].toString()

description = """Bill of Materials"""

sourceCompatibility = 1.8 targetCompatibility = 1.8

ext {
xxx = '1.0.0'
yyy = '1.2.0'
... }

repositories {
maven {
url "http://myrepo"
} }

dependencyManagement {
dependencies {
dependency "com.myorg:xxx:${xxx}"
dependency "com.myorg:yyy:${yyy}"
...
} }

uploadArchives {
repositories {
mavenDeployer {
snapshotRepository(url: 'http://myrepo') {
authentication(userName: "$System.env.NEXUS_USER", password: "$System.env.NEXUS_PASSWORD")
}
}
} }

asciidoctor {
sourceDir = file('src/docs/asciidoc/')
sources {
include '*.adoc'
}
outputDir = file("build/docs/${version}") }

task generateDummyBom {
doLast {
project.buildDir.mkdirs()
new File("$project.buildDir/dummy.pom").write("<project></project>\n")
}
ext.bomFile = file("$project.buildDir/dummy.pom") }

artifacts {
archives(generateDummyBom.bomFile) {
builtBy generateDummyBom
} }

jar.enabled = false

最佳答案

我发现 maven 插件似乎忽略了 packaging 属性。经过一些实验,我发现它将 packaging 属性设置为 Artifact 中文件的扩展名。因此,将 packaging 属性设置为 pom 的方法是使用扩展名为 .pom 的文件创建虚拟 Artifact ,如下所示.

// The real file that we want to publish is the pom generated implicitly by the
// maven publishing plugin.
//
// We need to generate at least one file that we can call an archive so that the
// maven plugin will actually publish anything at all. Luckily, if the file
// that we call an archive is a .pom file, it's magically discarded, and we are
// only left with the implicitly-generated .pom file.
//
// We need the extension of the file to be `.pom` so that the maven plugin will
// set the pom packaging to `pom` (i.e. `<packaging>pom</packaging>`). Otherwise,
// packaging would be set to `xml` if our only file had an `.xml` extension.
task generateDummyBom {
doLast {
// Since we don't depend on anything else, we have to create the build dir
// ourselves.
project.buildDir.mkdirs()
// The file actually has to have xml in it, or Sonatype will reject it
new File("$project.buildDir/${project.artifactId}.pom").write("<project></project>\n")
}
ext.bomFile = file("$project.buildDir/${project.artifactId}.pom")
}

artifacts {
archives(generateDummyBom.bomFile) {
builtBy generateDummyBom
}
}

jar.enabled = false

更新:如果您应用 java 插件,您将需要从您的存档中删除 jar 存档。

// Remove the default jar archive which is added by the 'java' plugin.
configurations.archives.artifacts.with { archives ->
def artifacts = []
archives.each {
if (it.file =~ 'jar') {
// We can't just call `archives.remove(it)` here because it triggers
// a `ConcurrentModificationException`, so we add matching artifacts
// to another list, then remove those elements outside of this iteration.
artifacts.add(it)
}
}
artifacts.each {
archives.remove(it)
}
}

第二次更新:将上面的“dummy.pom”替换为“${project.artifactId}”。

关于maven - 如何在 Gradle 中将包装设置为 pom 而不是默认为 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48742876/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com