【问题标题】:Upload only war/jar files in gradle(restrict zip/tar generation and upload)仅上传 gradle 中的 war/jar 文件(限制 zip/tar 生成和上传)
【发布时间】:2015-11-18 08:14:48
【问题描述】:

我的构建脚本如下。我使用gradle build 命令构建和gradle upload 命令上传工件。我的问题是使用此命令生成的 tar、zip 文件并上传。我不想要它。我想上传的只有“jar”和“war”文件。 我昨天还发布了一个相关问题here

更多细节(我已经排除了一些不需要的代码)

在根目录下构建文件

allprojects  {
  apply plugin: 'maven'
 group = 'groupid'
version = '1.0-SNAPSHOT'
}

subprojects {
  apply plugin: 'java'
  sourceCompatibility = 1.7
  targetCompatibility = 1.7


    repositories {
        maven {
            credentials {
                username "$nexusUser"
                password "$nexusPass"
            }
            url "$nexusUrl"
        }
    }

    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "$nexusSnapshotUrl") {
                    authentication(userName: "$nexusUser", password: "$nexusPass")
                }
            }
        }
    }
}

ext.comlib = [ // Groovy map literal
                  junit3: "junit:junit:3.8",
                  junit4: "junit:junit:4.9",
                  spring_core: "org.springframework:spring-core:3.1",
                  hibernate_validator : "org.hibernate:hibernate-validator:5.1.3.Final",
                  spring_core : "org.springframework.security:spring-security-core:4.0.2.RELEASE",
                  spring_security_web: "org.springframework.security:spring-security-web:4.0.2.RELEASE",
                  spring_security_config: "org.springframework.security:spring-security-config:4.0.2.RELEASE",
                  spring_boot_starter_test: "org.springframework.boot:spring-boot-starter-test:1.2.5.RELEASE",
                  spring_boot_starter_actuator: "org.springframework.boot:spring-boot-starter-actuator:1.2.5.RELEASE",
                  spring_boot_plugin_gradle: "org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE",
                  asciidoctor_gradle_plugin: "org.asciidoctor:asciidoctor-gradle-plugin:1.5.1",
                  asciidoctor_pdf_plugin: "org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.9"/*,
                  sl4j_api: "org.slf4j:slf4j-api:1.7.12",
                  sl4j_log4j: "org.slf4j:slf4j-log4j12:1.7.12",
                  logback_classic: "ch.qos.logback:logback-classic:1.1.3",
                  logback_core: "ch.qos.logback:logback-core:1.1.3"*/
]

在子模块中构建文件

apply plugin: 'spring-boot'
group = 'com.group.id'
apply from: "../build.gradle"
apply plugin: 'org.asciidoctor.gradle.asciidoctor'

apply plugin: 'war'

description = 'module name'
dependencies {
    compile "someothermodule:commonapi:1.0.0-SNAPSHOT"
    compile "io.springfox:springfox-swagger2:2.0.1"
    compile project(':dependingproject1:dependingproject2')
    compile comlib.spring_boot_starter_actuator
    compile comlib.spring_core
    compile comlib.spring_security_web
    compile comlib.spring_security_config
    testCompile(comlib.spring_boot_starter_test) {
exclude(module: 'commons-logging')
    }
    testCompile comlib.junit4
    providedCompile comlib_app.spring_boot_plugin_tomcat
    testCompile "io.springfox:springfox-staticdocs:2.0.3"
    testCompile "org.springframework:spring-test:4.1.7.RELEASE"
}

ext {
    swaggerOutputDir = file("src/docs/asciidoc/generated")
    asciiDocOutputDir = file("${buildDir}/asciidoc")
}

test {
    systemProperty 'org.springframework.restdocs.outputDir', asciiDocOutputDir
    systemProperty 'io.springfox.staticdocs.outputDir', swaggerOutputDir

}

//spring boot plugin
buildscript {
    repositories {
        maven {
            credentials {
                username "$nexusUser"
                password "$nexusPass"
            }
            url "$nexusCentral"
        }
        maven {
            credentials {
                username "$nexusUser"
                password "$nexusPass"
            }
            url "$nexusThirdParty"

        }
    }
    dependencies {
        classpath(comlib.spring_boot_plugin_gradle)
    }
}

【问题讨论】:

    标签: gradle upload


    【解决方案1】:

    在我的 gradle 文件中包含以下代码 sn-p

    [distZip, distTar].each { task -> configurations.archives.artifacts.removeAll
    { it.class.simpleName == "ArchivePublishArtifact" && it.archiveTask == task }
    
    task.enabled = false
    }
    

    更多详情请参考this link。它与 spring boot 插件有关。

    【讨论】:

    • 1.5 年后,即使任务被禁用,distTar 仍会添加到 uploadArchives。
    【解决方案2】:

    arjuncc 的解决方案似乎不适用于 Gradle 4.10.2,所以这里有一个可以使用并使用公共 API 的解决方案,希望它能继续工作。

    configurations.archives.artifacts.removeAll {
        // exclude from the archives configuration all artifacts that were generated by distZip & distTar
        def depTasks = it.getBuildDependencies().getDependencies()
        depTasks.contains(distZip) || depTasks.contains(distTar)  
    }
    

    【讨论】:

      【解决方案3】:

      与 ajuncc 的解决方案大致相同,但可能更简单一些。从档案配置中删除所有 .tar 工件:

      configurations.archives.artifacts.removeAll {PublishArtifact publishArtifact -> publishArtifact.type == 'tar'} 
      

      【讨论】:

        猜你喜欢
        • 2018-08-09
        • 2018-08-05
        • 2011-01-07
        • 1970-01-01
        • 2020-05-23
        • 2019-01-26
        • 1970-01-01
        • 2013-12-19
        • 1970-01-01
        相关资源
        最近更新 更多