Initial revision

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163280 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2004-12-29 06:20:51 +00:00
parent 153d46aa2f
commit df4ea9d0d3
27 changed files with 996 additions and 0 deletions

View File

@ -0,0 +1,9 @@
target
*~
*.ipr
*.iws
*.iml
*.log
.classpath
.project

View File

@ -0,0 +1,28 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>maven</groupId>
<artifactId>maven-archetype-core</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.0-alpha-2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-velocity</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>wagon-http-lightweight</artifactId>
<version>1.0-alpha-2-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1 @@
maven.log

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2004 Your Corporation. All Rights Reserved.
*/
package org.apache.maven.archetype;
import org.apache.maven.artifact.repository.ArtifactRepository;
import java.util.Map;
import java.util.Set;
import java.io.File;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public interface Archetype
{
String ROLE = Archetype.class.getName();
String ARCHETYPE_DESCRIPTOR = "META-INF/archetype.xml";
String ARCHETYPE_RESOURCES = "archetype-resources";
String ARCHETYPE_POM = "pom.xml";
void createArchetype( String archetypeId, ArtifactRepository localRepository, Set remoteRepositories, Map parameters )
throws ArchetypeNotFoundException, ArchetypeDescriptorException, ArchetypeTemplateProcessingException;
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2004 Your Corporation. All Rights Reserved.
*/
package org.apache.maven.archetype;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class ArchetypeDescriptorException
extends Exception
{
public ArchetypeDescriptorException( String message )
{
super( message );
}
public ArchetypeDescriptorException( Throwable cause )
{
super( cause );
}
public ArchetypeDescriptorException( String message, Throwable cause )
{
super( message, cause );
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2004 Your Corporation. All Rights Reserved.
*/
package org.apache.maven.archetype;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class ArchetypeNotFoundException
extends Exception
{
public ArchetypeNotFoundException( String message )
{
super( message );
}
public ArchetypeNotFoundException( Throwable cause )
{
super( cause );
}
public ArchetypeNotFoundException( String message, Throwable cause )
{
super( message, cause );
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2004 Your Corporation. All Rights Reserved.
*/
package org.apache.maven.archetype;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class ArchetypeTemplateProcessingException
extends Exception
{
public ArchetypeTemplateProcessingException( String message )
{
super( message );
}
public ArchetypeTemplateProcessingException( Throwable cause )
{
super( cause );
}
public ArchetypeTemplateProcessingException( String message, Throwable cause )
{
super( message, cause );
}
}

View File

@ -0,0 +1,199 @@
package org.apache.maven.archetype;
import org.apache.maven.archetype.descriptor.ArchetypeDescriptor;
import org.apache.maven.archetype.descriptor.ArchetypeDescriptorBuilder;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.velocity.VelocityComponent;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.net.URLClassLoader;
import java.net.URL;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class DefaultArchetype
implements Archetype
{
// ----------------------------------------------------------------------
// Components
// ----------------------------------------------------------------------
private VelocityComponent velocity;
private WagonManager wagonManager;
private ArtifactResolver artifactResolver;
// ----------------------------------------------------------------------
// Implementation
// ----------------------------------------------------------------------
// groupId = maven
// artifactId = maven-foo-archetype
// version = latest
public void createArchetype( String archetypeId, ArtifactRepository localRepository, Set remoteRepositories, Map parameters )
throws ArchetypeNotFoundException, ArchetypeDescriptorException, ArchetypeTemplateProcessingException
{
Artifact archetypeJar = wagonManager.createArtifact( "maven", "maven-archetype-" + archetypeId, "1.0-alpha-1-SNAPSHOT", "jar" );
try
{
artifactResolver.resolve( archetypeJar, remoteRepositories, localRepository );
}
catch ( Exception e )
{
throw new ArchetypeNotFoundException( "Cannot download archetype.", e );
}
String outputDirectory = (String) parameters.get( "outputDirectory" );
String packageName = (String) parameters.get( "package" );
createProjectDirectoryStructure( outputDirectory );
ArchetypeDescriptorBuilder builder = new ArchetypeDescriptorBuilder();
ArchetypeDescriptor descriptor = null;
URLClassLoader archetypeJarLoader;
try
{
URL[] urls = new URL[1];
urls[0] = archetypeJar.getFile().toURL();
archetypeJarLoader = new URLClassLoader( urls );
InputStream is = getStream( ARCHETYPE_DESCRIPTOR, archetypeJarLoader );
if ( is == null )
{
throw new ArchetypeDescriptorException( "The " + ARCHETYPE_DESCRIPTOR + " descriptor cannot be found." );
}
descriptor = (ArchetypeDescriptor) builder.build( new InputStreamReader( is ) );
}
catch ( Exception e )
{
throw new ArchetypeDescriptorException( "Error reading the " + ARCHETYPE_DESCRIPTOR + " descriptor.", e );
}
Context context = new VelocityContext();
context.put( "package", packageName );
for ( Iterator iterator = parameters.keySet().iterator(); iterator.hasNext(); )
{
String key = (String) iterator.next();
Object value = parameters.get( key );
context.put( key, value );
}
try
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( archetypeJarLoader );
processTemplate( outputDirectory, context, ARCHETYPE_POM, null );
processSources( outputDirectory, context, descriptor.getSources(), packageName );
processSources( outputDirectory, context, descriptor.getTestSources(), packageName );
Thread.currentThread().setContextClassLoader( old );
}
catch ( Exception e )
{
throw new ArchetypeTemplateProcessingException( "Error processing templates.", e );
}
}
protected void processSources( String outputDirectory, Context context, List sources, String packageName )
throws Exception
{
for ( Iterator i = sources.iterator(); i.hasNext(); )
{
String template = (String) i.next();
processTemplate( outputDirectory, context, template, packageName );
}
}
protected void processTemplate( String outputDirectory, Context context, String template, String packageName )
throws Exception
{
File f;
if ( packageName != null )
{
String path = packageName.replace( '.', '/' );
String filename = FileUtils.filename( template );
String dirname = FileUtils.dirname( template );
f = new File( new File( new File( outputDirectory, dirname ), path ), filename );
}
else
{
f = new File( outputDirectory, template );
}
if ( !f.getParentFile().exists() )
{
f.getParentFile().mkdirs();
}
Writer writer = new FileWriter( f );
template = ARCHETYPE_RESOURCES + "/" + template;
velocity.getEngine().mergeTemplate( template, context, writer );
writer.flush();
writer.close();
}
protected void createProjectDirectoryStructure( String outputDirectory )
{
FileUtils.mkdir( outputDirectory + "/src/main/java" );
FileUtils.mkdir( outputDirectory + "/src/main/resources" );
FileUtils.mkdir( outputDirectory + "/src/test/java" );
FileUtils.mkdir( outputDirectory + "/src/test/resources" );
}
private InputStream getStream( String name, ClassLoader loader )
{
if ( loader == null )
{
return Thread.currentThread().getContextClassLoader().getResourceAsStream( name );
}
return loader.getResourceAsStream( name );
}
}

View File

@ -0,0 +1,83 @@
package org.apache.maven.archetype.descriptor;
import java.util.ArrayList;
import java.util.List;
public class ArchetypeDescriptor
{
private String id;
private List sources;
private List testSources;
private List resources;
private List testResources;
public ArchetypeDescriptor()
{
sources = new ArrayList();
resources = new ArrayList();
testSources = new ArrayList();
testResources = new ArrayList();
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
public String getId()
{
return id;
}
public void setId( String id )
{
this.id = id;
}
public void addSource( String source )
{
sources.add( source );
}
public List getSources()
{
return sources;
}
public void addTestSource( String testSource )
{
testSources.add( testSource );
}
public List getTestSources()
{
return testSources;
}
public void addResource( String resource )
{
resources.add( resource );
}
public List getResources()
{
return resources;
}
public void addTestResource( String testResource )
{
testResources.add( testResource );
}
public List getTestResources()
{
return testResources;
}
}

View File

@ -0,0 +1,73 @@
package org.apache.maven.archetype.descriptor;
import java.io.Reader;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.Xpp3Dom;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class ArchetypeDescriptorBuilder
{
public ArchetypeDescriptor build( Reader reader )
throws Exception
{
ArchetypeDescriptor descriptor = new ArchetypeDescriptor();
Xpp3Dom dom = Xpp3DomBuilder.build( reader );
descriptor.setId( dom.getChild( "id" ).getValue() );
Xpp3Dom sources = dom.getChild( "sources" );
if ( sources != null )
{
Xpp3Dom[] sourceList = sources.getChildren( "source" );
for ( int i = 0; i < sourceList.length; i++ )
{
descriptor.addSource( sourceList[i].getValue() );
}
}
Xpp3Dom resources = dom.getChild( "resources" );
if ( resources != null )
{
Xpp3Dom[] resourceList = resources.getChildren( "source" );
for ( int i = 0; i < resourceList.length; i++ )
{
descriptor.addResource( resourceList[i].getValue() );
}
}
Xpp3Dom testSources = dom.getChild( "testSources" );
if ( testSources != null )
{
Xpp3Dom[] testSourceList = testSources.getChildren( "source" );
for ( int i = 0; i < testSourceList.length; i++ )
{
descriptor.addTestSource( testSourceList[i].getValue() );
}
}
Xpp3Dom testResources = dom.getChild( "sources" );
if ( testResources != null )
{
Xpp3Dom[] testResourceList = sources.getChildren( "source" );
for ( int i = 0; i < testResourceList.length; i++ )
{
descriptor.addTestResource( testResourceList[i].getValue() );
}
}
return descriptor;
}
}

View File

@ -0,0 +1,15 @@
/*
* Copyright (c) 2004 Your Corporation. All Rights Reserved.
*/
package org.apache.maven.archetype.descriptor;
/**
* Pass over the directory containing the sources of the archetype and create
* the appropriate descriptor.
*
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class ArchetypeDescriptorGenerator
{
}

View File

@ -0,0 +1,19 @@
<component-set>
<components>
<component>
<role>org.apache.maven.archetype.Archetype</role>
<implementation>org.apache.maven.archetype.DefaultArchetype</implementation>
<requirements>
<requirement>
<role>org.codehaus.plexus.velocity.VelocityComponent</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.manager.WagonManager</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.resolver.ArtifactResolver</role>
</requirement>
</requirements>
</component>
</components>
</component-set>

View File

@ -0,0 +1,60 @@
package org.apache.maven.archetype;
import org.codehaus.plexus.PlexusTestCase;
import org.apache.maven.artifact.repository.ArtifactRepository;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class ArchetypeTest
extends PlexusTestCase
{
public void testArchetype()
throws Exception
{
Archetype archetype = (Archetype) lookup( Archetype.ROLE );
Map parameters = new HashMap();
parameters.put( "name", "jason" );
parameters.put( "groupId", "maven" );
parameters.put( "artifactId", "quickstart" );
parameters.put( "version", "1.0-alpha-1-SNAPSHOT" );
parameters.put( "package", "org.apache.maven.quickstart" );
parameters.put( "outputDirectory",new File( getBasedir(), "target/archetype" ).getPath() );
// ----------------------------------------------------------------------
// This needs to be encapsulated in a maven test case.
// ----------------------------------------------------------------------
File mavenPropertiesFile = new File( System.getProperty( "user.home" ), ".m2/maven.properties" );
Properties mavenProperties = new Properties();
mavenProperties.load( new FileInputStream( mavenPropertiesFile ) );
ArtifactRepository localRepository = new ArtifactRepository( "local", "file://" + mavenProperties.getProperty( "maven.repo.local" ) );
Set remoteRepositories = new HashSet();
ArtifactRepository remoteRepository = new ArtifactRepository( "remote", "http://repo1.maven.org" );
remoteRepositories.add( remoteRepository );
archetype.createArchetype( "quickstart", localRepository, remoteRepositories, parameters);
}
}

View File

@ -0,0 +1,52 @@
package org.apache.maven.archetype.descriptor;
import junit.framework.TestCase;
import java.io.StringReader;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id$
*/
public class ArchetypeDescriptorBuilderTest
extends TestCase
{
public void testBuilder()
throws Exception
{
String xml =
"<archetype>" +
" <id>standard</id>" +
" <sources>" +
" <source>source0</source>" +
" <source>source1</source>" +
" </sources>" +
" <resources>" +
" <source>source0</source>" +
" <source>source1</source>" +
" </resources>" +
" <testSources>" +
" <source>source0</source>" +
" <source>source1</source>" +
" </testSources>" +
" <testResources>" +
" <source>source0</source>" +
" <source>source1</source>" +
" </testResources>" +
"</archetype>";
ArchetypeDescriptorBuilder builder = new ArchetypeDescriptorBuilder();
ArchetypeDescriptor descriptor = (ArchetypeDescriptor) builder.build( new StringReader( xml ) );
assertEquals( "standard", descriptor.getId() );
assertEquals( 2, descriptor.getSources().size() );
assertEquals( 2, descriptor.getResources().size() );
assertEquals( 2, descriptor.getTestSources().size() );
assertEquals( 2, descriptor.getTestResources().size() );
}
}

View File

@ -0,0 +1,27 @@
<?xml version="1.0"?>
<document>
<properties>
<title>Maven</title>
<author email="jason@zenplex.com">Jason van Zyl</author>
</properties>
<body>
<section name="Maven">
<p>
Maven is a Java project management and project comprehension tool. Maven
is based on the concept of a project object model (POM) in that all the
artifacts produced by Maven are a result of consulting a well defined
model for your project. Builds, documentation, source metrics, and source
cross-references are all controlled by your POM. Look here to see the
full list of Maven's <a href="features.html"><b>features</b></a>.
</p>
<source>
public class Foo
{
}
</source>
</section>
</body>
</document>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Maven">
<title>Maven</title>
<body>
<links>
<item name="Plexus" href="http://plexus.codehaus.org"/>
</links>
<menu name="Quick Links">
<item name="Model" href="/maven.html"/>
</menu>
</body>
</project>

View File

@ -0,0 +1,9 @@
target
*~
*.ipr
*.iws
*.iml
*.log
.classpath
.project

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>maven</groupId>
<artifactId>maven-plugin-parent</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<groupId>maven</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<name>Maven Archetype Plugin</name>
<version>1.0-SNAPSHOT</version>
<type>plugin</type>
<package>org.apache.maven</package>
<dependencies>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-archetype-core</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,102 @@
package org.apache.maven.plugin.archetype;
import org.apache.maven.archetype.Archetype;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;
import java.util.HashSet;
import java.util.Set;
/**
* @goal create
*
* @description Builds archetype containers.
*
* @parameter
* name="archetype"
* type="org.apache.maven.archetype.Archetype"
* required="true"
* validator=""
* expression="#component.org.apache.maven.archetype.Archetype"
* description=""
*
* @parameter
* name="localRepository"
* type="org.apache.maven.artifact.ArtifactRepository"
* required="true"
* validator=""
* expression="#localRepository"
* description=""
*
* @parameter
* name="groupId"
* type="String"
* required="true"
* validator=""
* expression="#groupId"
* default="maven"
* description=""
*
* @parameter
* name="artifactId"
* type="String"
* required="true"
* validator=""
* expression="#artifactId"
* default="quickstart"
* description=""
*
* @parameter
* name="version"
* type="String"
* required="true"
* validator=""
* expression="#version"
* default="1.0"
* description=""
*
* @parameter
* name="package"
* type="String"
* required="true"
* validator=""
* expression="#package"
* default="org.apache.maven.quickstart"
* description=""
*/
public class MavenArchetypePlugin
extends AbstractPlugin
{
public void execute( PluginExecutionRequest request, PluginExecutionResponse response )
throws Exception
{
// ----------------------------------------------------------------------
// archetypeId
// localRepository
// remoteRepository
// parameters
// ----------------------------------------------------------------------
// When there is no project how do we get the local repository and remote repos.
// The local repository will always be present but the remote is in the POM except
// for the super POM ...
ArtifactRepository localRepository = (ArtifactRepository) request.getParameter( "localRepository" );
Set remoteRepositories = new HashSet();
ArtifactRepository remoteRepository = new ArtifactRepository( "remote", "http://repo1.maven.org" );
remoteRepositories.add( remoteRepository );
String archetypeId = (String) request.getParameter( "archetypeId" );
Archetype archetype = (Archetype) request.getParameter( "archetype" );
request.getParameters().put( "outputDirectory", System.getProperty( "user.dir" ) );
archetype.createArchetype( "quickstart", localRepository, remoteRepositories, request.getParameters() );
}
}

View File

@ -0,0 +1,39 @@
<plexus>
<components>
<component>
<role>org.codehaus.plexus.builder.PlexusBuilder</role>
<implementation>org.codehaus.plexus.builder.DefaultPlexusBuilder</implementation>
<requirements>
<requirement>
<role>org.apache.maven.artifact.resolver.ArtifactResolver</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.collector.ArtifactCollector</role>
</requirement>
<requirement>
<role>org.apache.maven.project.MavenProjectBuilder</role>
</requirement>
<requirement>
<role>org.codehaus.plexus.velocity.VelocityComponent</role>
</requirement>
</requirements>
</component>
<component>
<role>org.codehaus.plexus.velocity.VelocityComponent</role>
<implementation>org.codehaus.plexus.velocity.DefaultVelocityComponent</implementation>
<configuration>
<properties>
<property>
<name>resource.loader</name>
<value>classpath</value>
</property>
<property>
<name>classpath.resource.loader.class</name>
<value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
</property>
</properties>
</configuration>
</component>
</components>
</plexus>

View File

@ -0,0 +1,9 @@
target
*~
*.ipr
*.iws
*.iml
*.log
.classpath
.project

View File

@ -0,0 +1,6 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>maven</groupId>
<artifactId>maven-archetype-quickstart</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</project>

View File

@ -0,0 +1,10 @@
<archetype>
<id>quickstart</id>
<sources>
<source>src/main/java/App.java</source>
</sources>
<testSources>
<source>src/test/java/AbstractTestCase.java</source>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>

View File

@ -0,0 +1,13 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
package $package;
/**
* Hello world!
*
* @author <a href="jason@zenplex.com">Jason van Zyl</a>
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@ -0,0 +1,40 @@
package $package;
import java.io.File;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Abstract base class for test cases.
*
* @author <a href="jason@zenplex.com">Jason van Zyl</a>
*/
public abstract class AbstractTestCase
extends TestCase
{
/**
* Basedir for all file I/O. Important when running tests from
* the reactor.
*/
public String basedir = System.getProperty("basedir");
/**
* Constructor.
*/
public AbstractTestCase(String testName)
{
super(testName);
}
/**
* Get test input file.
*
* @param path Path to test input file.
*/
public String getTestFile(String path)
{
return new File(basedir,path).getAbsolutePath();
}
}

View File

@ -0,0 +1,40 @@
package $package;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
*/
public class AppTest
extends AbstractTestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertEquals( "maven kicks ass", "maven kicks ass" );
}
}