isolate path generation in Repository class

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163753 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-04-04 05:08:45 +00:00
parent a6bc152aca
commit 27363f855e
5 changed files with 243 additions and 144 deletions

View File

@ -5,6 +5,7 @@ import download.ArtifactDownloader;
import jar.JarMojo; import jar.JarMojo;
import model.Dependency; import model.Dependency;
import model.ModelReader; import model.ModelReader;
import model.Repository;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import test.SurefirePlugin; import test.SurefirePlugin;
@ -18,6 +19,7 @@ import util.Os;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -91,10 +93,6 @@ public class MBoot
private ArtifactDownloader downloader; private ArtifactDownloader downloader;
private String repoLocal;
private List coreDeps;
private boolean online = true; private boolean online = true;
private static final String SCOPE_TEST = "test"; private static final String SCOPE_TEST = "test";
@ -138,8 +136,6 @@ public class MBoot
public void run( String[] args ) public void run( String[] args )
throws Exception throws Exception
{ {
ModelReader reader = new ModelReader( downloader );
String mavenRepoLocal = System.getProperty( "maven.repo.local" ); String mavenRepoLocal = System.getProperty( "maven.repo.local" );
SettingsReader userModelReader = new SettingsReader(); SettingsReader userModelReader = new SettingsReader();
@ -202,7 +198,16 @@ public class MBoot
System.out.println( "HOWEVER, since you did not specify a repository path, maven will use: " + System.out.println( "HOWEVER, since you did not specify a repository path, maven will use: " +
repoDir.getAbsolutePath() + " to store artifacts locally." ); repoDir.getAbsolutePath() + " to store artifacts locally." );
} }
repoLocal = mavenRepoLocal;
File repoLocalFile = new File( mavenRepoLocal );
repoLocalFile.mkdirs();
if ( !repoLocalFile.canWrite() )
{
System.err.println( "Can't write to " + mavenRepoLocal );
System.exit( 1 );
}
String mavenHome = null; String mavenHome = null;
@ -235,9 +240,13 @@ public class MBoot
online = false; online = false;
} }
Repository localRepository = new Repository( mavenRepoLocal, Repository.LAYOUT_LEGACY );
ModelReader reader = new ModelReader( localRepository );
if ( online ) if ( online )
{ {
downloader = new ArtifactDownloader( repoLocal, reader.getRemoteRepositories() ); downloader = new ArtifactDownloader( localRepository, reader.getRemoteRepositories() );
if ( userModelReader.getActiveProxy() != null ) if ( userModelReader.getActiveProxy() != null )
{ {
Proxy proxy = userModelReader.getActiveProxy(); Proxy proxy = userModelReader.getActiveProxy();
@ -245,34 +254,38 @@ public class MBoot
} }
} }
reader = new ModelReader( downloader ); reader = new ModelReader( localRepository );
String basedir = System.getProperty( "user.dir" ); String basedir = System.getProperty( "user.dir" );
reader.parse( new File( basedir, "maven-mboot2/pom.xml" ) ); reader.parse( new File( basedir, "maven-mboot2/pom.xml" ) );
ClassLoader bootstrapClassLoader = createClassloaderFromDependencies( reader.getDependencies(), null ); ClassLoader bootstrapClassLoader = createClassloaderFromDependencies( reader.getDependencies(), null,
localRepository );
reader = new ModelReader( downloader ); reader = new ModelReader( localRepository );
reader.parse( new File( basedir, "maven-plugins/maven-surefire-plugin/pom.xml" ) ); reader.parse( new File( basedir, "maven-plugins/maven-surefire-plugin/pom.xml" ) );
List surefireDependencies = reader.getDependencies(); List surefireDependencies = reader.getDependencies();
reader = new ModelReader( downloader ); reader = new ModelReader( localRepository );
// Install maven-components POM // Install maven-components POM
installPomFile( repoLocal, new File( basedir, "pom.xml" ) ); installPomFile( localRepository, new File( basedir, "pom.xml" ) );
// Install plugin-parent POM // Install plugin-parent POM
installPomFile( repoLocal, new File( basedir, "maven-plugins/pom.xml" ) ); installPomFile( localRepository, new File( basedir, "maven-plugins/pom.xml" ) );
// Install plugin-tools-parent POM // Install plugin-tools-parent POM
installPomFile( repoLocal, new File( basedir, "maven-plugin-tools/pom.xml" ) ); installPomFile( localRepository, new File( basedir, "maven-plugin-tools/pom.xml" ) );
// Install maven-script-parent POM // Install maven-script-parent POM
installPomFile( repoLocal, new File( basedir, "maven-script/pom.xml" ) ); installPomFile( localRepository, new File( basedir, "maven-script/pom.xml" ) );
// Install it-support POM // Install it-support POM
installPomFile( repoLocal, new File( basedir, "maven-core-it-support/pom.xml" ) ); installPomFile( localRepository, new File( basedir, "maven-core-it-support/pom.xml" ) );
List coreDeps = null;
Dependency corePom = null;
for ( int i = 0; i < builds.length; i++ ) for ( int i = 0; i < builds.length; i++ )
{ {
@ -284,21 +297,25 @@ public class MBoot
System.setProperty( "basedir", directory ); System.setProperty( "basedir", directory );
reader = buildProject( directory, builds[i], bootstrapClassLoader, surefireDependencies ); reader = buildProject( directory, builds[i], bootstrapClassLoader, surefireDependencies, localRepository );
if ( reader.getArtifactId().equals( "maven-core" ) ) if ( reader.getArtifactId().equals( "maven-core" ) )
{ {
coreDeps = reader.getDependencies(); coreDeps = reader.getDependencies();
corePom = new Dependency( reader.getGroupId(), reader.getArtifactId(), reader.getVersion(),
reader.getPackaging() );
} }
System.out.println( "--------------------------------------------------------------------" ); System.out.println( "--------------------------------------------------------------------" );
} }
reader = new ModelReader( downloader ); reader = new ModelReader( localRepository );
reader.parse( new File( basedir, "maven-plugin-tools/maven-plugin-tools-pluggy/pom.xml" ) ); reader.parse( new File( basedir, "maven-plugin-tools/maven-plugin-tools-pluggy/pom.xml" ) );
List dependencies = new ArrayList( reader.getDependencies() ); List dependencies = new ArrayList( reader.getDependencies() );
dependencies.add( new Dependency( reader.getGroupId(), reader.getArtifactId(), reader.getVersion() ) ); dependencies.add(
IsolatedClassLoader cl = createClassloaderFromDependencies( dependencies, bootstrapClassLoader ); new Dependency( reader.getGroupId(), reader.getArtifactId(), reader.getVersion(), reader.getPackaging() ) );
IsolatedClassLoader cl = createClassloaderFromDependencies( dependencies, bootstrapClassLoader,
localRepository );
for ( int i = 0; i < pluginBuilds.length; i++ ) for ( int i = 0; i < pluginBuilds.length; i++ )
{ {
@ -310,7 +327,7 @@ public class MBoot
System.setProperty( "basedir", directory ); System.setProperty( "basedir", directory );
reader = buildProject( directory, pluginBuilds[i], cl, surefireDependencies ); reader = buildProject( directory, pluginBuilds[i], cl, surefireDependencies, localRepository );
System.out.println( "--------------------------------------------------------------------" ); System.out.println( "--------------------------------------------------------------------" );
} }
@ -370,7 +387,7 @@ public class MBoot
{ {
Dependency d = (Dependency) i.next(); Dependency d = (Dependency) i.next();
File source = new File( repoLocal, d.getRepositoryPath() ); File source = localRepository.getArtifactFile( d );
if ( d.getArtifactId().equals( "classworlds" ) ) if ( d.getArtifactId().equals( "classworlds" ) )
{ {
FileUtils.copyFileToDirectory( source, boot ); FileUtils.copyFileToDirectory( source, boot );
@ -386,9 +403,7 @@ public class MBoot
} }
// Copy maven itself // Copy maven itself
FileUtils.copyFileToDirectory( localRepository.getArtifactFile( corePom ), lib );
// TODO: create a dependency object
FileUtils.copyFileToDirectory( new File( repoLocal, "org.apache.maven/jars/maven-core-2.0-SNAPSHOT.jar" ), lib );
System.out.println(); System.out.println();
@ -429,13 +444,13 @@ public class MBoot
System.out.println( "Finished at: " + fullStop ); System.out.println( "Finished at: " + fullStop );
} }
public ModelReader buildProject( String basedir, String projectId, ClassLoader classLoader, private ModelReader buildProject( String basedir, String projectId, ClassLoader classLoader,
List surefireDependencies ) List surefireDependencies, Repository localRepository )
throws Exception throws Exception
{ {
System.out.println( "Building project in " + basedir ); System.out.println( "Building project in " + basedir );
ModelReader reader = new ModelReader( downloader ); ModelReader reader = new ModelReader( localRepository );
if ( !reader.parse( new File( basedir, "pom.xml" ) ) ) if ( !reader.parse( new File( basedir, "pom.xml" ) ) )
{ {
@ -532,11 +547,12 @@ public class MBoot
if ( new File( generatedSources ).exists() ) if ( new File( generatedSources ).exists() )
{ {
compile( reader.getDependencies(), sources, classes, null, generatedSources, SCOPE_COMPILE ); compile( reader.getDependencies(), sources, classes, null, generatedSources, SCOPE_COMPILE,
localRepository );
} }
else else
{ {
compile( reader.getDependencies(), sources, classes, null, null, SCOPE_COMPILE ); compile( reader.getDependencies(), sources, classes, null, null, SCOPE_COMPILE, localRepository );
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@ -548,7 +564,7 @@ public class MBoot
System.out.println( "Generating maven plugin descriptor ..." ); System.out.println( "Generating maven plugin descriptor ..." );
generatePluginDescriptor( sources, new File( classes, "META-INF/maven" ).getAbsolutePath(), generatePluginDescriptor( sources, new File( classes, "META-INF/maven" ).getAbsolutePath(),
new File( basedir, "pom.xml" ).getAbsolutePath(), classLoader ); new File( basedir, "pom.xml" ).getAbsolutePath(), classLoader, localRepository );
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@ -567,7 +583,7 @@ public class MBoot
List testDependencies = reader.getDependencies(); List testDependencies = reader.getDependencies();
compile( testDependencies, testSources, testClasses, classes, null, SCOPE_TEST ); compile( testDependencies, testSources, testClasses, classes, null, SCOPE_TEST, localRepository );
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Test resources // Test resources
@ -581,7 +597,7 @@ public class MBoot
// Run tests // Run tests
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
runTests( basedir, classes, testClasses, reader, surefireDependencies ); runTests( basedir, classes, testClasses, reader, surefireDependencies, localRepository );
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Create JAR // Create JAR
@ -589,30 +605,32 @@ public class MBoot
createJar( classes, buildDir, reader ); createJar( classes, buildDir, reader );
installPom( basedir, repoLocal, reader ); installPom( basedir, localRepository, reader );
String artifactId = reader.getArtifactId(); String artifactId = reader.getArtifactId();
if ( !artifactId.equals( "maven-plugin" ) && artifactId.endsWith( "plugin" ) ) if ( !artifactId.equals( "maven-plugin" ) && artifactId.endsWith( "plugin" ) )
{ {
install( basedir, repoLocal, reader, "maven-plugin" ); install( basedir, localRepository, reader, "maven-plugin" );
} }
else else
{ {
install( basedir, repoLocal, reader, "jar" ); install( basedir, localRepository, reader, "jar" );
} }
return reader; return reader;
} }
private void generatePluginDescriptor( String sourceDirectory, String outputDirectory, String pom, ClassLoader cl ) private void generatePluginDescriptor( String sourceDirectory, String outputDirectory, String pom, ClassLoader cl,
Repository localRepository )
throws Exception throws Exception
{ {
Class cls = cl.loadClass( "org.apache.maven.tools.plugin.pluggy.Main" ); Class cls = cl.loadClass( "org.apache.maven.tools.plugin.pluggy.Main" );
Method m = cls.getMethod( "main", new Class[]{String[].class} ); Method m = cls.getMethod( "main", new Class[]{String[].class} );
String[] args = {"descriptor", sourceDirectory, outputDirectory, pom, repoLocal}; // Can pluggy really cope with the layout?
String[] args = {"descriptor", sourceDirectory, outputDirectory, pom, localRepository.getBasedir()};
m.invoke( null, new Object[]{args} ); m.invoke( null, new Object[]{args} );
} }
@ -638,7 +656,8 @@ public class MBoot
Thread.currentThread().setContextClassLoader( old ); Thread.currentThread().setContextClassLoader( old );
} }
private IsolatedClassLoader createClassloaderFromDependencies( List dependencies, ClassLoader parent ) private IsolatedClassLoader createClassloaderFromDependencies( List dependencies, ClassLoader parent,
Repository localRepository )
throws Exception throws Exception
{ {
if ( online ) if ( online )
@ -662,7 +681,7 @@ public class MBoot
{ {
Dependency dependency = (Dependency) i.next(); Dependency dependency = (Dependency) i.next();
File f = new File( repoLocal, dependency.getRepositoryPath() ); File f = localRepository.getArtifactFile( dependency );
if ( !f.exists() ) if ( !f.exists() )
{ {
String msg = ( !online ? "; run again online" : "; there was a problem downloading it earlier" ); String msg = ( !online ? "; run again online" : "; there was a problem downloading it earlier" );
@ -687,10 +706,10 @@ public class MBoot
jarMojo.execute( new File( classes ), buildDir, artifactId + "-" + version ); jarMojo.execute( new File( classes ), buildDir, artifactId + "-" + version );
} }
private void installPomFile( String repoLocal, File pomIn ) private void installPomFile( Repository localRepository, File pomIn )
throws Exception throws Exception
{ {
ModelReader reader = new ModelReader( downloader ); ModelReader reader = new ModelReader( localRepository );
if ( !reader.parse( pomIn ) ) if ( !reader.parse( pomIn ) )
{ {
@ -699,21 +718,18 @@ public class MBoot
System.exit( 1 ); System.exit( 1 );
} }
String artifactId = reader.getArtifactId(); installPomFile( reader, localRepository, pomIn );
String version = reader.getVersion();
String groupId = reader.getGroupId();
File pom = new File( repoLocal, "/" + groupId + "/poms/" + artifactId + "-" + version + ".pom" );
System.out.println( "Installing POM: " + pom );
FileUtils.copyFile( pomIn, pom );
} }
private void installPom( String basedir, String repoLocal, ModelReader reader ) private void installPom( String basedir, Repository localRepository, ModelReader reader )
throws Exception throws Exception
{
installPomFile( reader, localRepository, new File( basedir, "pom.xml" ) );
}
private void installPomFile( ModelReader reader, Repository localRepository, File source )
throws IOException
{ {
String artifactId = reader.getArtifactId(); String artifactId = reader.getArtifactId();
@ -721,14 +737,14 @@ public class MBoot
String groupId = reader.getGroupId(); String groupId = reader.getGroupId();
File pom = new File( repoLocal, "/" + groupId + "/poms/" + artifactId + "-" + version + ".pom" ); File pom = localRepository.getArtifactFile( groupId, artifactId, version, "pom" );
System.out.println( "Installing POM: " + pom ); System.out.println( "Installing POM: " + pom );
FileUtils.copyFile( new File( basedir, "pom.xml" ), pom ); FileUtils.copyFile( source, pom );
} }
private void install( String basedir, String repoLocal, ModelReader reader, String type ) private void install( String basedir, Repository localRepository, ModelReader reader, String type )
throws Exception throws Exception
{ {
String artifactId = reader.getArtifactId(); String artifactId = reader.getArtifactId();
@ -739,22 +755,24 @@ public class MBoot
String finalName = artifactId + "-" + version; String finalName = artifactId + "-" + version;
File file = new File( repoLocal, "/" + groupId + "/" + type + "s/" + finalName + ".jar" ); File file = localRepository.getArtifactFile( groupId, artifactId, version, type );
System.out.println( "Installing: " + file ); System.out.println( "Installing: " + file );
FileUtils.copyFile( new File( basedir, BUILD_DIR + "/" + finalName + ".jar" ), file );
if ( version.indexOf( "SNAPSHOT" ) >= 0 ) if ( version.indexOf( "SNAPSHOT" ) >= 0 )
{ {
File metadata = new File( repoLocal, "/" + groupId + "/poms/" + finalName + ".version.txt" ); File metadata = localRepository.getMetadataFile( groupId, artifactId, version, type,
finalName + ".version.txt" );
IOUtil.copy( new StringReader( version ), new FileWriter( metadata ) ); IOUtil.copy( new StringReader( version ), new FileWriter( metadata ) );
} }
FileUtils.copyFile( new File( basedir, BUILD_DIR + "/" + finalName + ".jar" ), file );
} }
private void runTests( String basedir, String classes, String testClasses, ModelReader reader, private void runTests( String basedir, String classes, String testClasses, ModelReader reader,
List surefireDependencies ) List surefireDependencies, Repository localRepository )
throws Exception throws Exception
{ {
SurefirePlugin testRunner = new SurefirePlugin(); SurefirePlugin testRunner = new SurefirePlugin();
@ -776,7 +794,7 @@ public class MBoot
List depList = new ArrayList( reader.getDependencies() ); List depList = new ArrayList( reader.getDependencies() );
depList.addAll( surefireDependencies ); depList.addAll( surefireDependencies );
List classpath = classpath( depList, null, SCOPE_TEST ); List classpath = classpath( depList, null, SCOPE_TEST, localRepository );
classpath.add( classes ); classpath.add( classes );
classpath.add( testClasses ); classpath.add( testClasses );
boolean success = testRunner.execute( basedir, includes, excludes, classpath, reportsDir ); boolean success = testRunner.execute( basedir, includes, excludes, classpath, reportsDir );
@ -791,7 +809,7 @@ public class MBoot
// Compile // Compile
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private List classpath( List dependencies, String extraClasspath, String scope ) private List classpath( List dependencies, String extraClasspath, String scope, Repository localRepository )
{ {
List classpath = new ArrayList( dependencies.size() + 1 ); List classpath = new ArrayList( dependencies.size() + 1 );
@ -799,7 +817,7 @@ public class MBoot
{ {
Dependency d = (Dependency) dependencies.get( i ); Dependency d = (Dependency) dependencies.get( i );
String element = repoLocal + "/" + d.getRepositoryPath(); String element = localRepository.getArtifactFile( d ).getAbsolutePath();
if ( SCOPE_COMPILE.equals( scope ) ) if ( SCOPE_COMPILE.equals( scope ) )
{ {
@ -827,7 +845,7 @@ public class MBoot
} }
private void compile( List dependencies, String sourceDirectory, String outputDirectory, String extraClasspath, private void compile( List dependencies, String sourceDirectory, String outputDirectory, String extraClasspath,
String generatedSources, String scope ) String generatedSources, String scope, Repository localRepository )
throws Exception throws Exception
{ {
JavacCompiler compiler = new JavacCompiler(); JavacCompiler compiler = new JavacCompiler();
@ -861,7 +879,8 @@ public class MBoot
CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation( outputDirectory ); compilerConfiguration.setOutputLocation( outputDirectory );
compilerConfiguration.setClasspathEntries( classpath( dependencies, extraClasspath, scope ) ); compilerConfiguration.setClasspathEntries(
classpath( dependencies, extraClasspath, scope, localRepository ) );
compilerConfiguration.setSourceLocations( Arrays.asList( sourceDirectories ) ); compilerConfiguration.setSourceLocations( Arrays.asList( sourceDirectories ) );
/* Compile with debugging info */ /* Compile with debugging info */

View File

@ -1,6 +1,7 @@
package download; package download;
import model.Dependency; import model.Dependency;
import model.Repository;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -14,8 +15,6 @@ public class ArtifactDownloader
{ {
public static final String SNAPSHOT_SIGNATURE = "-SNAPSHOT"; public static final String SNAPSHOT_SIGNATURE = "-SNAPSHOT";
private File mavenRepoLocal;
private List remoteRepos; private List remoteRepos;
private boolean useTimestamp = true; private boolean useTimestamp = true;
@ -30,7 +29,9 @@ public class ArtifactDownloader
private String proxyPassword; private String proxyPassword;
public ArtifactDownloader( String localRepository, List remoteRepositories ) private Repository localRepository;
public ArtifactDownloader( Repository localRepository, List remoteRepositories )
throws Exception throws Exception
{ {
setRemoteRepos( remoteRepositories ); setRemoteRepos( remoteRepositories );
@ -42,34 +43,12 @@ public class ArtifactDownloader
System.exit( 1 ); System.exit( 1 );
} }
mavenRepoLocal = new File( localRepository ); this.localRepository = localRepository;
if ( !mavenRepoLocal.exists() ) System.out.println( "Using the following for your local repository: " + localRepository );
{
if ( !mavenRepoLocal.mkdirs() )
{
System.err.println( "Cannot create the specified local repository: " + mavenRepoLocal );
System.exit( 1 );
}
}
if ( !mavenRepoLocal.canWrite() )
{
System.err.println( "Can't write to " + mavenRepoLocal.getAbsolutePath() );
System.exit( 1 );
}
System.out.println( "Using the following for your local repository: " + mavenRepoLocal );
System.out.println( "Using the following for your remote repositories: " + remoteRepos ); System.out.println( "Using the following for your remote repositories: " + remoteRepos );
} }
public File getMavenRepoLocal()
{
return mavenRepoLocal;
}
private Set downloadedArtifacts = new HashSet(); private Set downloadedArtifacts = new HashSet();
public void setProxy( String host, String port, String userName, String password ) public void setProxy( String host, String port, String userName, String password )
@ -90,8 +69,7 @@ public class ArtifactDownloader
if ( !downloadedArtifacts.contains( dep.getId() ) ) if ( !downloadedArtifacts.contains( dep.getId() ) )
{ {
String repositoryPath = dep.getRepositoryPath(); File destinationFile = localRepository.getArtifactFile( dep );
File destinationFile = new File( mavenRepoLocal, repositoryPath );
// The directory structure for this project may // The directory structure for this project may
// not exists so create it if missing. // not exists so create it if missing.
File directory = destinationFile.getParentFile(); File directory = destinationFile.getParentFile();
@ -106,7 +84,7 @@ public class ArtifactDownloader
continue; continue;
} }
getRemoteArtifact( repositoryPath, destinationFile ); getRemoteArtifact( dep, destinationFile );
if ( !destinationFile.exists() ) if ( !destinationFile.exists() )
{ {
@ -129,26 +107,22 @@ public class ArtifactDownloader
if ( repositories.isEmpty() ) if ( repositories.isEmpty() )
{ {
remoteRepos.add( "http://repo1.maven.org" ); // TODO: configure layout
remoteRepos.add( new Repository( "http://repo1.maven.org", Repository.LAYOUT_LEGACY ) );
} }
} }
private List getRemoteRepos() private boolean getRemoteArtifact( Dependency dep, File destinationFile )
{
return remoteRepos;
}
private boolean getRemoteArtifact( String file, File destinationFile )
{ {
boolean fileFound = false; boolean fileFound = false;
for ( Iterator i = getRemoteRepos().iterator(); i.hasNext(); ) for ( Iterator i = remoteRepos.iterator(); i.hasNext(); )
{ {
String remoteRepo = (String) i.next(); Repository remoteRepo = (Repository) i.next();
// The username and password parameters are not being // The username and password parameters are not being
// used here. Those are the "" parameters you see below. // used here. Those are the "" parameters you see below.
String url = remoteRepo + "/" + file; String url = remoteRepo.getArtifactPath( dep );
if ( !url.startsWith( "file" ) ) if ( !url.startsWith( "file" ) )
{ {

View File

@ -43,11 +43,12 @@ public class Dependency
{ {
} }
public Dependency( String groupId, String artifactId, String version ) public Dependency( String groupId, String artifactId, String version, String type )
{ {
this.version = version; this.version = version;
this.artifactId = artifactId; this.artifactId = artifactId;
this.groupId = groupId; this.groupId = groupId;
this.type = type;
} }
public void setId( String id ) public void setId( String id )
@ -193,14 +194,4 @@ public class Dependency
return false; return false;
} }
public String getRepositoryPath()
{
return getArtifactDirectory() + "/" + getType() + "s/" + getArtifact();
}
public String toString()
{
return getRepositoryPath();
}
} }

View File

@ -16,12 +16,10 @@ package model;
* limitations under the License. * limitations under the License.
*/ */
import download.ArtifactDownloader;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import util.AbstractReader; import util.AbstractReader;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -51,7 +49,7 @@ public class ModelReader
private List dependencies = new ArrayList(); private List dependencies = new ArrayList();
private List remoteRepositories = new ArrayList(); private List repositories = new ArrayList();
private List resources = new ArrayList(); private List resources = new ArrayList();
@ -71,16 +69,18 @@ public class ModelReader
private StringBuffer bodyText = new StringBuffer(); private StringBuffer bodyText = new StringBuffer();
private final ArtifactDownloader downloader; private final Repository localRepository;
public ModelReader( ArtifactDownloader downloader ) private Repository currentRepository;
public ModelReader( Repository downloader )
{ {
this.downloader = downloader; this.localRepository = downloader;
} }
public List getRemoteRepositories() public List getRemoteRepositories()
{ {
return remoteRepositories; return repositories;
} }
public List getDependencies() public List getDependencies()
@ -101,6 +101,8 @@ public class ModelReader
} }
else if ( rawName.equals( "repository" ) ) else if ( rawName.equals( "repository" ) )
{ {
currentRepository = new Repository();
insideRepository = true; insideRepository = true;
} }
else if ( rawName.equals( "dependency" ) ) else if ( rawName.equals( "dependency" ) )
@ -140,8 +142,6 @@ public class ModelReader
// support both v3 <extend> and v4 <parent> // support both v3 <extend> and v4 <parent>
if ( rawName.equals( "parent" ) ) if ( rawName.equals( "parent" ) )
{ {
File f;
if ( parentArtifactId == null || parentArtifactId.trim().length() == 0 ) if ( parentArtifactId == null || parentArtifactId.trim().length() == 0 )
{ {
throw new SAXException( "Missing required element in <parent>: artifactId." ); throw new SAXException( "Missing required element in <parent>: artifactId." );
@ -167,12 +167,9 @@ public class ModelReader
version = parentVersion; version = parentVersion;
} }
f = new File( downloader.getMavenRepoLocal(), parentGroupId + "/poms/" + parentArtifactId + "-" + ModelReader p = new ModelReader( localRepository );
parentVersion + ".pom" );
ModelReader p = new ModelReader( downloader ); if ( !p.parse( localRepository.getArtifactFile( parentGroupId, parentArtifactId, parentVersion, "pom" ) ) )
if ( !p.parse( f ) )
{ {
throw new SAXException( "Could not parse parent pom.xml" ); throw new SAXException( "Could not parse parent pom.xml" );
} }
@ -201,6 +198,12 @@ public class ModelReader
insideResource = false; insideResource = false;
} }
else if ( rawName.equals( "repository" ) )
{
repositories.add( currentRepository );
insideRepository = false;
}
else if ( insideParent ) else if ( insideParent )
{ {
if ( rawName.equals( "groupId" ) ) if ( rawName.equals( "groupId" ) )
@ -262,6 +265,17 @@ public class ModelReader
currentResource.addExclude( getBodyText() ); currentResource.addExclude( getBodyText() );
} }
} }
else if ( insideRepository )
{
if ( rawName.equals( "url" ) )
{
currentRepository.setBasedir( getBodyText() );
}
else if ( rawName.equals( "layout" ) )
{
currentRepository.setLayout( getBodyText() );
}
}
else if ( depth == 2 ) else if ( depth == 2 )
{ {
if ( rawName.equals( "artifactId" ) ) if ( rawName.equals( "artifactId" ) )
@ -280,17 +294,6 @@ public class ModelReader
{ {
packaging = getBodyText(); packaging = getBodyText();
} }
else if ( rawName.equals( "repository" ) )
{
insideRepository = false;
}
}
else if ( insideRepository )
{
if ( rawName.equals( "url" ) )
{
remoteRepositories.add( getBodyText() );
}
} }
bodyText = new StringBuffer(); bodyText = new StringBuffer();

View File

@ -0,0 +1,112 @@
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.File;
/**
* Repository path management.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class Repository
{
private String basedir;
public static final String LAYOUT_LEGACY = "legacy";
private String layout = LAYOUT_LEGACY;
public Repository()
{
}
public Repository( String basedir, String layout )
{
this.basedir = basedir;
this.layout = layout;
}
public File getArtifactFile( String groupId, String artifactId, String version, String type )
{
Dependency d = new Dependency( groupId, artifactId, version, type );
return getArtifactFile( d );
}
public File getArtifactFile( Dependency dependency )
{
String repositoryPath = getArtifactPath( dependency );
return new File( basedir, repositoryPath );
}
public String getArtifactPath( Dependency dependency )
{
String repositoryPath;
if ( LAYOUT_LEGACY.equals( layout ) )
{
repositoryPath = dependency.getArtifactDirectory() + "/" + dependency.getType() + "s/" +
dependency.getArtifact();
}
else
{
throw new IllegalStateException( "Unknown layout: " + layout );
}
return repositoryPath;
}
public File getMetadataFile( String groupId, String artifactId, String version, String type, String filename )
{
Dependency d = new Dependency( groupId, artifactId, version, type );
String repositoryPath;
if ( LAYOUT_LEGACY.equals( layout ) )
{
repositoryPath = d.getArtifactDirectory() + "/poms/" + filename;
}
else
{
throw new IllegalStateException( "Unknown layout: " + layout );
}
return new File( basedir, repositoryPath );
}
public String toString()
{
return basedir;
}
public String getBasedir()
{
return basedir;
}
public void setBasedir( String basedir )
{
this.basedir = basedir;
}
public void setLayout( String layout )
{
this.layout = layout;
}
}