How to browse/cleanup components/assets in groovy script with H2 instead of OrientDB (e.g. without StorageFacet)

Hi,

There are many examples how to browse/cleanup components/assets via groovy scripts like so - GitHub - PhilSwiss/nexus-cleanup: Remove old releases from a Maven2-repository on Nexus3 - but after switching to H2 instead of OrientDB with nexus.datastore.enabled=true I receiving error unable to resolve class org.sonatype.nexus.repository.storage.StorageFacet

Please explain me why I can’t use StorageFacet with H2 and how to browse components/assets in this case?

The internals for SQL were largely re-written.

Each repository will have a ContentFacet attached which can provide access to components and assets. Note that interacting with the internals of Nexus directly may not result in appropriate book keeping occurring.

Thank you!

I’m trying to use StorageFacet in similar way:

import org.sonatype.nexus.repository.storage.ContentFacet

def response = []

def repo = repository.repositoryManager.get(args)
def tx = repo.facet(ContentFacet).txSupplier().get()

try {
    tx.begin()
    tx.browseAssets(tx.findBucket(repo)).each { asset -> response << "asset:${asset.name()}" }
    tx.browseComponents(tx.findBucket(repo)).each { component -> response << "component:${component.name()}" }
}
finally {
    tx.close()
}

return response.join('\n')

But I’m receiving error even on import:

$ ./run.py browse-content.groovy 
UPDATE: <Response [204]> 
RUN: <Response [400]> {
  "name" : "browse-content",
  "result" : "org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:\nScript6.groovy: 1: unable to resolve class org.sonatype.nexus.repository.storage.ContentFacet\n @ line 1, column 1.\n   import org.sonatype.nexus.repository.storage.ContentFacet\n   ^\n\n1 error\n"
}

run.py is simple python script to update and run any groovy script via REST API

So, why ContentFacet can’t be imported and how to use it correctly?

I will suggest something like this below

import org.sonatype.nexus.repository.content.facet.ContentFacet

chunkSize = 100

repo = repository.getRepositoryManager().get(args)
content = repo.facet(ContentFacet.class)
response = []

for (page = content.assets().browse(chunkSize, null);
     !page.isEmpty();
     page = content.assets().browse(chunkSize, page.nextContinuationToken())) {
  page.each(asset -> response << "asset:${asset.path()}" } 
}

return response.join('\n')

Something to be careful with - if your page is ever empty the continuation token will be null and you may infinitely loop.

This class contains some helpers: nexus-public/components/nexus-common/src/main/java/org/sonatype/nexus/common/entity/Continuations.java at main · sonatype/nexus-public · GitHub

1 Like

Thanks! All above works good enough

Hi @eugene.prokopiev @wiget.
If you know the way to correctly remove docker components with H2 groovy API, could you please share it.