简体   繁体   English

如何防止覆盖 hudson 上的 Maven 存储库中已发布的工件(非快照版本)

[英]How to prevent overwriting of released artifacts (non snapshot versions) in maven repository on hudson

Problem Description问题描述

Consider the case maven is being used on hudson.考虑在 hudson 上使用 maven 的情况。

Now someone took checkout of a project,modified some files but accidently used same artifact id and version number(non snapshot).现在有人签出一个项目,修改了一些文件,但不小心使用了相同的工件 ID 和版本号(非快照)。

He/She then build this project on hudson and did maven install.The modified artifact is now in hudson .m2 .然后他/她在 hudson 上构建这个项目并安装了 maven。修改后的工件现在在 hudson .m2 中。 Any other project which depent on it will be build with modified artifact.任何其他依赖于它的项目都将使用修改后的工件构建。 No one finds this out if compilation doesn't fail.如果编译没有失败,没有人会发现这一点。 Even though correct artifact resides in central repository it is never used because modified one is picked up from .m2 when hudson starts building.即使正确的工件驻留在中央存储库中,它也从未使用过,因为在 hudson 开始构建时从 .m2 中提取了修改过的工件。

So i am looking for a way to prevent this accidental human error.所以我正在寻找一种方法来防止这种意外的人为错误。

  1. Anyway to revoke permissions of maven install on non snapshot versions (released artifacts) on hudson ?无论如何要撤销 hudson 上非快照版本(已发布的工件)上的 maven 安装权限?
  2. Any way to compare checksums of .m2 in hudson and on in remote central repository so that checksum failures can generate warnings or fail build ?有什么方法可以比较 hudson 和远程中央存储库中 .m2 的校验和,以便校验和失败可以生成警告或构建失败?

I have already checked that there is no way to force update non-snapshots versions from central repository as they are meant to be immutable.我已经检查过没有办法从中央存储库强制更新非快照版本,因为它们是不可变的。

Purging central repository or using separate repository for each job on hudson will result in increased build times & disk space usage respectively.清除中央存储库或为 hudson 上的每个作业使用单独的存储库将分别导致构建时间和磁盘空间使用量增加。

Any help would be appreciated.任何帮助,将不胜感激。

There was no direct way to solve this but we solved this inderctly by writing a cron-job which runs every five minutes and marks all the jars which are NON-SNAPSHOT as read only in the local repository of Hundson .没有直接的方法可以解决这个问题,但我们通过编写一个每五分钟运行一次的 cron-job 并在 Hundson 的本地存储库中将所有非快照的 jar 标记为只读来解决这个问题。 In this way when some project in Hudson tries to overwrite it my mvn install or mvn deploy it fails in overwiriting the artifacts as they are readonly.这样,当 Hudson 中的某个项目试图覆盖它时,我的 mvn install 或 mvn deploy 它无法覆盖工件,因为它们是只读的。

Any new artifacts to be realeased can easily be written.可以轻松编写要释放的任何新工件。 Once written within next five minutes script marks them as read only.一旦在接下来的五分钟内写入,脚本会将它们标记为只读。

Here is code for unix script permission-handler.sh这是unix脚本permission-handler.sh的代码

#!/bin/bash
cd ~/.m2
date 2>&1>> permission-handler.out
find . -name '*jar' -type f | grep -v 'SNAPSHOT' | xargs chmod -vc 444 2>&1>> permission-handler.out
chmod 777 permission-handler.out

Logging is also handled to see which all artifacts have been marked as released only.还处理日志以查看哪些所有工件已标记为仅发布。

I don't think you're going to find a way to stop an install from overwriting an artifact.我认为您不会找到一种方法来阻止安装覆盖工件。 A repository server should have a setting to prevent deploying an updated release artifact though.存储库服务器应该有一个设置来防止部署更新的发布工件。 See, for example, "How do I disable artifact redeployment" for Nexus.例如,请参阅 Nexus 的“如何禁用工件重新部署”

Here is how we manage versions in our project:以下是我们在项目中管理版本的方式:

We work on a SNAPSHOT version.我们正在开发SNAPSHOT版本。 On Jenkins, we have a Fast Build job that builds and tests this application, but fails if the version is not a SNAPSHOT .在 Jenkins 上,我们有一个快速构建作业来构建和测试此应用程序,但如果版本不是SNAPSHOT则失败。 This is done by a custom enforcer (this is the opposite of the require release version enforcer ).这是由自定义执行器完成的(这与require 发布版本执行器相反)。

When we want to make a release, we use a Jenkins job for that.当我们想要发布时,我们会使用 Jenkins 作业。 Using theparameterized build , and Maven release plugin , the person who is in charge of doing the release will just indicate the version of the release (the stable version), the next SNAPSHOT version, as well as the name of the SCM tag.使用参数化的 buildMaven release plugin ,负责做发布的人只会指出发布的版本(稳定版本),下一个SNAPSHOT版本,以及 SCM 标签的名称。 Thus, only Jenkins will define a stable version and the developers will always work on a SNAPSHOT code.因此,只有 Jenkins 会定义一个稳定版本,开发人员将始终使用SNAPSHOT代码。

But of course, this does not prevent the developers to make what he wants on his local machine.但是当然,这并不妨碍开发人员在他的本地机器上制作他想要的东西。 But we always consider one trusted place: the Jenkins server.但我们总是考虑一个值得信赖的地方:Jenkins 服务器。 It works on my machine is never a good answer to a problem ;o)它在我的机器上工作从来都不是问题的好答案;o)

This is solved by configuring your Maven repository (eg Nexus, Artifactory) from not allowing overwrite of the release repos.这是通过配置您的 Maven 存储库(例如 Nexus、Artifactory)不允许覆盖发布存储库来解决的。 In Nexus we have a repo for SNAPSHOT and one for releases.在 Nexus 中,我们有一个用于 SNAPSHOT 的仓库和一个用于发布的仓库。 The SNAPSHOT repo allows overwrite. SNAPSHOT 存储库允许覆盖。 But the release repo does not allow overwrite.但是发布回购不允许覆盖。 This is just a simple checkbox feature in Nexus for that repo.这只是 Nexus 中该存储库的一个简单复选框功能。 Once a release version is put in the repo, it cannot be overwritten.一旦将发布版本放入存储库,就无法覆盖它。 Works out very well.效果很好。

I had the same requirement.我有同样的要求。 Checking for the artifact can be achieved with REST request from a gradle task.可以使用来自 gradle 任务的 REST 请求来检查工件。

publish.dependsOn lookForArtifacts

task lookForArtifacts {
    group "upload"
    doLast {

        def pomFileName = "${ARTIFACT_ID}-${ARTIFACT_VERSION}.pom"
        def artifactPath = "${ARTIFACT_GROUP.replace(".", "/")}/${ARTIFACT_ID}/${ARTIFACT_VERSION}/${pomFileName}"
        def repositoryUrl = "$MAVEN_SERVER/${artifactPath}"

        println("# searching for existing artifact wit id ${ARTIFACT_VERSION}")
        println("")

        if (urlExists(repositoryUrl)) {
            println("# Existing artifact found")
            println("")
            throw new RuntimeException("Artifact with version $ARTIFACT_VERSION already exist - increase the verion to publish")
        } else {
            println("# No existing artifact found. Preceding to publish")
            println("")
        }
    }
}

def urlExists(String repositoryUrl) {

    try {
        def connection = (HttpURLConnection) new URL(repositoryUrl).openConnection()

        connection.setRequestProperty("Authorization", "Basic " + getBase64EncodedCredentials())
        connection.setConnectTimeout(10000)
        connection.setReadTimeout(10000)
        connection.setRequestMethod("HEAD")

        def responseCode = connection.getResponseCode()

        if (responseCode == 401) {
            throw new RuntimeException("Unauthorized MavenUser user. Please provide valid username and password.")
        }

        return (200 == responseCode)

    } catch (IOException ignored) {
        println(ignored)
        return false
    }
}

def getBase64EncodedCredentials() {
    def s = "$MAVEN_USERNAME" + ":" + "$MAVEN_PASSWORD"
    return s.bytes.encodeBase64().toString()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven存储库中SNAPSHOT版本的规则 - Rules for SNAPSHOT versions in Maven repository 如何在Nexus存储库管理器中绑定Maven工件的版本? - How do you peg versions of Maven artifacts in the Nexus repository manager? 使用Maven时如何自动将SNAPSHOT依赖项更新为最新发布的版本 - How do I automate the update of SNAPSHOT dependencies to the latest released versions when using maven 为什么 Maven 无法从 Azure 工件存储库下载“SNAPSHOT”工件? (快照到时间戳的转换出现故障) - Why maven is not able to download "SNAPSHOT" artifacts from azure artifacts repository? (Snapshot translation to timestamp is malfunctioning) 无法将Maven工件部署到快照/私有Cloudbees存储库 - Unable to deploy Maven artifacts to snapshot/private Cloudbees repository 如何自动从OSS 3.0.0上托管的Maven存储库中删除工件的旧版本? - How to automatically delete old versions of artifacts from hosted maven repository on OSS 3.0.0? SNAPSHOT和RELEASE版本在Maven本地存储库中未获得更新 - SNAPSHOT and RELEASE versions are not getting update at Maven Local Repository 使用已发布的工件时,Maven 依赖关系解析如何工作? - How does Maven dependency resolution work when using released artifacts? 如何防止Maven检查远程存储库中的某些工件? - How do I prevent maven from checking a remote repository for certain artifacts? 当我刚刚更新项目的 pom 版本时,如何防止 Maven 从存储库下载工件 - How to prevent Maven from downloading artifacts from repository when I just update the pom version of my project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM