Skip to content

Commit

Permalink
[MSHARED-811] Improve handling of Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Apr 1, 2019
1 parent 7cd1dc2 commit 46660e4
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.channels.UnsupportedAddressTypeException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -31,6 +32,9 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -42,6 +46,7 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
import org.apache.maven.shared.transfer.metadata.ArtifactMetadata;
import org.apache.maven.shared.transfer.repository.RepositoryManager;

/**
Expand Down Expand Up @@ -113,6 +118,15 @@ private void deployerProject( ProjectBuildingRequest pbr )
artifactWithClassifier.setRepository( session.getProjectBuildingRequest().getLocalRepository() );

Collection<Artifact> mavenArtifacts = Arrays.<Artifact>asList( artifact, artifactWithClassifier );

for ( Artifact a : mavenArtifacts )
{
File camVfile = File.createTempFile( "test-deploy", ".camV", artifactsDirectory );
a.addMetadata( new CustomArtifactMetadata( a, camVfile, true ) );

File camGfile = File.createTempFile( "test-deploy", ".camG", artifactsDirectory );
a.addMetadata( new CustomArtifactMetadata( a, camGfile, false ) );
}

deployer.deploy( session.getProjectBuildingRequest(), mavenArtifacts );
}
Expand All @@ -126,4 +140,77 @@ private void deployerProject( ProjectBuildingRequest pbr )
}
}

private class CustomArtifactMetadata extends AbstractArtifactMetadata implements ArtifactMetadata
{
private final File file;

private final boolean storedInArtifactVersionDirectory;

protected CustomArtifactMetadata( Artifact artifact, File file, boolean storedInArtifactVersionDirectory )
{
super( artifact );
this.file = file;
this.storedInArtifactVersionDirectory = storedInArtifactVersionDirectory;
}

@Override
public File getFile()
{
return file;
}

@Override
public String getRemoteFilename()
{
return artifact.getArtifactId() + '-' + artifact.getVersion() + getDotExtension();
}

@Override
public String getLocalFilename( ArtifactRepository repository )
{
return artifact.getArtifactId() + '-' + artifact.getVersion() + getDotExtension();
}

@Override
public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
throws RepositoryMetadataStoreException
{
throw new UnsupportedOperationException("ArtifactDeployerMojo.CustomArtifactMetadata.storeInLocalRepository(ArtifactRepository, ArtifactRepository)");
}

@Override
public boolean storedInArtifactVersionDirectory()
{
return storedInArtifactVersionDirectory;
}

@Override
public void merge( org.apache.maven.artifact.metadata.ArtifactMetadata metadata )
{
throw new UnsupportedOperationException("ArtifactDeployerMojo.CustomArtifactMetadata.merge(ArtifactMetadata)");
}

@Override
public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
{
throw new UnsupportedOperationException("ArtifactDeployerMojo.CustomArtifactMetadata.merge(ArtifactMetadata)");
}

@Override
public String getBaseVersion()
{
return artifact.getBaseVersion();
}

@Override
public Object getKey()
{
return artifact.getId() + getDotExtension();
}

private String getDotExtension()
{
return file.getName().substring( file.getName().lastIndexOf( '.' ) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void buildExample()
.withCliOption( "-DmvnVersion=" + mavenRuntime.getMavenVersion() ) // Might be superfluous
.withCliOption( "-B" )
.withCliOption( "-V" )
.withCliOption( "-e" )
.execute( "clean", "verify" );
//@formatter:on

Expand All @@ -92,6 +93,8 @@ public void buildExample()
File baseDirectoy = new File( localRepo, "ARTIFACT-DEPLOYER-GROUPID-" + mvnVersion + "/ARTIFACTID/VERSION/" );

checkForArtifactFile( baseDirectoy );
checkForArtifactGroupMetaFile( baseDirectoy );
checkForArtifactVersionMetaFile( baseDirectoy );
checkForArtifactClassifierFile( baseDirectoy );

assertTrue( new File( localRepo, "ARTIFACT-DEPLOYER-GROUPID-" + mvnVersion
Expand All @@ -117,4 +120,22 @@ private void checkForArtifactFile( File baseDirectoy )
assertTrue( "artifactFile md5 not found.", new File( artifactFile.getAbsolutePath() + ".md5" ).exists() );
assertTrue( "artifactFile sha1 not found.", new File( artifactFile.getAbsolutePath() + ".sha1" ).exists() );
}

private void checkForArtifactGroupMetaFile( File baseDirectoy )
{
File localFile = new File( baseDirectoy.getParentFile(), "ARTIFACTID-VERSION-local.camG" );
File baseFile = new File( baseDirectoy.getParentFile(), "ARTIFACTID-VERSION.camG" );
assertTrue( "localFile '" + localFile.getAbsolutePath() + "'", localFile.exists() );
assertTrue( "artifactFile md5 not found.", new File( baseFile.getAbsolutePath() + ".md5" ).exists() );
assertTrue( "artifactFile sha1 not found.", new File( baseFile.getAbsolutePath() + ".sha1" ).exists() );
}

private void checkForArtifactVersionMetaFile( File baseDirectoy )
{
File localFile = new File( baseDirectoy, "ARTIFACTID-VERSION-local.camV" );
File baseFile = new File( baseDirectoy, "ARTIFACTID-VERSION.camV" );
assertTrue( "localFile '" + localFile.getAbsolutePath() + "'", localFile.exists() );
assertTrue( "artifactFile md5 not found.", new File( baseFile.getAbsolutePath() + ".md5" ).exists() );
assertTrue( "artifactFile sha1 not found.", new File( baseFile.getAbsolutePath() + ".sha1" ).exists() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -42,6 +45,7 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
import org.apache.maven.shared.transfer.metadata.ArtifactMetadata;
import org.apache.maven.shared.transfer.repository.RepositoryManager;

/**
Expand Down Expand Up @@ -111,6 +115,15 @@ private void installProject( ProjectBuildingRequest pbr )
artifactWithClassifier.setFile( tmpFileClassifier );

Collection<Artifact> mavenArtifacts = Arrays.<Artifact>asList( artifact, artifactWithClassifier );

for ( Artifact a : mavenArtifacts )
{
File camVfile = File.createTempFile( "test-install", ".camV", artifactsDirectory );
a.addMetadata( new CustomArtifactMetadata( a, camVfile, true ) );

File camGfile = File.createTempFile( "test-install", ".camG", artifactsDirectory );
a.addMetadata( new CustomArtifactMetadata( a, camGfile, false ) );
}

installer.install( session.getProjectBuildingRequest(), mavenArtifacts );
}
Expand All @@ -125,4 +138,77 @@ private void installProject( ProjectBuildingRequest pbr )

}

private class CustomArtifactMetadata extends AbstractArtifactMetadata implements ArtifactMetadata
{
private final File file;

private final boolean storedInArtifactVersionDirectory;

protected CustomArtifactMetadata( Artifact artifact, File file, boolean storedInArtifactVersionDirectory )
{
super( artifact );
this.file = file;
this.storedInArtifactVersionDirectory = storedInArtifactVersionDirectory;
}

@Override
public File getFile()
{
return file;
}

@Override
public String getRemoteFilename()
{
return artifact.getArtifactId() + '-' + artifact.getVersion() + getDotExtension();
}

@Override
public String getLocalFilename( ArtifactRepository repository )
{
return artifact.getArtifactId() + '-' + artifact.getVersion() + getDotExtension();
}

@Override
public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
throws RepositoryMetadataStoreException
{
throw new UnsupportedOperationException("ArtifactDeployerMojo.CustomArtifactMetadata.storeInLocalRepository(ArtifactRepository, ArtifactRepository)");
}

@Override
public boolean storedInArtifactVersionDirectory()
{
return storedInArtifactVersionDirectory;
}

@Override
public void merge( org.apache.maven.artifact.metadata.ArtifactMetadata metadata )
{
throw new UnsupportedOperationException("ArtifactDeployerMojo.CustomArtifactMetadata.merge(ArtifactMetadata)");
}

@Override
public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
{
throw new UnsupportedOperationException("ArtifactDeployerMojo.CustomArtifactMetadata.merge(ArtifactMetadata)");
}

@Override
public String getBaseVersion()
{
return artifact.getBaseVersion();
}

@Override
public Object getKey()
{
return artifact.getId() + getDotExtension();
}

private String getDotExtension()
{
return file.getName().substring( file.getName().lastIndexOf( '.' ) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public void buildExample()
// We don't have a pom file.
checkForNonExistingPomFile( baseDirectoy );
checkForArtifact( baseDirectoy );
checkForArtifactGroupMetaFile( baseDirectoy );
checkForArtifactVersionMetaFile( baseDirectoy );
checkForArtifactClassifier( baseDirectoy );
}

Expand All @@ -116,6 +118,20 @@ private void checkForArtifact( File baseDirectoy )
assertFalse( "artifactFile md5 not found.", new File( artifactFile.getAbsolutePath() + ".md5" ).exists() );
assertFalse( "artifactFile sha1 not found.", new File( artifactFile.getAbsolutePath() + ".sha1" ).exists() );
}

private void checkForArtifactGroupMetaFile( File baseDirectoy )
{
File localFile = new File( baseDirectoy.getParentFile(), "ARTIFACTID-VERSION-local.camG" );
File baseFile = new File( baseDirectoy.getParentFile(), "ARTIFACTID-VERSION.camG" );
assertTrue( "localFile '" + localFile.getAbsolutePath() + "'", localFile.exists() );
}

private void checkForArtifactVersionMetaFile( File baseDirectoy )
{
File localFile = new File( baseDirectoy, "ARTIFACTID-VERSION-local.camV" );
File baseFile = new File( baseDirectoy, "ARTIFACTID-VERSION.camV" );
assertTrue( "localFile '" + localFile.getAbsolutePath() + "'", localFile.exists() );
}

private void checkForNonExistingPomFile( File baseDirectoy )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
import org.apache.maven.shared.transfer.metadata.internal.Maven30MetadataBridge;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.sonatype.aether.RepositorySystem;
Expand Down Expand Up @@ -109,9 +110,12 @@ else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
{
// eaten, handled by repo system
}
else
else if ( metadata instanceof org.apache.maven.shared.transfer.metadata.ArtifactMetadata )
{
// request.addMetadata( new MetadataBridge( metadata ) );
org.apache.maven.shared.transfer.metadata.ArtifactMetadata transferMedata =
( org.apache.maven.shared.transfer.metadata.ArtifactMetadata ) metadata;

request.addMetadata( new Maven30MetadataBridge( metadata ).setFile( transferMedata.getFile() ) );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
import org.apache.maven.shared.transfer.metadata.internal.Maven31MetadataBridge;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.eclipse.aether.RepositorySystem;
Expand Down Expand Up @@ -109,9 +110,12 @@ else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
{
// eaten, handled by repo system
}
else
else if ( metadata instanceof org.apache.maven.shared.transfer.metadata.ArtifactMetadata )
{
// request.addMetadata( new MetadataBridge( metadata ) );
org.apache.maven.shared.transfer.metadata.ArtifactMetadata transferMetadata =
(org.apache.maven.shared.transfer.metadata.ArtifactMetadata) metadata;

request.addMetadata( new Maven31MetadataBridge( metadata ).setFile( transferMetadata.getFile() ) );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
import org.apache.maven.shared.transfer.metadata.internal.Maven30MetadataBridge;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
Expand Down Expand Up @@ -89,9 +90,12 @@ else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
{
// eaten, handled by repo system
}
else
else if ( metadata instanceof org.apache.maven.shared.transfer.metadata.ArtifactMetadata )
{
// request.addMetadata( new MetadataBridge( metadata ) );
org.apache.maven.shared.transfer.metadata.ArtifactMetadata transferMedata =
( org.apache.maven.shared.transfer.metadata.ArtifactMetadata ) metadata;

request.addMetadata( new Maven30MetadataBridge( metadata ).setFile( transferMedata.getFile() ) );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
import org.apache.maven.shared.transfer.metadata.internal.Maven31MetadataBridge;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
Expand Down Expand Up @@ -89,9 +90,12 @@ else if ( // metadata instanceof SnapshotArtifactRepositoryMetadata ||
{
// eaten, handled by repo system
}
else
else if ( metadata instanceof org.apache.maven.shared.transfer.metadata.ArtifactMetadata )
{
// request.addMetadata( new MetadataBridge( metadata ) );
org.apache.maven.shared.transfer.metadata.ArtifactMetadata transferMetadata =
(org.apache.maven.shared.transfer.metadata.ArtifactMetadata) metadata;

request.addMetadata( new Maven31MetadataBridge( metadata ).setFile( transferMetadata.getFile() ) );
}
}
}
Expand Down
Loading

0 comments on commit 46660e4

Please sign in to comment.