Switching to new repository layout

This commit is contained in:
Martin Stockhammer 2020-06-02 22:53:18 +02:00
parent 22a8cbfd22
commit 873753536b
8 changed files with 95 additions and 28 deletions

View File

@ -69,6 +69,19 @@ public class ArtifactReference
private String type;
public String getProjectVersion( )
{
return projectVersion;
}
public void setProjectVersion( String projectVersion )
{
this.projectVersion = projectVersion;
}
private String projectVersion;
//-----------/
//- Methods -/
//-----------/

View File

@ -49,6 +49,9 @@ import org.apache.archiva.repository.ManagedRepository;
import org.apache.archiva.repository.RemoteRepository;
import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.metadata.base.MetadataTools;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.storage.fs.FilesystemStorage;
@ -418,7 +421,15 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
private StorageAsset toLocalFile(ManagedRepository repository, ArtifactReference artifact ) throws LayoutException
{
return repository.getContent().getLayout( BaseRepositoryContentLayout.class ).toFile( artifact );
ItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( artifact.getGroupId( ) )
.withProjectId( artifact.getArtifactId( ) )
.withArtifactId( artifact.getArtifactId( ) )
.withArtifactVersion( artifact.getVersion() )
.withVersion( artifact.getProjectVersion( ) )
.withType( artifact.getType( ) ).build();
Artifact repoArtifact = repository.getContent( ).getLayout( BaseRepositoryContentLayout.class ).getArtifact( selector );
return repoArtifact.getAsset( );
}
/**

View File

@ -34,8 +34,7 @@ import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* SnapshotTransferTest
@ -314,6 +313,7 @@ public class SnapshotTransferTest
StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
assertNotNull( downloadedFile );
assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
assertNoTempFiles( expectedFile );
}
@ -333,6 +333,7 @@ public class SnapshotTransferTest
BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
artifact.setProjectVersion( "1.0-SNAPSHOT" );
assertTrue( Files.exists(expectedFile) );
@ -344,6 +345,7 @@ public class SnapshotTransferTest
StorageAsset downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository.getRepository(), artifact );
Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
assertNotNull( downloadedFile );
assertFileEquals( expectedFile, downloadedFile.getFilePath(), proxiedFile );
assertNoTempFiles( expectedFile );
}

View File

@ -36,6 +36,7 @@ import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.Namespace;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.ArchivaArtifact;
import org.apache.archiva.repository.content.base.ArchivaContentItem;
import org.apache.archiva.repository.content.base.ArchivaDataItem;
import org.apache.archiva.repository.content.base.ArchivaNamespace;
@ -173,7 +174,15 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout
@Override
public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
{
return null;
StringBuilder path = new StringBuilder(selector.getNamespace( ).replace( ".", "/" ));
path.append( "/" ).append( selector.getProjectId( ) ).append( "/" ).append( selector.getVersion( ) );
path.append( "/" ).append( selector.getArtifactId( ) ).append( "-" ).append( selector.getArtifactVersion( ) ).append( "." ).append( selector.getType( ) );
StorageAsset asset = fsStorage.getAsset( path.toString( ) );
ArchivaNamespace ns = ArchivaNamespace.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ).getParent( ).getParent( ) ).withNamespace( selector.getNamespace( ) ).build( );
ArchivaProject project = ArchivaProject.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ).getParent( ) ).withNamespace( ns ).withId( selector.getProjectId( ) ).build( );
ArchivaVersion version = ArchivaVersion.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ) ).withProject( project ).withVersion( selector.getVersion( ) ).build( );
ArchivaArtifact artifact = ArchivaArtifact.withAsset( asset ).withVersion( version ).withId( selector.getArtifactId( ) ).withArtifactVersion( selector.getArtifactVersion( ) ).withType( selector.getType( ) ).build( );
return artifact;
}
@Override
@ -424,6 +433,7 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout
artifact.setGroupId( metadata.getNamespace() );
artifact.setArtifactId( metadata.getProject() );
artifact.setVersion( metadata.getVersion() );
artifact.setProjectVersion( metadata.getProjectVersion( ) );
MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
if ( facet != null )
{

View File

@ -79,6 +79,7 @@ public class DefaultPathParser
artifact.setGroupId( metadata.getNamespace() );
artifact.setArtifactId( metadata.getProject() );
artifact.setVersion( metadata.getVersion() );
artifact.setProjectVersion( metadata.getProjectVersion( ) );
MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
if ( facet != null )
{

View File

@ -39,7 +39,10 @@ import org.apache.archiva.proxy.model.NetworkProxy;
import org.apache.archiva.proxy.model.ProxyConnector;
import org.apache.archiva.proxy.model.RepositoryProxyHandler;
import org.apache.archiva.repository.*;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.PathParser;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.maven.MavenSystemManager;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.storage.StorageAsset;
@ -572,28 +575,39 @@ public class Maven2RepositoryStorage
pomReference.setGroupId(artifact.getGroupId());
pomReference.setArtifactId(artifact.getArtifactId());
pomReference.setVersion(artifact.getVersion());
pomReference.setProjectVersion( artifact.getProjectVersion() );
pomReference.setType("pom");
BaseRepositoryContentLayout layout;
try
{
layout = managedRepository.getContent( ).getLayout( BaseRepositoryContentLayout.class );
}
catch ( LayoutException e )
{
throw new ProxyDownloadException( "Could not set layout " + e.getMessage( ), new HashMap<>( ) );
}
RepositoryType repositoryType = managedRepository.getType();
if (!proxyRegistry.hasHandler(repositoryType)) {
throw new ProxyDownloadException("No proxy handler found for repository type " + repositoryType, new HashMap<>());
}
ItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( artifact.getGroupId( ) )
.withProjectId( artifact.getArtifactId( ) )
.withArtifactId( artifact.getArtifactId( ) )
.withVersion( artifact.getVersion( ) )
.withArtifactVersion( artifact.getVersion( ) )
.withType( "pom" ).build( );
Artifact pom = layout.getArtifact( selector );
RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(repositoryType).get(0);
// Get the artifact POM from proxied repositories if needed
proxyHandler.fetchFromProxies(managedRepository, pomReference);
// Open and read the POM from the managed repo
StorageAsset pom = null;
try
{
pom = managedRepository.getContent().getLayout( BaseRepositoryContentLayout.class ).toFile(pomReference);
}
catch ( LayoutException e )
{
throw new ProxyDownloadException( "Cannot convert layout ", new HashMap<>( ) );
}
if (!pom.exists()) {
return;
@ -603,7 +617,7 @@ public class Maven2RepositoryStorage
// MavenXpp3Reader leaves the file open, so we need to close it ourselves.
Model model;
try (Reader reader = Channels.newReader(pom.getReadChannel(), Charset.defaultCharset().name())) {
try (Reader reader = Channels.newReader(pom.getAsset().getReadChannel(), Charset.defaultCharset().name())) {
model = MAVEN_XPP_3_READER.read(reader);
}

View File

@ -25,8 +25,11 @@ import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.repository.storage.util.StorageUtil;
import org.apache.commons.lang3.StringUtils;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
@ -65,11 +68,6 @@ public class ArtifactBuilder
public Artifact build()
{
ArtifactReference ref = new ArtifactReference();
ref.setArtifactId( artifactMetadata.getProject() );
ref.setGroupId( artifactMetadata.getNamespace() );
ref.setVersion( artifactMetadata.getVersion() );
String type = null, classifier = null;
MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
@ -79,8 +77,20 @@ public class ArtifactBuilder
classifier = facet.getClassifier();
}
ref.setClassifier( classifier );
ref.setType( type );
ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( )
.withNamespace( artifactMetadata.getNamespace( ) )
.withProjectId( artifactMetadata.getProject( ) )
.withVersion( artifactMetadata.getProjectVersion( ) )
.withArtifactId( artifactMetadata.getProject( ) )
.withArtifactVersion( artifactMetadata.getVersion( ) );
if (StringUtils.isNotEmpty( type ) ) {
selectorBuilder.withType( type );
}
if (StringUtils.isNotEmpty( classifier )) {
selectorBuilder.withClassifier( classifier );
}
BaseRepositoryContentLayout layout;
try
{
@ -90,17 +100,17 @@ public class ArtifactBuilder
{
throw new RuntimeException( "Could not convert to layout " + e.getMessage( ) );
}
StorageAsset file = layout.toFile( ref );
org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact( selectorBuilder.build( ) );
String extension = getExtensionFromFile(file);
Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() );
String extension = repoArtifact.getExtension();
Artifact artifact = new Artifact( repoArtifact.getVersion( ).getProject( ).getNamespace( ).getNamespace( ), repoArtifact.getId( ), repoArtifact.getArtifactVersion( ) );
artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
artifact.setClassifier( classifier );
artifact.setPackaging( type );
artifact.setType( type );
artifact.setFileExtension( extension );
artifact.setPath( managedRepositoryContent.toPath( ref ) );
artifact.setPath( managedRepositoryContent.toPath( repoArtifact ) );
// TODO: find a reusable formatter for this
double s = this.artifactMetadata.getSize();
String symbol = "b";
@ -125,7 +135,7 @@ public class ArtifactBuilder
DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
artifact.setSize( df.format( s ) + " " + symbol );
artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );
artifact.setId( repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType() );
return artifact;

View File

@ -35,6 +35,7 @@ import org.apache.archiva.configuration.Configuration;
import org.apache.archiva.configuration.FileTypes;
import org.apache.archiva.configuration.RepositoryGroupConfiguration;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.maven.content.MavenContentHelper;
import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
import org.apache.archiva.proxy.ProxyRegistry;
import org.apache.archiva.repository.EditableManagedRepository;
@ -68,6 +69,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
import javax.inject.Named;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@ -141,6 +143,9 @@ public class ArchivaDavResourceFactoryTest
@Inject
ProxyRegistry proxyRegistry;
@Inject
@Named( "MavenContentHelper" )
MavenContentHelper mavenContentHelper;
@Inject
DefaultRepositoryGroupAdmin defaultRepositoryGroupAdmin;
@ -255,11 +260,12 @@ public class ArchivaDavResourceFactoryTest
throws RepositoryAdminException
{
org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( repoId );
ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repo, artifactMappingProviders, fileTypes, fileLockManager);
ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repo, artifactMappingProviders, fileTypes, fileLockManager);
if (repo!=null && repo instanceof EditableManagedRepository)
{
( (EditableManagedRepository) repo ).setContent( repoContent );
}
repoContent.setMavenContentHelper( mavenContentHelper );
return repoContent;
}