mirror of https://github.com/apache/archiva.git
[MRM-125] implement timestamp based discovery
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@424918 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
dfebf13ace
commit
a96bd2dc93
|
@ -20,14 +20,22 @@ import org.apache.maven.artifact.Artifact;
|
|||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
||||
import org.codehaus.plexus.util.IOUtil;
|
||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||
import org.codehaus.plexus.util.xml.Xpp3DomWriter;
|
||||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Base class for artifact discoverers.
|
||||
|
@ -48,38 +56,41 @@ public abstract class AbstractArtifactDiscoverer
|
|||
|
||||
private static final String POM = ".pom";
|
||||
|
||||
/**
|
||||
* Scan the repository for artifact paths.
|
||||
*/
|
||||
private String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns )
|
||||
private List scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, long comparisonTimestamp )
|
||||
{
|
||||
return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES );
|
||||
return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES,
|
||||
comparisonTimestamp );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of artifacts found in a specified repository
|
||||
*
|
||||
* @param repository The ArtifactRepository to discover artifacts
|
||||
* @param blacklistedPatterns Comma-delimited list of string paths that will be excluded in the discovery
|
||||
* @param includeSnapshots if the repository contains snapshots which should also be included
|
||||
* @return list of artifacts
|
||||
*/
|
||||
public List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots )
|
||||
public List discoverArtifacts( ArtifactRepository repository, String operation, String blacklistedPatterns,
|
||||
boolean includeSnapshots )
|
||||
throws DiscovererException
|
||||
{
|
||||
if ( !"file".equals( repository.getProtocol() ) )
|
||||
{
|
||||
throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
|
||||
}
|
||||
|
||||
long comparisonTimestamp = readComparisonTimestamp( repository, operation );
|
||||
|
||||
File repositoryBase = new File( repository.getBasedir() );
|
||||
|
||||
List artifacts = new ArrayList();
|
||||
|
||||
String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
|
||||
List artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns, comparisonTimestamp );
|
||||
|
||||
for ( int i = 0; i < artifactPaths.length; i++ )
|
||||
try
|
||||
{
|
||||
String path = artifactPaths[i];
|
||||
setLastCheckedTime( repository, operation, new Date() );
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
throw new DiscovererException( "Error writing metadata: " + e.getMessage(), e );
|
||||
}
|
||||
|
||||
for ( Iterator i = artifactPaths.iterator(); i.hasNext(); )
|
||||
{
|
||||
String path = (String) i.next();
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -114,11 +125,12 @@ public abstract class AbstractArtifactDiscoverer
|
|||
|
||||
File repositoryBase = new File( repository.getBasedir() );
|
||||
|
||||
String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
|
||||
// TODO: if we keep this method, set comparison timestamp properly!
|
||||
List artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns, 0 );
|
||||
|
||||
for ( int i = 0; i < artifactPaths.length; i++ )
|
||||
for ( Iterator i = artifactPaths.iterator(); i.hasNext(); )
|
||||
{
|
||||
String path = artifactPaths[i];
|
||||
String path = (String) i.next();
|
||||
|
||||
String filename = repositoryBase.getAbsolutePath() + "/" + path;
|
||||
|
||||
|
@ -163,6 +175,78 @@ public abstract class AbstractArtifactDiscoverer
|
|||
return artifacts;
|
||||
}
|
||||
|
||||
public void resetLastCheckedTime( ArtifactRepository repository, String operation )
|
||||
throws IOException
|
||||
{
|
||||
// TODO: get these changes into maven-metadata.xml and migrate towards that. The model is further diverging to a different layout at each level so submodels might be a good idea.
|
||||
// TODO: maven-artifact probably needs an improved pathOfMetadata to cope with top level metadata
|
||||
// TODO: might we need to write this as maven-metadata-local in some circumstances? merge others? Probably best to keep it simple and just use this format at the root. No need to merge anything that I can see
|
||||
// TODO: since this metadata isn't meant to be shared, perhaps another file is called for after all.
|
||||
// Format is: <repository><lastDiscovery><KEY>yyyyMMddHHmmss</KEY></lastDiscovery></repository> (ie, flat properties)
|
||||
|
||||
File file = new File( repository.getBasedir(), "maven-metadata.xml" );
|
||||
|
||||
Xpp3Dom dom = readDom( file );
|
||||
|
||||
Xpp3Dom lastDiscoveryDom = getLastDiscoveryDom( dom );
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
// do this in reverse so that removing doesn't affect counter
|
||||
Xpp3Dom[] children = lastDiscoveryDom.getChildren();
|
||||
for ( int i = lastDiscoveryDom.getChildCount() - 1; i >= 0; i-- )
|
||||
{
|
||||
if ( children[i].getName().equals( operation ) )
|
||||
{
|
||||
changed = true;
|
||||
lastDiscoveryDom.removeChild( i );
|
||||
}
|
||||
}
|
||||
|
||||
if ( changed )
|
||||
{
|
||||
saveDom( file, dom );
|
||||
}
|
||||
}
|
||||
|
||||
private void saveDom( File file, Xpp3Dom dom )
|
||||
throws IOException
|
||||
{
|
||||
FileWriter writer = new FileWriter( file );
|
||||
|
||||
// save metadata
|
||||
try
|
||||
{
|
||||
Xpp3DomWriter.write( writer, dom );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( writer );
|
||||
}
|
||||
}
|
||||
|
||||
public void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
|
||||
throws IOException
|
||||
{
|
||||
// see notes in resetLastCheckedTime
|
||||
|
||||
File file = new File( repository.getBasedir(), "maven-metadata.xml" );
|
||||
|
||||
Xpp3Dom dom = readDom( file );
|
||||
|
||||
Xpp3Dom lastDiscoveryDom = getLastDiscoveryDom( dom );
|
||||
|
||||
Xpp3Dom entry = lastDiscoveryDom.getChild( operation );
|
||||
if ( entry == null )
|
||||
{
|
||||
entry = new Xpp3Dom( operation );
|
||||
lastDiscoveryDom.addChild( entry );
|
||||
}
|
||||
entry.setValue( new SimpleDateFormat( DATE_FMT, Locale.US ).format( date ) );
|
||||
|
||||
saveDom( file, dom );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an artifact object that is represented by the specified path in a repository
|
||||
*
|
||||
|
|
|
@ -17,16 +17,27 @@ package org.apache.maven.repository.discovery;
|
|||
*/
|
||||
|
||||
import org.apache.maven.artifact.factory.ArtifactFactory;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.codehaus.plexus.logging.AbstractLogEnabled;
|
||||
import org.codehaus.plexus.util.DirectoryScanner;
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
import org.codehaus.plexus.util.IOUtil;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
|
||||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Base class for the artifact and metadata discoverers.
|
||||
|
@ -48,6 +59,8 @@ public abstract class AbstractDiscoverer
|
|||
|
||||
private List excludedPaths = new ArrayList();
|
||||
|
||||
protected static final String DATE_FMT = "yyyyMMddHHmmss";
|
||||
|
||||
/**
|
||||
* Add a path to the list of files that were kicked out due to being invalid.
|
||||
*
|
||||
|
@ -69,11 +82,8 @@ public abstract class AbstractDiscoverer
|
|||
return kickedOutPaths.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan the repository for artifact paths.
|
||||
*/
|
||||
protected String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, String[] includes,
|
||||
String[] excludes )
|
||||
protected List scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, String[] includes,
|
||||
String[] excludes, long comparisonTimestamp )
|
||||
{
|
||||
List allExcludes = new ArrayList();
|
||||
allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
|
||||
|
@ -104,7 +114,19 @@ public abstract class AbstractDiscoverer
|
|||
excludedPaths.add( new DiscovererPath( path, "Artifact was in the specified list of exclusions" ) );
|
||||
}
|
||||
|
||||
return scanner.getIncludedFiles();
|
||||
// TODO: this could be a part of the scanner
|
||||
List includedPaths = new ArrayList();
|
||||
for ( Iterator files = Arrays.asList( scanner.getIncludedFiles() ).iterator(); files.hasNext(); )
|
||||
{
|
||||
String path = files.next().toString();
|
||||
|
||||
if ( comparisonTimestamp == 0 || new File( repositoryBase, path ).lastModified() > comparisonTimestamp )
|
||||
{
|
||||
includedPaths.add( path );
|
||||
}
|
||||
}
|
||||
|
||||
return includedPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -116,4 +138,67 @@ public abstract class AbstractDiscoverer
|
|||
{
|
||||
return excludedPaths.iterator();
|
||||
}
|
||||
|
||||
protected long readComparisonTimestamp( ArtifactRepository repository, String operation )
|
||||
{
|
||||
File file = new File( repository.getBasedir(), "maven-metadata.xml" );
|
||||
Xpp3Dom dom = readDom( file );
|
||||
dom = getLastDiscoveryDom( dom );
|
||||
Xpp3Dom entry = dom.getChild( operation );
|
||||
long comparisonTimestamp = 0;
|
||||
if ( entry != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
comparisonTimestamp = new SimpleDateFormat( DATE_FMT, Locale.US ).parse( entry.getValue() ).getTime();
|
||||
}
|
||||
catch ( ParseException e )
|
||||
{
|
||||
getLogger().error( "Timestamp was invalid: " + entry.getValue() + "; ignoring" );
|
||||
}
|
||||
}
|
||||
return comparisonTimestamp;
|
||||
}
|
||||
|
||||
protected Xpp3Dom readDom( File file )
|
||||
{
|
||||
Xpp3Dom dom;
|
||||
FileReader fileReader = null;
|
||||
try
|
||||
{
|
||||
fileReader = new FileReader( file );
|
||||
dom = Xpp3DomBuilder.build( fileReader );
|
||||
}
|
||||
catch ( FileNotFoundException e )
|
||||
{
|
||||
// Safe to ignore
|
||||
dom = new Xpp3Dom( "metadata" );
|
||||
}
|
||||
catch ( XmlPullParserException e )
|
||||
{
|
||||
getLogger().error( "Error reading metadata (ignoring and recreating): " + e.getMessage() );
|
||||
dom = new Xpp3Dom( "metadata" );
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
getLogger().error( "Error reading metadata (ignoring and recreating): " + e.getMessage() );
|
||||
dom = new Xpp3Dom( "metadata" );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( fileReader );
|
||||
}
|
||||
return dom;
|
||||
}
|
||||
|
||||
protected Xpp3Dom getLastDiscoveryDom( Xpp3Dom dom )
|
||||
{
|
||||
Xpp3Dom lastDiscoveryDom = dom.getChild( "lastDiscovery" );
|
||||
if ( lastDiscoveryDom == null )
|
||||
{
|
||||
dom.addChild( new Xpp3Dom( "lastDiscovery" ) );
|
||||
lastDiscoveryDom = dom.getChild( "lastDiscovery" );
|
||||
}
|
||||
return lastDiscoveryDom;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ package org.apache.maven.repository.discovery;
|
|||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -39,11 +41,15 @@ public interface ArtifactDiscoverer
|
|||
* Discover artifacts in the repository.
|
||||
*
|
||||
* @param repository the location of the repository
|
||||
* @param operation the operation being used to discover for timestamp checking
|
||||
* @param blacklistedPatterns pattern that lists any files to prevent from being included when scanning
|
||||
* @param includeSnapshots whether to discover snapshots
|
||||
* @return the list of artifacts discovered
|
||||
* @throws DiscovererException if there was an unrecoverable problem discovering artifacts or recording progress
|
||||
*/
|
||||
List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots );
|
||||
List discoverArtifacts( ArtifactRepository repository, String operation, String blacklistedPatterns,
|
||||
boolean includeSnapshots )
|
||||
throws DiscovererException;
|
||||
|
||||
/**
|
||||
* Discover standalone POM artifacts in the repository.
|
||||
|
@ -66,4 +72,25 @@ public interface ArtifactDiscoverer
|
|||
*/
|
||||
Artifact buildArtifact( String path )
|
||||
throws DiscovererException;
|
||||
|
||||
/**
|
||||
* Reset the time in the repository that indicates the last time a check was performed.
|
||||
*
|
||||
* @param repository the location of the repository
|
||||
* @param operation the operation to record the timestamp for
|
||||
* @throws java.io.IOException if there is a non-recoverable problem reading or writing the metadata
|
||||
*/
|
||||
void resetLastCheckedTime( ArtifactRepository repository, String operation )
|
||||
throws IOException;
|
||||
|
||||
/**
|
||||
* Set the time in the repository that indicates the last time a check was performed.
|
||||
*
|
||||
* @param repository the location of the repository
|
||||
* @param operation the operation to record the timestamp for
|
||||
* @param date the date to set the last check to
|
||||
* @throws java.io.IOException if there is a non-recoverable problem reading or writing the metadata
|
||||
*/
|
||||
void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
|
||||
throws IOException;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ package org.apache.maven.repository.discovery;
|
|||
*/
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
|
||||
import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
|
||||
import org.apache.maven.artifact.repository.metadata.Metadata;
|
||||
|
@ -56,38 +57,33 @@ public class DefaultMetadataDiscoverer
|
|||
private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/*-metadata.xml", "**/*/*-metadata.xml",
|
||||
"**/*/*/*-metadata.xml", "**/*-metadata-*.xml", "**/*/*-metadata-*.xml", "**/*/*/*-metadata-*.xml"};
|
||||
|
||||
/**
|
||||
* @see org.apache.maven.repository.discovery.MetadataDiscoverer#discoverMetadata(java.io.File,String)
|
||||
*/
|
||||
public List discoverMetadata( File repositoryBase, String blacklistedPatterns )
|
||||
public List discoverMetadata( ArtifactRepository repository, String operation, String blacklistedPatterns )
|
||||
{
|
||||
List metadataFiles = new ArrayList();
|
||||
String[] metadataPaths =
|
||||
scanForArtifactPaths( repositoryBase, blacklistedPatterns, STANDARD_DISCOVERY_INCLUDES, null );
|
||||
long comparisonTimestamp = readComparisonTimestamp( repository, operation );
|
||||
|
||||
for ( int i = 0; i < metadataPaths.length; i++ )
|
||||
List metadataFiles = new ArrayList();
|
||||
List metadataPaths = scanForArtifactPaths( new File( repository.getBasedir() ), blacklistedPatterns,
|
||||
STANDARD_DISCOVERY_INCLUDES, null, comparisonTimestamp );
|
||||
|
||||
// TODO: save! should we be using a different entry for metadata?
|
||||
|
||||
for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
|
||||
{
|
||||
String metadataPath = (String) i.next();
|
||||
try
|
||||
{
|
||||
RepositoryMetadata metadata = buildMetadata( repositoryBase.getPath(), metadataPaths[i] );
|
||||
RepositoryMetadata metadata = buildMetadata( repository.getBasedir(), metadataPath );
|
||||
metadataFiles.add( metadata );
|
||||
}
|
||||
catch ( DiscovererException e )
|
||||
{
|
||||
addKickedOutPath( metadataPaths[i], e.getMessage() );
|
||||
addKickedOutPath( metadataPath, e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
return metadataFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create RepositoryMetadata object.
|
||||
*
|
||||
* @param repo The path to the repository.
|
||||
* @param metadataPath The path to the metadata file.
|
||||
* @return the metadata
|
||||
*/
|
||||
private RepositoryMetadata buildMetadata( String repo, String metadataPath )
|
||||
throws DiscovererException
|
||||
{
|
||||
|
@ -95,7 +91,7 @@ public class DefaultMetadataDiscoverer
|
|||
String repoPath = repo + "/" + metadataPath;
|
||||
try
|
||||
{
|
||||
URL url = new File( repoPath ).toURL();
|
||||
URL url = new File( repoPath ).toURI().toURL();
|
||||
InputStream is = url.openStream();
|
||||
Reader reader = new InputStreamReader( is );
|
||||
MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
|
||||
|
|
|
@ -16,7 +16,8 @@ package org.apache.maven.repository.discovery;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -30,8 +31,10 @@ public interface MetadataDiscoverer
|
|||
/**
|
||||
* Search for metadata files in the repository.
|
||||
*
|
||||
* @param repositoryBase The repository directory.
|
||||
* @param repository The repository.
|
||||
* @param operation the operation being performed (used for timestamp comparison)
|
||||
* @param blacklistedPatterns Patterns that are to be excluded from the discovery process.
|
||||
* @return the list of artifacts found
|
||||
*/
|
||||
List discoverMetadata( File repositoryBase, String blacklistedPatterns );
|
||||
List discoverMetadata( ArtifactRepository repository, String operation, String blacklistedPatterns );
|
||||
}
|
||||
|
|
|
@ -16,14 +16,21 @@ package org.apache.maven.repository.discovery;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.codehaus.plexus.PlexusTestCase;
|
||||
import org.apache.maven.artifact.factory.ArtifactFactory;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.factory.ArtifactFactory;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
|
||||
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
|
||||
import org.codehaus.plexus.PlexusTestCase;
|
||||
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @author Edwin Punzalan
|
||||
|
@ -37,6 +44,8 @@ public abstract class AbstractArtifactDiscovererTest
|
|||
|
||||
protected ArtifactRepository repository;
|
||||
|
||||
protected static final String TEST_OPERATION = "test";
|
||||
|
||||
protected abstract String getLayout();
|
||||
|
||||
protected abstract File getRepositoryFile();
|
||||
|
@ -51,6 +60,8 @@ public abstract class AbstractArtifactDiscovererTest
|
|||
factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
|
||||
|
||||
repository = getRepository();
|
||||
|
||||
removeTimestampMetadata();
|
||||
}
|
||||
|
||||
protected ArtifactRepository getRepository()
|
||||
|
@ -60,7 +71,8 @@ public abstract class AbstractArtifactDiscovererTest
|
|||
|
||||
ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
|
||||
|
||||
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, getLayout() );
|
||||
ArtifactRepositoryLayout layout =
|
||||
(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, getLayout() );
|
||||
|
||||
return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
|
||||
}
|
||||
|
@ -75,8 +87,125 @@ public abstract class AbstractArtifactDiscovererTest
|
|||
return factory.createArtifact( groupId, artifactId, version, null, type );
|
||||
}
|
||||
|
||||
protected Artifact createArtifact( String groupId, String artifactId, String version, String type, String classifier )
|
||||
protected Artifact createArtifact( String groupId, String artifactId, String version, String type,
|
||||
String classifier )
|
||||
{
|
||||
return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
|
||||
}
|
||||
|
||||
public void testUpdatedInRepository()
|
||||
throws ComponentLookupException, DiscovererException, ParseException, IOException
|
||||
{
|
||||
// Set repository time to 1-1-2000, a time in the distant past so definitely updated
|
||||
discoverer.setLastCheckedTime( repository, "update",
|
||||
new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" ) );
|
||||
|
||||
List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertTrue( "Check included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
|
||||
|
||||
// try again with the updated timestamp
|
||||
artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertFalse( "Check not included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
}
|
||||
|
||||
public void testNotUpdatedInRepository()
|
||||
throws ComponentLookupException, DiscovererException, IOException
|
||||
{
|
||||
// Set repository time to now, which is after any artifacts, so definitely not updated
|
||||
discoverer.setLastCheckedTime( repository, "update", new Date() );
|
||||
|
||||
List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertFalse( "Check not included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
}
|
||||
|
||||
public void testNotUpdatedInRepositoryForcedDiscovery()
|
||||
throws ComponentLookupException, DiscovererException, IOException
|
||||
{
|
||||
discoverer.resetLastCheckedTime( repository, "update" );
|
||||
|
||||
List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertTrue( "Check included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
|
||||
// try again with the updated timestamp
|
||||
artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertFalse( "Check not included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
}
|
||||
|
||||
public void testNotUpdatedInRepositoryForcedDiscoveryMetadataAlreadyExists()
|
||||
throws ComponentLookupException, DiscovererException, IOException
|
||||
{
|
||||
discoverer.setLastCheckedTime( repository, "update", new Date() );
|
||||
|
||||
discoverer.resetLastCheckedTime( repository, "update" );
|
||||
|
||||
List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertTrue( "Check included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
|
||||
// try again with the updated timestamp
|
||||
artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertFalse( "Check not included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
}
|
||||
|
||||
public void testNotUpdatedInRepositoryForcedDiscoveryOtherMetadataAlreadyExists()
|
||||
throws ComponentLookupException, DiscovererException, IOException
|
||||
{
|
||||
discoverer.setLastCheckedTime( repository, "test", new Date() );
|
||||
|
||||
discoverer.resetLastCheckedTime( repository, "update" );
|
||||
|
||||
List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertTrue( "Check included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
|
||||
// try again with the updated timestamp
|
||||
artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertFalse( "Check not included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
|
||||
}
|
||||
|
||||
public void testNoRepositoryMetadata()
|
||||
throws ComponentLookupException, DiscovererException, ParseException, IOException
|
||||
{
|
||||
removeTimestampMetadata();
|
||||
|
||||
// should find all
|
||||
List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
|
||||
assertNotNull( "Check artifacts not null", artifacts );
|
||||
|
||||
assertTrue( "Check included",
|
||||
artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
|
||||
}
|
||||
|
||||
private void removeTimestampMetadata()
|
||||
{
|
||||
// remove the metadata that tracks time
|
||||
File file = new File( repository.getBasedir(), "maven-metadata.xml" );
|
||||
file.delete();
|
||||
assertFalse( file.exists() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@ package org.apache.maven.repository.discovery;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
|
||||
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
|
||||
import org.codehaus.plexus.PlexusTestCase;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -30,7 +33,9 @@ public class DefaultMetadataDiscovererTest
|
|||
{
|
||||
private MetadataDiscoverer discoverer;
|
||||
|
||||
private File repositoryLocation;
|
||||
private static final String TEST_OPERATION = "test";
|
||||
|
||||
private ArtifactRepository repository;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -41,7 +46,22 @@ public class DefaultMetadataDiscovererTest
|
|||
super.setUp();
|
||||
|
||||
discoverer = (MetadataDiscoverer) lookup( MetadataDiscoverer.ROLE, "default" );
|
||||
repositoryLocation = getTestFile( "src/test/repository" );
|
||||
|
||||
repository = getRepository();
|
||||
|
||||
removeTimestampMetadata();
|
||||
}
|
||||
|
||||
protected ArtifactRepository getRepository()
|
||||
throws Exception
|
||||
{
|
||||
File basedir = getTestFile( "src/test/repository" );
|
||||
|
||||
ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
|
||||
|
||||
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
|
||||
|
||||
return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,7 +79,7 @@ public class DefaultMetadataDiscovererTest
|
|||
*/
|
||||
public void testMetadataDiscovererSuccess()
|
||||
{
|
||||
List metadataPaths = discoverer.discoverMetadata( repositoryLocation, null );
|
||||
List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
|
||||
assertNotNull( "Check metadata not null", metadataPaths );
|
||||
assertEquals( 3, metadataPaths.size() );
|
||||
}
|
||||
|
@ -69,7 +89,7 @@ public class DefaultMetadataDiscovererTest
|
|||
*/
|
||||
public void testKickoutWrongDirectory()
|
||||
{
|
||||
discoverer.discoverMetadata( repositoryLocation, null );
|
||||
discoverer.discoverMetadata( repository, TEST_OPERATION, null );
|
||||
Iterator iter = discoverer.getKickedOutPathsIterator();
|
||||
boolean found = false;
|
||||
while ( iter.hasNext() && !found )
|
||||
|
@ -93,7 +113,7 @@ public class DefaultMetadataDiscovererTest
|
|||
*/
|
||||
public void testKickoutBlankMetadata()
|
||||
{
|
||||
discoverer.discoverMetadata( repositoryLocation, null );
|
||||
discoverer.discoverMetadata( repository, TEST_OPERATION, null );
|
||||
Iterator iter = discoverer.getKickedOutPathsIterator();
|
||||
boolean found = false;
|
||||
while ( iter.hasNext() && !found )
|
||||
|
@ -112,4 +132,11 @@ public class DefaultMetadataDiscovererTest
|
|||
assertTrue( found );
|
||||
}
|
||||
|
||||
private void removeTimestampMetadata()
|
||||
{
|
||||
// remove the metadata that tracks time
|
||||
File file = new File( repository.getBasedir(), "maven-metadata.xml" );
|
||||
file.delete();
|
||||
assertFalse( file.exists() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
dummy content. sample file only.
|
|
@ -0,0 +1 @@
|
|||
dummy content. sample file only.
|
|
@ -0,0 +1 @@
|
|||
dummy content. sample file only.
|
|
@ -0,0 +1,10 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.apache.maven.update</groupId>
|
||||
<artifactId>test-not-updated</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>Maven Test Repository Artifact Discovery</name>
|
||||
<!-- default packaging is jar -->
|
||||
<!--packaging>jar</packaging-->
|
||||
</project>
|
|
@ -0,0 +1 @@
|
|||
dummy content. sample file only.
|
|
@ -0,0 +1,10 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.apache.maven.update</groupId>
|
||||
<artifactId>test-updated</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>Maven Test Repository Artifact Discovery</name>
|
||||
<!-- default packaging is jar -->
|
||||
<!--packaging>jar</packaging-->
|
||||
</project>
|
Loading…
Reference in New Issue