尝试通过 gradle 将 zip 文件上传到本地 artifactory

Trying to upload a zip file via gradle to local artifactory

我正在尝试结合 gradle 来熟悉 artifactory。我有一个测试项目,它基本上只包含一个 zip 文件(将上传到 docker 中的本地工件 运行ning)和包装器的 gradle 文件 + build.gradle 文件.

我的 build.gradle 文件如下所示:

plugins {
  id "com.jfrog.artifactory" version "4.16.0"
  id 'maven-publish'
  id 'java'
}

repositories {
    maven { url 'http://localhost:8081/artifactory'}
}

configurations {
    generic {
        description = 'generic'
    }
    published
}

artifactory {
    contextUrl = "http://localhost:8081/artifactory"   //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${local_user}"
            password = "${local_pw}"
            maven = true
            
        }
        defaults {
            publishArtifacts = true
            properties = ['qa.level': 'basic', 'dev.team' : 'core']
            publications('MyZip')
        }
    }
    resolve {
        repository {
            repoKey = 'gradle-dev'
            username = "${local_user}"
            password = "${local_pw}"
            maven = true
            
        }
    }
}
publishing {
    publications{
        MyZip(MavenPublication) {
            artifact(file("jetty.zip")){
                extension "zip"
            }
        }
    }
}

artifactoryPublish {
    publications('MyZip')
}

当我执行

./gradlew artifactoryPublish

它说“构建成功”但只将构建信息上传到 artifactory,而不是我希望它上传的 zip 文件。我用谷歌搜索并查看了以下内容:

https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html

roma77 in this post 有完全相同的问题但已修复,但对我不起作用: https://discuss.gradle.org/t/publish-zip-artifact/5792/2

我也试着效仿这个例子https://github.com/jfrog/project-examples/blob/master/gradle-examples/gradle-android-example/build.gradle 但这也没有用。

当我运行

./gradlew artifactoryPublish --info

它说

Task ':artifactoryPublish' is not up-to-date because:
  Task has not declared any outputs despite executing actions.

这就是我问你们的重点,因为我几乎被困住了^^感谢任何帮助。


编辑 1: 所以在@afterburner 提出了一个解决方案后,我尝试了这个 build.gradle:

plugins {
  id "com.jfrog.artifactory" version "4.16.0"
  id 'maven-publish'
  id 'java'
}


repositories {
    maven { url 'http://localhost:8081/artifactory'}
}

configurations {
    generic {
        description = 'generic'
    }
    published
}

artifactory {
    contextUrl = "http://localhost:8081/artifactory"   //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${local_user}"
            password = "${local_pw}"
            maven = true
            
        }
        defaults {
            publishArtifacts = true
            publications('myzip')
        }
    }
    resolve {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${local_user}"
            password = "${local_pw}"
            maven = true
            
        }
    }
}

task makeZip(type: Zip) {
    from fileTree(dir: 'jetty')
    include '**/*'
    archiveName "jetty.zip"
    destinationDir file("$buildDir/libs/")
    description "Assemble archive $archiveName into ${relativePath(destinationDir)}"
}

publishing {
    publications{
        myzip(MavenPublication) {
            artifact makeZip
        }
    }
}

artifactoryPublish {
    publications('myzip')
}

在下面我添加了一个看起来像这样的构建脚本:

buildscript {
    repositories {
        maven {
            url 'http://localhost:8081/artifactory/plugins-release'
            credentials {
                username = "${local_user}"
                password = "${local_pw}"
            }
            name = "local"
        }
    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
    }
}

找到 here。它曾经工作过一次,之后就不再工作了。它还在 gradle/unspecified 下上传了 zip 文件。请注意,我会将我的 Gradle 技能描述为相当有限。我想我可能缺少 gradle 的一些基本配置来说明要做什么。

日志:

> Task :artifactoryDeploys an incubating feature.
Deploying build descriptor to: http://localhost:8081/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://localhost:8081/artifactory/webapp/builds/gradle/1593782379748

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2s
5 actionable tasks: 4 executed, 1 up-to-date

我过去在发布 protobuf 文件时这样做过。

task protoZip(type: Zip) {
    from "src/main/proto"
    include '**/*.proto'
    archiveName "proto.zip"
    destinationDir file("$buildDir/libs/")
    description "Assemble archive $archiveName into ${relativePath(destinationDir)}"
}


publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact protoZip
        }
    }
}

在您发布了新信息之后,让我们逐步分解它。首先,它会在 gradle/unspecified 下发布,因为您尚未设置 groupnameversion。在构建系统中,您缺少 GAV 坐标:组、artefactId/artefactName 和版本。 所以你需要添加如下内容:

group = com.Whosebug.myapp
name = zippy
version = 0.0.1 or something else, depending on how you want to version this

此外,尝试使用 gradle publishToMavenLocal 并检查您的 .m2 本地存储库。

以任何一种方式设置 GAV 坐标后,您可能会取得更大的成功,因为我认为默认情况下 Artifactory 设置为不允许工件覆盖。

我使用以下 build.gradle:

buildscript {
    repositories {
        maven {
            url 'http://localhost:8081/artifactory/gradle-dev'
            credentials {
                username = "${local_user}"
                password = "${local_pw}"
            }
            name = "maven-main-cache"
        }
    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
    }
}

plugins {
  id "com.jfrog.artifactory" version "4.16.0"
  id 'maven-publish'
  id 'java'
}

version = '0.1.0-SNAPSHOT'
group = 'io.klib'

repositories {
    add buildscript.repositories.getByName("maven-main-cache")
}

artifactory {
    contextUrl = "http://localhost:8081/artifactory"   //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${local_user}"
            password = "${local_pw}"
            maven = true
            
        }
        defaults {
            publishArtifacts = true
            publications('myzip')
        }
    }
    resolve {
        repository {
            repoKey = 'gradle-dev-local'
            username = "${local_user}"
            password = "${local_pw}"
            maven = true
            
        }
    }
}

task makeZip(type: Zip) {
    from fileTree(dir: 'jetty')
    include '**/*'
    archiveName "jetty.zip"
    destinationDir file("$buildDir/libs/")
    description "Assemble archive $archiveName into ${relativePath(destinationDir)}"
}

publishing {
    publications{
        myzip(MavenPublication) {
            artifact makeZip
        }
    }
}

artifactoryPublish {
    publications('myzip')
}

makeZip 任务应该不是必需的,您可以直接添加文件。

我在这里犯了两个基本错误。首先,我完全错过了 buildscript 部分,这对于插件知道在哪里推送什么至关重要。其次,我错误地配置了 buildscript 部分。我想推送到 'gradle-dev-local' 仓库,因为 artifactory 只允许你推送到本地仓库。在构建脚本中,我使用了 'gradle-dev' 虚拟存储库,因为这是包含 'gradle-dev-local' 的可访问虚拟存储库。 Virtual repositories include all local, remote and other virtual repositories.

我希望这对某人有所帮助。