清理nexus3日志,从Nexus 3清除旧版本

I use Nexus 3 and I want to delete my old releases. In Nexus 2 there is a scheduled task called Remove Releases From Repository. It seems that this tasks is missing in Nexus 3.

How can we delete old release from Nexus 3 ?

Thanks

解决方案

Since the release of nexus 3 you can deploy groovy scripts to the nexus manager. Simply create a new execute script task and use the following script:

import org.sonatype.nexus.repository.storage.StorageFacet;

import org.sonatype.nexus.common.app.GlobalComponentLookupHelper

import org.sonatype.nexus.repository.maintenance.MaintenanceService

import org.sonatype.nexus.repository.storage.ComponentMaintenance

import org.sonatype.nexus.repository.storage.Query;

import org.sonatype.nexus.script.plugin.RepositoryApi

import org.sonatype.nexus.script.plugin.internal.provisioning.RepositoryApiImpl

import com.google.common.collect.ImmutableList

import org.joda.time.DateTime;

import org.slf4j.Logger

// ----------------------------------------------------

// delete these rows when this script is added to nexus

RepositoryApiImpl repository = null;

Logger log = null;

GlobalComponentLookupHelper container = null;

// ----------------------------------------------------

def retentionDays = 30;

def retentionCount = 10;

def repositoryName = 'maven-releases';

def whitelist = ["org.javaee7.sample/javaee7-simple-sample", "org.javaee7.next/javaee7-another-sample"].toArray();

log.info(":::Cleanup script started!");

MaintenanceService service = container.lookup("org.sonatype.nexus.repository.maintenance.MaintenanceService");

def repo = repository.repositoryManager.get(repositoryName);

def tx = repo.facet(StorageFacet.class).txSupplier().get();

def components = null;

try {

tx.begin();

components = tx.browseComponents(tx.findBucket(repo));

}catch(Exception e){

log.info("Error: "+e);

}finally{

if(tx!=null)

tx.close();

}

if(components != null) {

def retentionDate = DateTime.now().minusDays(retentionDays).dayOfMonth().roundFloorCopy();

int deletedComponentCount = 0;

int compCount = 0;

def listOfComponents = ImmutableList.copyOf(components);

def previousComp = listOfComponents.head().group() + listOfComponents.head().name();

listOfComponents.reverseEach{comp ->

log.info("Processing Component - group: ${comp.group()}, ${comp.name()}, version: ${comp.version()}");

if(!whitelist.contains(comp.group()+"/"+comp.name())){

log.info("previous: ${previousComp}");

if(previousComp.equals(comp.group() + comp.name())) {

compCount++;

log.info("ComCount: ${compCount}, ReteCount: ${retentionCount}");

if (compCount > retentionCount) {

log.info("CompDate: ${comp.lastUpdated()} RetDate: ${retentionDate}");

if(comp.lastUpdated().isBefore(retentionDate)) {

log.info("compDate after retentionDate: ${comp.lastUpdated()} isAfter ${retentionDate}");

log.info("deleting ${comp.group()}, ${comp.name()}, version: ${comp.version()}");

// ------------------------------------------------

// uncomment to delete components and their assets

// service.deleteComponent(repo, comp);

// ------------------------------------------------

log.info("component deleted");

deletedComponentCount++;

}

}

} else {

compCount = 1;

previousComp = comp.group() + comp.name();

}

}else{

log.info("Component skipped: ${comp.group()} ${comp.name()}");

}

}

log.info("Deleted Component count: ${deletedComponentCount}");

}

This script will clean your nexus repository by the specified parameters at the top.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值