2016-11-22 1248 views
5

我使用Nexus 3,并且想要删除旧版本。在Nexus 2中,有一项计划任务叫做Remove Releases From Repository。 Nexus 3似乎没有这项任务。从Nexus 3清除旧版本

我们如何从Nexus 3中删除旧版本?

感谢

+0

听起来像https://issues.sonatype.org/browse/NEXUS-10821如果你想跟随。 – joedragons

回答

2

我们没有尚未建立了这个计划的任务,在此期间,如果您需要手动删除版本中,您可以使用删除组件功能的UI。

+0

感谢您的回答。有没有办法手动删除发布的组件?我们有很多组件,并且单击每个组件上的删除按钮都很乏味。 – vespasien

+1

尚未通过UI以批量方式处理。然而,编写一个使用脚本API来执行此操作的groovy脚本是有潜力的。 – DarthHater

+0

这里有任何消息吗?你有计划吗? –

3

看到这个剧本为基础,以工作:

通过emexelem

https://gist.github.com/emexelem/bcf6b504d81ea9019ad4ab2369006e66

import org.sonatype.nexus.repository.storage.StorageFacet; 
import org.sonatype.nexus.repository.storage.Query; 
import org.joda.time.DateTime; 
import org.joda.time.format.DateTimeFormat; 

def fmt = DateTimeFormat.forPattern('yyyy-MM-dd HH:mm:ss'); 
// Get a repository 
def repo = repository.repositoryManager.get('nuget-releases'); 
// Get a database transaction 
def tx = repo.facet(StorageFacet).txSupplier().get(); 
// Begin the transaction 
tx.begin(); 
// Search assets that havn't been downloaded for more than three months 
tx.findAssets(Query.builder().where('last_downloaded <').param(DateTime.now().minusMonths(3).toString(fmt)).build(), [repo]).each { asset -> 
    def component = tx.findComponent(asset.componentId()); 
    // Check if there is newer components of the same name 
    def count = tx.countComponents(Query.builder().where('name').eq(component.name()).and('version >').param(component.version()).build(), [repo]); 
    if (count > 0) { 
     log.info("Delete asset ${asset.name()} as it has not been downloaded since 3 months and has a newer version") 
     tx.deleteAsset(asset); 
     tx.deleteComponent(component); 
    } 
} 
// End the transaction 
tx.commit(); 
0

所以,我打了一个问题,跑出来的磁盘空间作为我们发布/构建了叠加起来,所以我有一点调侃的在创建一个脚本来删除旧围绕构建,并得到了这一点:

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

def repositoryName = 'Integration'; 
def maxArtifactCount = 20; 

// Get a repository 
def repo = repository.repositoryManager.get(repositoryName); 
// Get a database transaction 
def tx = repo.facet(StorageFacet).txSupplier().get(); 
try { 
    // Begin the transaction 
    tx.begin(); 

    def previousComponent = null; 
    def uniqueComponents = []; 
    tx.findComponents(Query.builder().suffix(' ORDER BY group, name').build(), [repo]).each{component -> 
     if (previousComponent == null || (!component.group().equals(previousComponent.group()) || !component.name().equals(previousComponent.name()))) { 
      uniqueComponents.add(component); 
     } 
     previousComponent = component; 
    } 

    uniqueComponents.each {uniqueComponent -> 
     def componentVersions = tx.findComponents(Query.builder().where('group = ').param(uniqueComponent.group()).and('name = ').param(uniqueComponent.name()).suffix(' ORDER BY last_updated DESC').build(), [repo]); 
     log.info(uniqueComponent.group() + ", " + uniqueComponent.name() + " size " + componentVersions.size()); 
     if (componentVersions.size() > maxArtifactCount) { 
      componentVersions.eachWithIndex { component, index -> 
       if (index > maxArtifactCount) { 
        log.info("Deleting Component ${component.group()} ${component.name()} ${component.version()}") 
        tx.deleteComponent(component); 
       } 
      } 
     } 
    } 
} finally { 
    // End the transaction 
    tx.commit(); 
} 

这是通过一个存储库,寻找所有的日e组件。然后它通过所有版本(按上次更新排序 - 我无法确定按版本号排序,但我认为这应该没问题),然后删除超过maxArtifactCount编号的任何版本。

希望这可能是有用的 - 如果你看到任何问题让我知道。

1

自从nexus 3发布以来,您可以将groovy脚本部署到nexus管理器。只需创建一个新的执行脚本任务,使用下面的脚本:

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}"); 
} 

https://github.com/xninjaxelitex/nexus3-cleanup-release-artifact

这个脚本会在顶部指定参数清洁您的Nexus仓库。