首页 文章

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

提问于
浏览
3

我有一个生成物料清单(BOM)的项目 . 当我执行gradle构建时,它会生成一个空jar,只包含一个META-INF文件夹 .

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

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

packaging archiveTask.extension这里,uploadTask和archiveTask是指用于上传和生成存档的任务

如何将包装设置为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>

UPDATE:

完整的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

1 回答

  • 0

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

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

    Update :如果您应用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)
      }
    }
    

    Second update :用上面的"$"替换了"dummy.pom" .

相关问题