【问题标题】:groovy script to delete artifacts on nexus 3 (not nexus 2)删除nexus 3(不是nexus 2)上的工件的groovy脚本
【发布时间】:2018-01-17 07:11:27
【问题描述】:

我有我保存工件的 nexus 3 服务器,它已被填充到最大。 我希望每天创建一个删除旧工件的任务,但始终保留至少 50 个工件。问题是应该执行的默认任务不起作用。

所以我读到它可以使用我安排在任务内部运行的 groovy 脚本来完成。

有人可以帮我吗?我在互联网上找不到任何有用的东西。

【问题讨论】:

  • 您总共有 50 个 maven 工件还是同一工件的 50 个不同版本?我不清楚您要做什么,也不清楚您显示的任务为何不起作用。
  • 我有存储库名称生产,我想只保存最后 50 个工件版本,所以每天它都会删除最旧的工件版本,直到只剩下 50 个。这是同一个工件,不同的构建器
  • 一天输出多少张快照?超过 50 个?
  • 太多了.. 但没有 50 多... 但是我们的开发人员有他们的日子。测试库的上传速度很疯狂
  • 好吧。你能描述一下为什么你上面列出的任务不起作用吗?或者你觉得这超出了范围?我认为它应该根据您对场景的描述起作用。

标签: groovy scripting nexus nexus3


【解决方案1】:

我偶然发现了同样的问题。我真的认为这些功能应该是开箱即用的,但是删除旧发布的工件等的任务只是等待连接积压的时间。最后,我编写了一些脚本来显示在哪个 repo 中存储了多少工件以及每月有多少等。 然后我写了一个脚本来删除旧的...... 您可能可以使用或扩展它: https://github.com/danischroeter/nexus-repo-scripting

【讨论】:

    【解决方案2】:

    根据@daniel-schröter 的回答,您可以在此示例之后添加Scheduled Task

    转到System -> Tasks 并单击Create Task。创建脚本任务:

    将语言设置为groovy 并复制修改后的脚本以适应计划任务(您应该提供自己的修改,这只是一个示例):

    import org.sonatype.nexus.repository.storage.Component
    import org.sonatype.nexus.repository.storage.Query
    import org.sonatype.nexus.repository.storage.StorageFacet
    
    log.info("delete components for repository: my-repo")
    
    def compInfo = { Component c -> "${c.group()}:${c.name()}:${c.version()}[${c.lastUpdated()}]}" }
    
    def repo = repository.repositoryManager.get("my-repo")
    StorageFacet storageFacet = repo.facet(StorageFacet)
    
    def tx = storageFacet.txSupplier().get()
    tx.begin()
    Iterable<Component> components = tx.findComponents(Query.builder().where('last_updated < ').param('2190-01-01').build(), [repo])
    tx.commit()
    tx.close()
    
    log.info("about to delete " + components.flatten(compInfo))
    for(Component c : components) {
        log.info("deleting " + compInfo(c))
        tx2 = storageFacet.txSupplier().get()
        tx2.begin()
        tx2.deleteComponent(c)
        tx2.commit()
        tx2.close()
    }
    
    log.info("finished deleting " + components.flatten(compInfo))
    

    【讨论】:

    • 我在使用log.delete 执行脚本时遇到问题。我更改为log.info,它可以工作。我使用 Nexus 3.12.1
    • 是的,我忘了说,我已经更新了答案
    • 我使用脚本删除了一个组。然后运行任务压缩 blob 存储,然后重建元数据。现在我没有工件,但有许多 Maven 元数据文件。我如何也可以删除这些?
    猜你喜欢
    • 2017-02-24
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多