refactor mboot

- model reading separated
- define mboot dependencies in its own POM


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163745 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-04-04 01:36:58 +00:00
parent 0cd1aec3ef
commit f5270e45f7
7 changed files with 808 additions and 734 deletions

View File

@ -10,4 +10,57 @@
<name>Maven MBoot</name>
<version>2.0-SNAPSHOT</version>
<description>Tool used to bootstrap m2.</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello</artifactId>
<version>core-1.0-alpha-1</version>
</dependency>
<dependency>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello</artifactId>
<version>xdoc-plugin-1.0-alpha-1</version>
</dependency>
<dependency>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello</artifactId>
<version>xml-plugin-1.0-alpha-1</version>
</dependency>
<dependency>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello</artifactId>
<version>xpp3-plugin-1.0-alpha-1</version>
</dependency>
<!-- TODO: should get these from the surefire plugin -->
<dependency>
<groupId>surefire</groupId>
<artifactId>surefire-booter</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>surefire</groupId>
<artifactId>surefire</artifactId>
<version>1.2</version>
</dependency>
<!-- Just for install... possibly not needed here? -->
<dependency>
<groupId>classworlds</groupId>
<artifactId>classworlds</artifactId>
<version>1.1-alpha-1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.0-alpha-2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</model>

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
package download;
import model.Dependency;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
@ -84,11 +86,12 @@ public class ArtifactDownloader
{
for ( Iterator j = files.iterator(); j.hasNext(); )
{
String file = (String) j.next();
Dependency dep = (Dependency) j.next();
if ( !downloadedArtifacts.contains( file ) )
if ( !downloadedArtifacts.contains( dep ) )
{
File destinationFile = new File( mavenRepoLocal, file );
String repositoryPath = dep.getRepositoryPath();
File destinationFile = new File( mavenRepoLocal, repositoryPath );
// The directory structure for this project may
// not exists so create it if missing.
File directory = destinationFile.getParentFile();
@ -98,19 +101,19 @@ public class ArtifactDownloader
directory.mkdirs();
}
if ( destinationFile.exists() && file.indexOf( SNAPSHOT_SIGNATURE ) < 0 )
if ( destinationFile.exists() && dep.getVersion().indexOf( SNAPSHOT_SIGNATURE ) < 0 )
{
continue;
}
getRemoteArtifact( file, destinationFile );
getRemoteArtifact( repositoryPath, destinationFile );
if ( !destinationFile.exists() )
{
throw new Exception( "Failed to download " + file );
throw new Exception( "Failed to download " + dep );
}
downloadedArtifacts.add( file );
downloadedArtifacts.add( dep );
}
}
}
@ -170,15 +173,8 @@ public class ArtifactDownloader
try
{
log( "Downloading " + url );
HttpUtils.getFile( url,
destinationFile,
ignoreErrors,
useTimestamp,
proxyHost,
proxyPort,
proxyUserName,
proxyPassword,
true );
HttpUtils.getFile( url, destinationFile, ignoreErrors, useTimestamp, proxyHost, proxyPort,
proxyUserName, proxyPassword, true );
// Artifact was found, continue checking additional remote repos (if any)
// in case there is a newer version (i.e. snapshots) in another repo

View File

@ -0,0 +1,182 @@
package model;
/*
* 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.
*/
/**
* Describes a dependency.
*
* @version $Id$
*/
public class Dependency
{
private String id;
private String version;
private String url;
private String jar;
private String artifactId;
private String groupId;
private String type = "jar";
public Dependency()
{
}
public void setId( String id )
{
this.id = id;
}
public String getId()
{
if ( isValid( getGroupId() ) && isValid( getArtifactId() ) )
{
return getGroupId() + ":" + getArtifactId();
}
return id;
}
public void setGroupId( String groupId )
{
this.groupId = groupId;
}
public String getGroupId()
{
return groupId;
}
public String getArtifactDirectory()
{
if ( isValid( getGroupId() ) )
{
return getGroupId();
}
return getId();
}
public String getArtifactId()
{
return artifactId;
}
public void setArtifactId( String artifactId )
{
this.artifactId = artifactId;
}
public String getArtifact()
{
// If the jar name has been explicty set then use that. This
// is when the <jar/> element is explicity used in the POM.
if ( jar != null )
{
return jar;
}
String artifact;
if ( isValid( getArtifactId() ) )
{
artifact = getArtifactId() + "-" + getVersion() + ".";
}
else
{
artifact = getId() + "-" + getVersion() + ".";
}
if ( "jar".equals( getType() ) || "maven-plugin".equals( getType() ) )
{
artifact += "jar";
}
else
{
artifact += getType();
}
return artifact;
}
public void setVersion( String version )
{
this.version = version;
}
public String getVersion()
{
return version;
}
public void setJar( String jar )
{
// This is a check we need because of the jelly interpolation
// process. If we don't check an empty string will be set and
// screw up getArtifact() above.
if ( jar.trim().length() == 0 )
{
return;
}
this.jar = jar;
}
public String getJar()
{
return jar;
}
public void setUrl( String url )
{
this.url = url;
}
public String getUrl()
{
return url;
}
public String getType()
{
return type;
}
public void setType( String type )
{
this.type = type;
}
private boolean isValid( String value )
{
if ( value != null && value.trim().equals( "" ) == false )
{
return true;
}
return false;
}
public String getRepositoryPath()
{
return getArtifactDirectory() + "/" + getType() + "s/" + getArtifact();
}
}

View File

@ -0,0 +1,316 @@
package model;
/*
* 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 download.ArtifactDownloader;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import util.AbstractReader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Parse a POM.
*
* @version $Id$
*/
public class ModelReader
extends AbstractReader
{
private int depth = 0;
private String artifactId;
private String version;
private String groupId;
private String packaging = "jar";
private String parentGroupId;
private String parentArtifactId;
private String parentVersion;
private List dependencies = new ArrayList();
private List remoteRepositories = new ArrayList();
private List resources = new ArrayList();
private List testResources = new ArrayList();
private Dependency currentDependency;
private Resource currentResource;
private boolean insideParent = false;
private boolean insideDependency = false;
private boolean insideResource = false;
private boolean insideRepository = false;
private StringBuffer bodyText = new StringBuffer();
private final ArtifactDownloader downloader;
public ModelReader( ArtifactDownloader downloader )
{
this.downloader = downloader;
}
public List getRemoteRepositories()
{
return remoteRepositories;
}
public List getDependencies()
{
return dependencies;
}
public List getResources()
{
return resources;
}
public void startElement( String uri, String localName, String rawName, Attributes attributes )
{
if ( rawName.equals( "parent" ) )
{
insideParent = true;
}
else if ( rawName.equals( "repository" ) )
{
insideRepository = true;
}
else if ( rawName.equals( "dependency" ) )
{
currentDependency = new Dependency();
insideDependency = true;
}
else if ( rawName.equals( "resource" ) )
{
currentResource = new Resource();
insideResource = true;
}
else if ( rawName.equals( "testResource" ) )
{
currentResource = new Resource();
insideResource = true;
}
depth++;
}
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
{
// support both v3 <extend> and v4 <parent>
if ( rawName.equals( "parent" ) )
{
File f;
if ( parentArtifactId == null || parentArtifactId.trim().length() == 0 )
{
throw new SAXException( "Missing required element in <parent>: artifactId." );
}
if ( parentGroupId == null || parentGroupId.trim().length() == 0 )
{
throw new SAXException( "Missing required element in <parent>: groupId." );
}
if ( parentVersion == null || parentVersion.trim().length() == 0 )
{
throw new SAXException( "Missing required element in <parent>: version." );
}
if ( groupId == null )
{
groupId = parentGroupId;
}
if ( version == null )
{
version = parentVersion;
}
f = new File( downloader.getMavenRepoLocal(), parentGroupId + "/poms/" + parentArtifactId + "-" +
parentVersion + ".pom" );
ModelReader p = new ModelReader( downloader );
if ( !p.parse( f ) )
{
throw new SAXException( "Could not parse parent pom.xml" );
}
dependencies.addAll( p.getDependencies() );
resources.addAll( p.getResources() );
insideParent = false;
}
else if ( rawName.equals( "dependency" ) )
{
dependencies.add( currentDependency );
insideDependency = false;
}
else if ( rawName.equals( "resource" ) )
{
resources.add( currentResource );
insideResource = false;
}
else if ( rawName.equals( "testResource" ) )
{
testResources.add( currentResource );
insideResource = false;
}
else if ( insideParent )
{
if ( rawName.equals( "groupId" ) )
{
parentGroupId = getBodyText();
}
else if ( rawName.equals( "artifactId" ) )
{
parentArtifactId = getBodyText();
}
else if ( rawName.equals( "version" ) )
{
parentVersion = getBodyText();
}
}
else if ( insideDependency )
{
if ( rawName.equals( "id" ) )
{
currentDependency.setId( getBodyText() );
}
else if ( rawName.equals( "version" ) )
{
currentDependency.setVersion( getBodyText() );
}
else if ( rawName.equals( "jar" ) )
{
currentDependency.setJar( getBodyText() );
}
else if ( rawName.equals( "type" ) )
{
currentDependency.setType( getBodyText() );
}
else if ( rawName.equals( "groupId" ) )
{
currentDependency.setGroupId( getBodyText() );
}
else if ( rawName.equals( "artifactId" ) )
{
currentDependency.setArtifactId( getBodyText() );
}
}
else if ( insideResource )
{
if ( rawName.equals( "directory" ) )
{
currentResource.setDirectory( getBodyText() );
}
else if ( rawName.equals( "include" ) )
{
currentResource.addInclude( getBodyText() );
}
else if ( rawName.equals( "exclude" ) )
{
currentResource.addExclude( getBodyText() );
}
}
else if ( depth == 2 )
{
if ( rawName.equals( "artifactId" ) )
{
artifactId = getBodyText();
}
else if ( rawName.equals( "version" ) )
{
version = getBodyText();
}
else if ( rawName.equals( "groupId" ) )
{
groupId = getBodyText();
}
else if ( rawName.equals( "packaging" ) )
{
packaging = getBodyText();
}
else if ( rawName.equals( "repository" ) )
{
insideRepository = false;
}
}
else if ( insideRepository )
{
if ( rawName.equals( "url" ) )
{
remoteRepositories.add( getBodyText() );
}
}
bodyText = new StringBuffer();
depth--;
}
public String getArtifactId()
{
return artifactId;
}
public String getVersion()
{
return version;
}
public String getGroupId()
{
return groupId;
}
public String getPackaging()
{
return packaging;
}
}

View File

@ -0,0 +1,66 @@
package model;
/*
* 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 java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Describes a resource.
*
* @version $Id$
*/
public class Resource
implements Serializable
{
private String directory;
private List includes = new ArrayList();
private List excludes = new ArrayList();
public void addInclude( String pattern )
{
this.includes.add( pattern );
}
public void addExclude( String pattern )
{
this.excludes.add( pattern );
}
public List getIncludes()
{
return this.includes;
}
public List getExcludes()
{
return this.excludes;
}
public void setDirectory( String directory )
{
this.directory = directory;
}
public String getDirectory()
{
return this.directory;
}
}

View File

@ -0,0 +1,80 @@
package util;
/*
* 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.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileInputStream;
/**
* Parse an XML file.
*
* @version $Id$
*/
public abstract class AbstractReader
extends DefaultHandler
{
private SAXParserFactory saxFactory;
public boolean parse( File file )
{
try
{
saxFactory = SAXParserFactory.newInstance();
SAXParser parser = saxFactory.newSAXParser();
InputSource is = new InputSource( new FileInputStream( file ) );
parser.parse( is, this );
return true;
}
catch ( Exception e )
{
e.printStackTrace();
return false;
}
}
public void warning( SAXParseException spe )
{
printParseError( "Warning", spe );
}
public void error( SAXParseException spe )
{
printParseError( "Error", spe );
}
public void fatalError( SAXParseException spe )
{
printParseError( "Fatal Error", spe );
}
private final void printParseError( String type, SAXParseException spe )
{
System.err.println( type + " [line " + spe.getLineNumber() + ", row " + spe.getColumnNumber() + "]: " +
spe.getMessage() );
}
}