我试图将我们的暂存仓库中的所有资产一起推广到已发布的仓库。到目前为止,我已设法创建该组件,以便在尝试重新创建它时检测到它,但它不会出现在搜索或浏览中。我很确定我需要发出一个事件才能显示,但即使在重建索引之后它也不会显示出来,所以我一定也错过了其他东西。
import org.sonatype.nexus.repository.storage.Asset
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.storage.Component
import org.sonatype.nexus.repository.storage.StorageFacet
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def request = new JsonSlurper().parseText(args)
assert request.fromRepoName: 'fromRepo parameter is required'
assert request.toRepoName: 'toRepo parameter is required'
assert request.artifactId: 'artifactId parameter is required'
assert request.groupId: 'groupId parameter is required'
assert request.version: 'version parameter is required'
log.info("Moving ${request.groupId}:${request.artifactId}:${request.version} from ${request.fromRepoName} to ${request.toRepoName}")
def fromRepo = repository.repositoryManager.get(request.fromRepoName)
def toRepo = repository.repositoryManager.get(request.toRepoName)
StorageFacet fromStorageFacet = fromRepo.facet(StorageFacet)
StorageFacet toStorageFacet = toRepo.facet(StorageFacet)
def fromTx = fromStorageFacet.txSupplier().get()
def toTx = toStorageFacet.txSupplier().get()
try {
fromTx.begin()
log.info("Transaction started for repo: ${request.fromRepoName}")
Iterable<Component> components = fromTx.
findComponents((Query.builder().where('name == ').param(request.artifactId).and('group == ').param(request.groupId) & 'version == ').param(request.version).build(), [fromRepo])
log.info("Trying to get component.")
component = components.iterator().next()
try {
toTx.begin()
log.info("Transaction started for repo: ${request.toRepoName}")
if (toTx.componentExists(component.group(), component.name(), component.version(), toRepo)) {
log.info("Component already exists, deleting.")
Iterable<Component> componentsToDelete = toTx.
findComponents((Query.builder().where('name == ').param(request.artifactId).and('group == ').param(request.groupId) & 'version == ').param(request.version).build(), [toRepo])
log.info("Trying to get component.")
componentToDelete = componentsToDelete.iterator().next()
toTx.deleteComponent(componentToDelete)
}
log.info("Creating new component with name: ${component.name()}, group: ${component.group()}, version: ${component.version()} in repo ${request.toRepoName}")
newComponent = toTx.createComponent(toTx.findBucket(toRepo), toRepo.getFormat())
.name(component.name())
.group(component.group())
.version(component.version())
.attributes(component.attributes())
newComponent.setEntityMetadata(component.getEntityMetadata())
log.info("Component metadata: ${component.getEntityMetadata()}")
log.info("New component successfully created.")
fromTx.browseAssets(component).each {Asset asset ->
log.info("Attaching ${asset.name()} to new component.")
log.info("Asset metadata: ${asset}")
newAsset = toTx.createAsset(toTx.findBucket(toRepo), newComponent)
.name(asset.name())
.attributes(asset.attributes())
newAsset.setEntityMetadata(asset.getEntityMetadata())
log.info("Asset entityMetadata: ${asset.getEntityMetadata()}")
log.info("New asset metadata: ${newAsset}")
log.info("Trying to save asset.")
toTx.saveAsset(newAsset)
}
toTx.saveComponent(newComponent)
toTx.commit()
log.info("Transaction commited for repo: ${request.toRepoName}")
} finally {
toTx.close()
}
log.info("Deleting component with name: ${component.name()}, group: ${component.group()}, version: ${component.version()} in repo ${request.fromRepoName}")
fromTx.deleteComponent(component)
fromTx.commit()
log.info("Transaction commited for repo: ${request.fromRepoName}")
}
finally {
fromTx.close()
}
def result = JsonOutput.toJson([
status: "Success"
])
return result
对于添加的上下文,我尝试使用Jenkins Pipelines&amp; amp;设置CI / CD流程。 Maven基于此:https://www.cloudbees.com/blog/new-way-do-continuous-delivery-maven-and-jenkins-pipeline
答案 0 :(得分:0)
不确定你是否已经解决了这个问题,但似乎我正试图找到一个非常相似的解决方案。
我发现如果我使用MavenFacet或RawContentFacet(取决于底层存储库格式)将资产添加到目标存储库中并使用put方法,那么上传的资产会显示在浏览和搜索中。 / p>
这是我提出的代码:
import org.sonatype.nexus.repository.storage.StorageFacet
import org.sonatype.nexus.repository.transaction.TransactionalStoreBlob
import org.sonatype.nexus.repository.view.payloads.BlobPayload
import org.sonatype.nexus.repository.storage.Component
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.maven.MavenFacet
import org.sonatype.nexus.repository.maven.internal.Maven2Format
import org.sonatype.nexus.repository.raw.RawContentFacet
import org.sonatype.nexus.repository.raw.internal.RawFormat
import org.sonatype.nexus.repository.storage.Asset
import org.sonatype.nexus.blobstore.api.BlobStoreManager
import org.sonatype.nexus.repository.manager.RepositoryManager
RepositoryManager rManager = repository.repositoryManager
BlobStoreManager bManager = blobStore.blobStoreManager
def STAGING = 'staging'
def input = new groovy.json.JsonSlurper().parseText(args)
for (item in input.items) {
assert item.to: 'item.to parameter is required'
assert item.group: 'item.group parameter is required'
assert item.artifact: 'item.artifact parameter is required'
assert item.version: 'item.version parameter is required'
assert item.to != STAGING: 'item.to cannot be the staging area'
// Check the repository exists
def toRepo = repository.repositoryManager.get(item.to)
assert toRepo: 'item.to is not a valid repository name'
// Currently only support these payload types
assert [Maven2Format.NAME, RawFormat.NAME].contains(toRepo.getFormat().toString()): 'Unknown Format ' + toRepo.getFormat().toString()
}
def repo = rManager.get(STAGING)
def tx = repo.facet(StorageFacet).txSupplier().get()
def deNest = new ArrayList()
try
{
tx.begin()
for (item in input.items) {
Iterable<Component> components = tx.findComponents(
Query.builder().where('name == ').param(item.artifact).and('group == ').param(item.group)
.and('version == ').param(item.version).build(), [repo])
Component component = components.iterator().next();
tx.browseAssets(component).each { Asset asset ->
def br = asset.requireBlobRef()
deNest.add([
to: item.to,
store: br.getStore(),
blobId: br.getBlobId(),
asset: asset.name(),
contentType: asset.contentType()
])
}
}
tx.commit()
}
finally
{
tx.close()
}
for (dn in deNest) {
def toRepo = rManager.get(dn.to)
def payload = new BlobPayload(bManager.get(dn.store).get(dn.blobId), dn.contentType)
TransactionalStoreBlob.operation.withDb(toRepo.facet(StorageFacet).txSupplier()).call {
switch (toRepo.getFormat().toString()) {
case Maven2Format.NAME:
def toTx = toRepo.facet(MavenFacet)
toTx.put(toTx.mavenPathParser.parsePath(dn.asset), payload)
break
case RawFormat.NAME:
def toTx = toRepo.facet(RawContentFacet)
toTx.put(dn.asset, payload)
break
default:
throw new RuntimeException('Unknown Format ' + toRepo.getFormat().toString())
}
}
}
return groovy.json.JsonOutput.toJson([success: true])