jvanzyl 2003/01/01 23:25:00
Modified: src/java/org/apache/maven MavenUtils.java
Log:
o cleaned up and tried to fully explain two of the most obscure operations in maven:
the creation of project object and the use of jelly to do interpolation.
Revision Changes Path
1.81 +105 -54 jakarta-turbine-maven/src/java/org/apache/maven/MavenUtils.java
Index: MavenUtils.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/MavenUtils.java,v
retrieving revision 1.80
retrieving revision 1.81
diff -u -r1.80 -r1.81
--- MavenUtils.java 31 Dec 2002 07:01:38 -0000 1.80
+++ MavenUtils.java 2 Jan 2003 07:25:00 -0000 1.81
@@ -77,6 +77,7 @@
import org.apache.maven.jelly.MavenJellyContext;
import org.apache.maven.project.BaseObject;
import org.apache.maven.project.Project;
+import org.apache.maven.project.Resource;
import org.apache.maven.util.StringInputStream;
import org.apache.tools.ant.DirectoryScanner;
import org.xml.sax.XMLReader;
@@ -147,7 +148,28 @@
}
/**
- * Create a Project object given a file descriptor.
+ * Create a Project object given a file descriptor and optionally a parent Jelly
+ * context. We are doing several things when creating a POM object, the phases
+ * are outlined here:
+ *
+ * 1) The project.xml file is read in using betwixt which creates for us a
+ * Project object that, at this point, has not been run through Jelly i.e.
+ * no interpolation has occured yet.
+ *
+ * 2) We check to see if the <extend> tag is being employed. If so, the parent
+ * project.xml file is read in. At this point we have a child and parent POM
+ * and the values are merged where the child's values override those of the
+ * parent.
+ *
+ *
+ * 3) The context for the project is created and set. So each project manages
its
+ * own context. See the createContext() method for the details context
creation
+ * process.
+ *
+ * 4) The POM we have at this point is then processed through Jelly.
+ *
+ * 5) The project is handed the reference to the File that was used to create
+ * the project itself.
*
* @param projectDescriptor a maven project.xml {@link File}
* @return the MavenSession project object for the given project descriptor
@@ -156,9 +178,11 @@
public static Project getProject( File projectDescriptor, MavenJellyContext
parentContext )
throws Exception
{
+ // 1)
BeanReader projectBeanReader = getProjectBeanReader();
Project project = (Project) projectBeanReader.parse( projectDescriptor );
+ // 2)
String pomToExtend = project.getExtend();
if ( pomToExtend != null )
{
@@ -166,14 +190,20 @@
project = (Project) mergeBeans( project, parent );
}
- project = getJellyProject( project );
- project.setFile( projectDescriptor );
-
+ // 3)
MavenJellyContext context = MavenUtils.createContext(
projectDescriptor.getParentFile(),
parentContext );
- context.setProject( project );
+ // Set the created context, and put the project itself in the context. This
+ // is how we get the ${pom} reference in the project.xml file to work.
project.setContext( context );
+ context.setProject( project );
+
+ // 4)
+ project = getJellyProject( project );
+
+ // 5)
+ project.setFile( projectDescriptor );
// Fully initialize the project.
project.initialize();
@@ -182,6 +212,48 @@
}
/**
+ * This is currently used for the reactor but may be generally useful.
+ *
+ * @param directory the directory to scan for maven projects
+ * @param includes the pattern that matches a project
+ * @return a {link List} of {@link Project}s
+ * @throws Exception when anything goes wrong. FIXME this is bad
+ */
+ public static List getProjects( File directory, String includes,
MavenJellyContext context )
+ throws Exception
+ {
+ return getProjects( directory, includes, null, context );
+ }
+
+ /**
+ * This is currently used for the reactor but may be generally useful.
+ *
+ * @param directory the directory to scan for maven projects
+ * @param includes Patterns to include.
+ * @param excludes Patterns to exclude.
+ * @return a {link List} of {@link Project}s
+ * @throws Exception when anything goes wrong. FIXME this is bad
+ */
+ public static List getProjects( File directory,
+ String includes,
+ String excludes,
+ MavenJellyContext context )
+ throws Exception
+ {
+ String[] files = getFiles( directory, includes, excludes );
+
+ List projects = new ArrayList();
+
+ for ( int i = 0; i < files.length; i++ )
+ {
+ Project p = getProject( new File( files[i] ), context );
+ projects.add( p );
+ }
+
+ return projects;
+ }
+
+ /**
* Create a project bean reader. We use it more than once so we don't want
* to create it more than once.
*
@@ -200,7 +272,8 @@
}
/**
- * Process the project descriptor using Jelly itself.
+ * Take the POM and interpolate the value of the project's context to create
+ * a new version of the POM with expanded context values.
*
* @param project the maven POM
* @return Jelly interpolated project.
@@ -209,24 +282,40 @@
private static Project getJellyProject( Project project )
throws Exception
{
+ // Save the original context because we null it temporarly
+ // while we funnel it through betwixt.
+ MavenJellyContext originalContext = project.getContext();
+
+ // We don't want any taglib references in the context or Jelly
+ // gets confused. All we want are the variables for interpolation. We
+ // can change this but I think we would like to avoid general Jelly
+ // idiom in the POM anyway.
JellyContext context = new JellyContext();
+ context.setVariables( originalContext.getVariables() );
- context.setVariable( MavenConstants.MAVEN_POM, project );
-
+ // We don't want the context being written out into the XML which
+ // is the interpolated POM.
+ project.setContext( null) ;
Script script = JellyUtils.compileScript( getProjectInputStream( project ),
context,
INTERNAL_ENCODING );
+ // Now run the script against the fully populated context so all the
+ // values are filled in correctly.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer writer = new BufferedWriter( new OutputStreamWriter( baos ) );
XMLOutput output = XMLOutput.createXMLOutput( writer );
script.run( context, output );
writer.close();
- BeanReader projectBeanReader = getProjectBeanReader();
+ // Read in the the project.xml contents with the interpolated values and
+ // put back the original context with all the values that have been
populated
+ // but change the project in the context to the newly interpolated version.
+ project = (Project) getProjectBeanReader().parse( new StringReader(
baos.toString() ) );
+ project.setContext( originalContext );
+ project.getContext().setProject( project );
- return (Project) projectBeanReader.parse(
- new StringReader( baos.toString() ) );
+ return project;
}
/**
@@ -259,7 +348,6 @@
// We do not care what the original encoding was originally. This
// is all completely internal. Our StringInputStream requires
// everything to be encoded in "ISO-8859-1".
-
return projectStream.toString( INTERNAL_ENCODING );
}
@@ -394,46 +482,6 @@
return files;
}
- /**
- * This is currently used for the reactor but may be generally useful.
- *
- * @param directory the directory to scan for maven projects
- * @param includes the pattern that matches a project
- * @return a {link List} of {@link Project}s
- * @throws Exception when anything goes wrong. FIXME this is bad
- */
- public static List getProjects( File directory, String includes,
MavenJellyContext context )
- throws Exception
- {
- return getProjects( directory, includes, null, context );
- }
-
- /**
- * This is currently used for the reactor but may be generally useful.
- *
- * @param directory the directory to scan for maven projects
- * @param includes Patterns to include.
- * @param excludes Patterns to exclude.
- * @return a {link List} of {@link Project}s
- * @throws Exception when anything goes wrong. FIXME this is bad
- */
- public static List getProjects( File directory,
- String includes,
- String excludes,
- MavenJellyContext context )
- throws Exception
- {
- String[] files = getFiles( directory, includes, excludes );
-
- List projects = new ArrayList();
-
- for ( int i = 0; i < files.length; i++ )
- {
- projects.add( getProject( new File( files[i] ), context ) );
- }
-
- return projects;
- }
/**
* Creates a new instance of BeanReader
@@ -704,8 +752,11 @@
MavenUtils.integrateMapInContext( result, context );
- // Turn inheritance back on to make the parent's values visible;
+ // Turn inheritance back on to make the parent's values visible.
context.setInherit( true );
+
+ // Set the basedir value in the context.
+ context.setVariable( "basedir", descriptorDirectory.getPath() );
return context;
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>