PR: MNG-613

use new metadata in bootstrap

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@279535 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-09-08 08:28:06 +00:00
parent fca3fe1cf0
commit ba35fd5965
4 changed files with 421 additions and 37 deletions

View File

@ -26,7 +26,6 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
* @todo merge with artifactmetadatasource
* @todo retrieval exception not appropriate for store
* @todo not happy about the store/retrieve methods - they use "this"
* @todo separate specific interfaces
*/
public interface ArtifactMetadata
{

View File

@ -1,6 +1,7 @@
import compile.CompilerConfiguration;
import compile.JavacCompiler;
import download.ArtifactDownloader;
import download.RepositoryMetadata;
import jar.JarMojo;
import model.Dependency;
import model.ModelReader;
@ -11,17 +12,14 @@ import test.SurefirePlugin;
import util.AbstractReader;
import util.Commandline;
import util.FileUtils;
import util.IOUtil;
import util.IsolatedClassLoader;
import util.Os;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -815,22 +813,12 @@ public class MBoot
System.out.println( "Installing: " + file );
if ( version.indexOf( "SNAPSHOT" ) >= 0 )
{
// TODO: replace
File metadata = localRepository.getMetadataFile( groupId, artifactId, version, type,
finalName + ".version.txt" );
metadata.getParentFile().mkdirs();
IOUtil.copy( new StringReader( version ), new FileWriter( metadata ) );
}
FileUtils.copyFile( new File( basedir, BUILD_DIR + "/" + finalName + ".jar" ), file );
// TODO: replace
RepositoryMetadata metadata = new RepositoryMetadata();
metadata.setReleaseVersion( version );
file = localRepository.getMetadataFile( groupId, artifactId, null, type, artifactId + "-RELEASE.version.txt" );
IOUtil.copy( new StringReader( version ), new FileWriter( file ) );
metadata.write( file );
}
private void runTests( String basedir, String classes, String testClasses, ModelReader reader,

View File

@ -136,48 +136,114 @@ public class ArtifactDownloader
String version = dep.getVersion();
if ( snapshot )
{
// TODO: augment with maven-metadata
String filename = getSnapshotMetadataFile( destinationFile.getName(), "SNAPSHOT.version.txt" );
File file = localRepository.getMetadataFile( dep.getGroupId(), dep.getArtifactId(),
dep.getVersion(), dep.getType(), filename );
String filename = "maven-metadata-" + remoteRepo.getId() + ".xml";
File localFile = localRepository.getMetadataFile( dep.getGroupId(), dep.getArtifactId(),
dep.getVersion(), dep.getType(),
"maven-metadata-local.xml" );
File remoteFile = localRepository.getMetadataFile( dep.getGroupId(), dep.getArtifactId(),
dep.getVersion(), dep.getType(), filename );
String metadataPath = remoteRepo.getMetadataPath( dep.getGroupId(), dep.getArtifactId(),
dep.getVersion(), dep.getType(), filename );
String metaUrl = remoteRepo.getBasedir() + "/" + metadataPath;
log( "Downloading " + metaUrl );
try
{
HttpUtils.getFile( metaUrl, file, ignoreErrors, true, proxyHost, proxyPort, proxyUserName,
HttpUtils.getFile( metaUrl, remoteFile, ignoreErrors, true, proxyHost, proxyPort, proxyUserName,
proxyPassword, false );
}
catch ( IOException e )
{
log( "WARNING: remote SNAPSHOT version not found, using local: " + e.getMessage() );
log( "WARNING: remote metadata version not found, using local: " + e.getMessage() );
}
File file = localFile;
if ( remoteFile.exists() )
{
if ( !localFile.exists() || localFile.lastModified() < remoteFile.lastModified() )
{
file = remoteFile;
}
}
boolean found = false;
if ( file.exists() )
{
version = FileUtils.fileRead( file );
log( "Resolved version: " + version );
dep.setResolvedVersion( version );
if ( !version.endsWith( "SNAPSHOT" ) )
log( "Using metadata: " + file );
RepositoryMetadata metadata = RepositoryMetadata.read( file );
if ( version.equals( metadata.getVersion() ) )
{
String ver = version.substring(
version.lastIndexOf( "-", version.lastIndexOf( "-" ) - 1 ) + 1 );
String extension = url.substring( url.length() - 4 );
url = getSnapshotMetadataFile( url, ver + extension );
found = true;
version = metadata.constructVersion( version );
log( "Resolved version: " + version );
dep.setResolvedVersion( version );
if ( !version.endsWith( "SNAPSHOT" ) )
{
String ver = version.substring(
version.lastIndexOf( "-", version.lastIndexOf( "-" ) - 1 ) + 1 );
String extension = url.substring( url.length() - 4 );
url = getSnapshotMetadataFile( url, ver + extension );
}
else if ( destinationFile.exists() )
{
// It's already there
return true;
}
}
else if ( destinationFile.exists() )
else
{
// It's already there
return true;
log( "WARNING: versions did not match, not using metadata (" + version + " vs " +
metadata.getVersion() + ")" );
}
}
else
if ( !found )
{
log( "WARNING: local SNAPSHOT version not found, using default" );
log( "WARNING: attempting to use legacy metadata" );
filename = getSnapshotMetadataFile( destinationFile.getName(), "SNAPSHOT.version.txt" );
file = localRepository.getMetadataFile( dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
dep.getType(), filename );
metadataPath = remoteRepo.getMetadataPath( dep.getGroupId(), dep.getArtifactId(),
dep.getVersion(), dep.getType(), filename );
metaUrl = remoteRepo.getBasedir() + "/" + metadataPath;
log( "Downloading " + metaUrl );
try
{
HttpUtils.getFile( metaUrl, file, ignoreErrors, true, proxyHost, proxyPort, proxyUserName,
proxyPassword, false );
}
catch ( IOException e )
{
log( "WARNING: remote SNAPSHOT version not found, using local: " + e.getMessage() );
}
if ( file.exists() )
{
version = FileUtils.fileRead( file );
log( "Resolved version: " + version );
dep.setResolvedVersion( version );
if ( !version.endsWith( "SNAPSHOT" ) )
{
String ver = version.substring(
version.lastIndexOf( "-", version.lastIndexOf( "-" ) - 1 ) + 1 );
String extension = url.substring( url.length() - 4 );
url = getSnapshotMetadataFile( url, ver + extension );
}
else if ( destinationFile.exists() )
{
// It's already there
return true;
}
}
else
{
log( "WARNING: local SNAPSHOT version not found, using default" );
}
}
}
if ( !dep.getType().equals( "pom" ) )
if ( !"pom".equals( dep.getType() ) )
{
String name = dep.getArtifactId() + "-" + dep.getResolvedVersion() + ".pom";
File file = localRepository.getMetadataFile( dep.getGroupId(), dep.getArtifactId(),

View File

@ -0,0 +1,331 @@
package download;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import util.AbstractReader;
import util.StringUtils;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* TODO: describe
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class RepositoryMetadata
{
private String snapshotTimestamp;
private int snapshotBuildNumber;
private String releaseVersion;
private String groupId;
private String artifactId;
private String version;
private List versions = new ArrayList();
private String latestVersion;
public String getSnapshotTimestamp()
{
return snapshotTimestamp;
}
public void setSnapshotTimestamp( String snapshotTimestamp )
{
this.snapshotTimestamp = snapshotTimestamp;
}
public int getSnapshotBuildNumber()
{
return snapshotBuildNumber;
}
public void setSnapshotBuildNumber( int snapshotBuildNumber )
{
this.snapshotBuildNumber = snapshotBuildNumber;
}
public String getGroupId()
{
return groupId;
}
public void setGroupId( String groupId )
{
this.groupId = groupId;
}
public String getArtifactId()
{
return artifactId;
}
public void setArtifactId( String artifactId )
{
this.artifactId = artifactId;
}
public String getVersion()
{
return version;
}
public void setVersion( String version )
{
this.version = version;
}
public List getVersions()
{
return versions;
}
public void setVersions( List versions )
{
this.versions = versions;
}
public String getReleaseVersion()
{
return releaseVersion;
}
public void setReleaseVersion( String releaseVersion )
{
this.releaseVersion = releaseVersion;
}
public String getLatestVersion()
{
return latestVersion;
}
public void setLatestVersion( String latestVersion )
{
this.latestVersion = latestVersion;
}
public void addVersion( String version )
{
versions.add( version );
}
public static RepositoryMetadata read( File file )
throws IOException, ParserConfigurationException, SAXException
{
RepositoryMetadata metadata = new RepositoryMetadata();
new Reader( metadata ).parse( file );
return metadata;
}
public void write( File file )
throws IOException
{
new Writer( this ).write( file );
}
public String constructVersion( String baseVersion )
{
if ( snapshotTimestamp != null )
{
baseVersion = StringUtils.replace( baseVersion, "SNAPSHOT", snapshotTimestamp + "-" + snapshotBuildNumber );
}
return baseVersion;
}
static class Reader
extends AbstractReader
{
private boolean insideVersioning;
private StringBuffer bodyText = new StringBuffer();
private boolean insideSnapshot;
private final RepositoryMetadata metadata;
private boolean insideVersions;
public Reader( RepositoryMetadata metadata )
{
this.metadata = metadata;
}
public void startElement( String uri, String localName, String rawName, Attributes attributes )
{
if ( insideVersioning )
{
if ( insideSnapshot )
{
if ( "buildNumber".equals( rawName ) )
{
metadata.setSnapshotBuildNumber( Integer.valueOf( getBodyText() ).intValue() );
}
else if ( "timestamp".equals( rawName ) )
{
metadata.setSnapshotTimestamp( getBodyText() );
}
}
else if ( insideVersions )
{
if ( "version".equals( rawName ) )
{
metadata.addVersion( getBodyText() );
}
}
else
{
if ( "snapshot".equals( rawName ) )
{
insideSnapshot = true;
}
else if ( "versions".equals( rawName ) )
{
insideVersions = true;
}
else if ( "latest".equals( rawName ) )
{
metadata.setLatestVersion( getBodyText() );
}
else if ( "release".equals( rawName ) )
{
metadata.setReleaseVersion( getBodyText() );
}
}
}
else
{
// root element
if ( "versioning".equals( rawName ) )
{
insideVersioning = true;
}
else if ( "groupId".equals( rawName ) )
{
metadata.setGroupId( getBodyText() );
}
else if ( "artifactId".equals( rawName ) )
{
metadata.setArtifactId( getBodyText() );
}
else if ( "version".equals( rawName ) )
{
metadata.setVersion( getBodyText() );
}
}
}
public void characters( char buffer[], int start, int length )
{
bodyText.append( buffer, start, length );
}
private String getBodyText()
{
return bodyText.toString().trim();
}
public void endElement( String uri, String localName, String rawName )
throws SAXException
{
if ( insideVersioning )
{
if ( "versioning".equals( rawName ) )
{
insideVersioning = false;
}
if ( "snapshot".equals( rawName ) )
{
insideSnapshot = false;
}
if ( "versions".equals( rawName ) )
{
insideVersions = false;
}
}
bodyText = new StringBuffer();
}
}
static class Writer
{
private final RepositoryMetadata metadata;
public Writer( RepositoryMetadata metadata )
{
this.metadata = metadata;
}
public void write( File file )
throws IOException
{
PrintWriter w = new PrintWriter( new FileWriter( file ) );
try
{
w.println( "<metadata>" );
writeLine( w, " ", "groupId", metadata.getGroupId() );
writeLine( w, " ", "artifactId", metadata.getArtifactId() );
writeLine( w, " ", "version", metadata.getVersion() );
w.println( " <versioning>" );
writeLine( w, " ", "latest", metadata.getLatestVersion() );
writeLine( w, " ", "release", metadata.getReleaseVersion() );
w.println( " <snapshot>" );
writeLine( w, " ", "buildNumber", String.valueOf( metadata.getSnapshotBuildNumber() ) );
writeLine( w, " ", "timestamp", metadata.getSnapshotTimestamp() );
w.println( " </snapshot>" );
w.println( " <versions>" );
for ( Iterator i = metadata.getVersions().iterator(); i.hasNext(); )
{
writeLine( w, " ", "version", (String) i.next() );
}
w.println( " </versions>" );
w.println( " </versioning>" );
w.println( "</metadata>" );
}
finally
{
w.close();
}
}
private void writeLine( PrintWriter w, String indent, String tag, String content )
{
if ( content != null )
{
w.println( indent + ( "<" + tag + ">" + content + "</" + tag + ">" ) );
}
}
}
}