You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by di...@apache.org on 2003/09/01 02:34:40 UTC
cvs commit: maven/src/java/org/apache/maven MavenUtils.java
dion 2003/08/31 17:34:40
Modified: src/java/org/apache/maven MavenUtils.java
Log:
Change POM interpolation from compiling as a script to evaluating as an expression.
This saves around 40M on bootstrapping.
Checkstyle fixes.
Revision Changes Path
1.100 +72 -38 maven/src/java/org/apache/maven/MavenUtils.java
Index: MavenUtils.java
===================================================================
RCS file: /home/cvs/maven/src/java/org/apache/maven/MavenUtils.java,v
retrieving revision 1.99
retrieving revision 1.100
diff -u -r1.99 -r1.100
--- MavenUtils.java 19 Aug 2003 04:31:38 -0000 1.99
+++ MavenUtils.java 1 Sep 2003 00:34:40 -0000 1.100
@@ -65,8 +65,6 @@
import org.apache.commons.digester.ExtendedBaseRules;
import org.apache.commons.digester.Rule;
import org.apache.commons.jelly.JellyContext;
-import org.apache.commons.jelly.Script;
-import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.jelly.expression.CompositeExpression;
import org.apache.commons.jelly.expression.Expression;
import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
@@ -80,18 +78,16 @@
import org.apache.maven.project.Project;
import org.apache.maven.util.StringInputStream;
import org.apache.tools.ant.DirectoryScanner;
+import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import javax.xml.parsers.SAXParserFactory;
-import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStreamWriter;
import java.io.StringReader;
-import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -140,7 +136,7 @@
private static HashMap parentPoms = new HashMap();
/**
- * Create a Project object given a name a file descriptor.
+ * Create a Project object given a file descriptor.
*
* @param projectDescriptor a maven project.xml
* @return the Maven project object for the given project descriptor
@@ -152,6 +148,13 @@
return getProject( projectDescriptor, null );
}
+ /**
+ * Create a Project object given a file descriptor, and a parent context
+ * @param projectDescriptor The file to create the project from
+ * @param parentContext the parent Maven Jelly Context
+ * @return a new Project
+ * @throws Exception when any error happens. FIXME
+ */
public static Project getProject( File projectDescriptor, MavenJellyContext parentContext )
throws Exception
{
@@ -182,6 +185,8 @@
* the project itself.
*
* @param projectDescriptor a maven project.xml {@link File}
+ * @param parentContext the parent context for the new project
+ * @param useParentPom whether a parent project should be respected
* @return the MavenSession project object for the given project descriptor
* @throws Exception when any errors occur
*/
@@ -211,6 +216,7 @@
Expression e = JellyUtils.decomposeExpression( pomToExtend, mavenExpressionFactory, context );
pomToExtend = e.evaluateAsString( context );
pomToExtend = MavenUtils.makeAbsolutePath( projectDescriptor.getParentFile(), pomToExtend );
+ project.setExtend( pomToExtend );
File parentPom = new File( pomToExtend );
@@ -255,6 +261,7 @@
*
* @param directory the directory to scan for maven projects
* @param includes the pattern that matches a project
+ * @param context the parent context
* @return a {link List} of {@link Project}s
* @throws Exception when anything goes wrong. FIXME this is bad
*/
@@ -270,6 +277,7 @@
* @param directory the directory to scan for maven projects
* @param includes Patterns to include.
* @param excludes Patterns to exclude.
+ * @param context the parent context
* @return a {link List} of {@link Project}s
* @throws Exception when anything goes wrong. FIXME this is bad
*/
@@ -335,22 +343,8 @@
// 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, INTERNAL_ENCODING ) );
- XMLOutput output = XMLOutput.createXMLOutput( writer );
- script.run( context, output );
- writer.close();
-
- // 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 = getInterpolatedPOM(project, context);
+
project.setContext( originalContext );
project.getContext().setProject( project );
@@ -358,6 +352,41 @@
}
/**
+ * Get the POM with all variables resolved.
+ * @param project the project to resolve
+ * @param context the context to retrieve variables from
+ * @return a project with no unresolved elements.
+ * @throws Exception when there is an error getting the project as a string
+ * @throws IOException if there is an error parsing the project
+ * @throws SAXException if there is a sax error parsing the project
+ */
+ private static Project getInterpolatedPOM(Project project, JellyContext context)
+ throws Exception, IOException, SAXException
+ {
+ String projectString = getProjectString(project);
+ Expression e = JellyUtils.decomposeExpression( projectString, mavenExpressionFactory, context );
+ String newProjectString = e.evaluateAsString( context );
+ project = (Project) getProjectBeanReader().parse( new StringReader( newProjectString ) );
+ return project;
+// 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, INTERNAL_ENCODING ) );
+// XMLOutput output = XMLOutput.createXMLOutput( writer );
+// script.run( context, output );
+// writer.close();
+//
+// // 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() ) );
+ }
+
+ /**
* @return an {@link InputStream} for the given project
* @param project a {@link Project maven project}
* @throws Exception when anything goes wrong. FIXME this is bad
@@ -403,7 +432,7 @@
BeanMap parentBeanMap = new BeanMap( parent );
BeanMap childBeanMap = new BeanMap( child );
- for ( Iterator i = parentBeanMap.keySet().iterator(); i.hasNext(); )
+ for ( Iterator i = parentBeanMap.keySet().iterator(); i.hasNext();)
{
// Take the property for the parent and insert it
// into the child bean map.
@@ -429,6 +458,7 @@
catch ( IllegalArgumentException e )
{
// There is no write method for this property.
+ logger.debug("No write method for property", e);
}
}
return child;
@@ -463,13 +493,10 @@
{
return true;
}
- else if ( o == null )
- {
- return true;
- }
else
{
- return false;
+ // it needs populating if it's null
+ return (o == null);
}
}
@@ -666,7 +693,7 @@
// Now take the keys we just found and extract the values from
// the recessiveMap and put the key:value pairs into the dominantMap.
- for ( Iterator i = contributingRecessiveKeys.iterator(); i.hasNext(); )
+ for ( Iterator i = contributingRecessiveKeys.iterator(); i.hasNext();)
{
Object key = i.next();
result.put( key, recessiveMap.get( key ) );
@@ -787,12 +814,12 @@
{
context = new MavenJellyContext( parentContext );
context.setInherit( false );
-
+
MavenUtils.integrateMapInContext( result, context );
-
+
// Turn inheritance back on to make the parent's values visible.
context.setInherit( true );
-
+
//add in the driver.properties with defaults, but in inheritance mode.
MavenUtils.integrateMapInContext( defaultProperties, context );
}
@@ -805,7 +832,7 @@
//integrate defaults...
MavenUtils.integrateMapInContext( defaultProperties, context );
-
+
// Turn inheritance back on to make the parent's values visible.
context.setInherit( true );
}
@@ -834,7 +861,7 @@
JexlExpressionFactory factory = new JexlExpressionFactory();
- for ( Iterator i = map.keySet().iterator(); i.hasNext(); )
+ for ( Iterator i = map.keySet().iterator(); i.hasNext();)
{
String key = (String) i.next();
Object value;
@@ -861,6 +888,7 @@
catch ( Exception e )
{
// do nothing.
+ logger.debug("Unexpected error evaluating expression", e);
}
}
}
@@ -876,11 +904,12 @@
{
try
{
- return loadProperties( new FileInputStream( file ) );
+ return loadProperties( new FileInputStream (file) );
}
catch ( Exception e )
{
// ignore
+ logger.debug("Unexpected error loading properties", e);
}
return null;
@@ -903,6 +932,7 @@
catch ( IOException e )
{
// ignore
+ logger.debug("Unexpected exception loading properties", e);
}
finally
{
@@ -916,6 +946,7 @@
catch ( IOException e )
{
// ignore
+ logger.debug("Unexpected exception loading properties", e);
}
}
@@ -959,7 +990,8 @@
/**
* Finish off this element.
- *
+ * @param namespace the namespace of the element
+ * @param elementName the name of the element
* @throws Exception when any errors occur
*/
public void end(String namespace, String elementName)
@@ -1037,10 +1069,11 @@
* @param basedir the base directory for relative paths
* @param dir the directory to resolve
* @throws IOException if canonical path fails
+ * @return the canonical path of the directory if not absolute
*/
public static String makeAbsolutePath( File basedir, String dir ) throws IOException
{
- if ( new File( dir ).isAbsolute() == false )
+ if ( !(new File( dir ).isAbsolute()) )
{
return new File( basedir, dir ).getCanonicalPath();
}
@@ -1051,6 +1084,7 @@
* Convert an absolute path to a relative path if it is under a given base directory.
* @param basedir the base directory for relative paths
* @param path the directory to resolve
+ * @return the relative path
* @throws IOException if canonical path fails
*/
public static String makeRelativePath( File basedir, String path ) throws IOException
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org