groovy script to delete artifacts on nexus 3 (not nexus 2)

无人久伴 提交于 2020-01-05 03:32:15

问题


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.


回答1:


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

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

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



回答2:


I stumbled upon the same problem. I really think that those features should be in nexus out of the box but the tasks for deleting old released artifacts etc. just wait for ages in nexus backlog. In the end I wrote some scripts to show how many artifacts are stored in which repo and how many per month etc. Then I wrote a script to delete old ones... You can probably use or extend this: https://github.com/danischroeter/nexus-repo-scripting




回答3:


Sonatype has a user mailing list where they often direct people to ask for groovy scripting advice. In addition it may prove a better forum for asking for help about the scheduled task than either a StackOverflow answer or in comments.



来源:https://stackoverflow.com/questions/45589937/groovy-script-to-delete-artifacts-on-nexus-3-not-nexus-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!