groovy脚本以删除nexus 3(不是nexus 2)上的工件 [英] groovy script to delete artifacts on nexus 3 (not nexus 2)

查看:415
本文介绍了groovy脚本以删除nexus 3(不是nexus 2)上的工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有nexus 3服务器,可以在其上保存工件,并且已满. 我希望创建一个每天删除旧工件的任务,但始终保留至少50个工件.问题是应该执行的默认任务不起作用.

i have nexus 3 server that i save artifacts on it, and it has been filled to max. i wish to create a task to delete the old artifacts every day but always remain with at least 50 artifacts. the problem is that the default task that should do it, does't work.

所以我读到它可以通过安排在任务中运行的常规脚本来完成.

so i read that it can be done with a groovy script that i schedule to run inside tasks.

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

can anyone help me with it? i can't find anything useful on the internet.

推荐答案

基于@daniel-schröter的答案,您可以在此示例后添加Scheduled Task:

based on @daniel-schröter answer you could add a Scheduled Task following this example:

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

Go to System -> Tasks and click Create Task. Create a script task:

将语言设置为groovy并复制修改后的脚本以适合预定任务(您应该对其进行修改,这只是一个示例):

Set the language to groovy and copy this script modified to fit to scheduled task (you should provide your own modifications to it, it's just an example):

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

这篇关于groovy脚本以删除nexus 3(不是nexus 2)上的工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆