Index: main/java/org/apache/maven/plugin/DefaultMavenExpressions.java =================================================================== --- main/java/org/apache/maven/plugin/DefaultMavenExpressions.java (revision 0) +++ main/java/org/apache/maven/plugin/DefaultMavenExpressions.java (revision 0) @@ -0,0 +1,257 @@ +/* Copyright 2006 Stellent, Inc. All rights reserved. + * Stellent PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ + +package org.apache.maven.plugin; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; + +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.logging.Logger; + +/** + * A class that adds a default set of expressions to maven. The following list + * of expressions are currently supported: + * + * + * + * TODO: Possible future enhancement/support: + *
  • ${mvn.scm.revision} - Current revision of the project. I'm not sure of + * the feasibility of doing this for any SCM other than Subversion and Bazaar. + * Subversion and Bazaar + * have the concept of "global revisions" while CVS doesn't. We need to + * investigate if ClearCase, Perforce, and StarTeam support concept similar to + * global revision number and how to integrate that with SCM.
  • + * + * @author Sharmarke Aden (saden) + * + * @author $Author$ + * @version $Revision$ + */ +public class DefaultMavenExpressions { + + /** + * String Constants. + */ + private static final String BUILD = "build"; + + private static final String DATE = "date"; + + private static final String VERSION = "version"; + + private static final String POM_PROPERTIES = "pom.properties"; + + private static final String MVN_POM_DATE = "mvn.pom.date"; + + private static final String MVN_POM_BUILD = "mvn.pom.build"; + + /** + * Fields. + */ + + private Properties properties; + + private SimpleDateFormat dateFormat; + + private InputStream resourceAsStream; + + private MavenProject project; + + private Properties pomProperties; + + private Logger logger; + + /** + * Default constructor. + * + * @param project + * project model. + * @param logger + */ + public DefaultMavenExpressions(MavenProject project, Logger logger) { + this.properties = new Properties(); + this.dateFormat = new SimpleDateFormat(); + this.project = project; + this.logger = logger; + } + + /** + * Evaluate the given default expression and return it's value. Note that + * this method requires that the "mvn." prefix be striped from the + * expression beforehand. + * + * @param expression + * the expression that will be evaluated. + * @return the value of the expression. + */ + public String getExpressionValue(String expression) { + String value = ""; + int index = expression.lastIndexOf("."); + String expSubstr = expression.substring(index + 1); + + if (expression.startsWith("timestamp")) { + + if (index < 0) { + logger.info("using default date format 'yyyyMMddHHmmss'"); + dateFormat.applyPattern("yyyyMMddHHmmss"); + } else { + logger.info("using custom date format '" + expSubstr + "'"); + dateFormat.applyPattern(expSubstr); + } + value = dateFormat.format(new Date()); + + } else if (expression.startsWith(DefaultMavenExpressions.BUILD)) { + + expSubstr = value = getBuildValue(expSubstr); + + } else if (expression.startsWith("pom")) { + value = getPomValue(expSubstr); + + } + return value; + } + + /** + * Get POM related information such as the build number and build date. + * + * @param expSubstr + * the sub-expression whose value is to be determined. + * @return the value of the pom sub-expression. + * + */ + private String getPomValue(String expSubstr) { + String value = ""; + Properties pomProperties = getPomProperties(); + if (DefaultMavenExpressions.BUILD.equals(expSubstr)) { + value = System.getProperty(DefaultMavenExpressions.MVN_POM_BUILD); + if (value == null) { + value = pomProperties + .getProperty(DefaultMavenExpressions.MVN_POM_BUILD); + } + } else if (DefaultMavenExpressions.DATE.equals(expSubstr)) { + value = System.getProperty(DefaultMavenExpressions.MVN_POM_DATE); + if (value == null) { + value = pomProperties + .getProperty(DefaultMavenExpressions.MVN_POM_DATE); + } + } else if ("dir".equals(expSubstr)) { + value = project.getBasedir().getName(); + } + return value; + } + + /** + * Load the pom.properties file if it exists otherwise create a new + * properties file and add the default values of "1" for the build number + * and today's date as the build date. + * + * @return a Properties object containing the build properties. + */ + private Properties getPomProperties() { + + if (this.pomProperties == null) { + File pomFile = new File(this.project.getBasedir() + File.separator + + DefaultMavenExpressions.POM_PROPERTIES); + FileInputStream fis = null; + FileOutputStream fos = null; + this.pomProperties = new Properties(); + try { + if (!pomFile.exists()) { + logger.warn("pom.properties file doesn't exist. Creating '" + + pomFile.getAbsolutePath() + "' "); + + logger.info("using default build number of '1'"); + this.pomProperties.setProperty( + DefaultMavenExpressions.MVN_POM_BUILD, "1"); + this.dateFormat.applyPattern("yyyyMMddHHmmss"); + logger.info("using today's day as the build date."); + this.pomProperties.setProperty( + DefaultMavenExpressions.MVN_POM_DATE, dateFormat + .format(new Date())); + fos = new FileOutputStream(pomFile); + this.pomProperties.store(fos, + "Default Maven POM Build Properties."); + fos.flush(); + fos.close(); + } else { + logger.info("loading '" + pomFile.getAbsolutePath() + "'"); + fis = new FileInputStream(pomFile); + this.pomProperties.load(fis); + fis.close(); + } + } catch (IOException e) { + System.err + .println("Unable load or save the project pom.properties file." + + e.getMessage()); + } + } + return this.pomProperties; + } + + /** + * Get maven build related information. + * + * @param expSubstr + * the sub-expression whose value is to be determined. + * @return the value of the build sub-expression. + */ + private String getBuildValue(String expSubstr) { + if (resourceAsStream == null) { + resourceAsStream = DefaultMavenExpressions.class + .getClassLoader() + .getResourceAsStream( + "META-INF/maven/org.apache.maven/maven-core/pom.properties"); + try { + properties.load(resourceAsStream); + } catch (IOException e) { + System.err + .println("Unable determine version from maven-core JAR file: " + + e.getMessage()); + } + } + + String value = ""; + if (DefaultMavenExpressions.VERSION.equals(expSubstr)) { + value = properties.getProperty(DefaultMavenExpressions.VERSION); + } else if (DefaultMavenExpressions.DATE.equals(expSubstr)) { + value = properties.getProperty("builtOn"); + } + return value; + } +} \ No newline at end of file Property changes on: main\java\org\apache\maven\plugin\DefaultMavenExpressions.java ___________________________________________________________________ Name: svn:keywords + Author Rev Id Date Name: svn:eol-style + native Index: main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java =================================================================== --- main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java (revision 406788) +++ main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java (working copy) @@ -70,6 +70,8 @@ private final String basedir; private final Properties properties; + + private DefaultMavenExpressions defaultExpression; public PluginParameterExpressionEvaluator( MavenSession context, MojoExecution mojoExecution, @@ -84,7 +86,8 @@ this.logger = logger; this.project = project; this.properties = properties; - + this.defaultExpression = new DefaultMavenExpressions(project, logger); + String basedir = null; if ( project != null ) @@ -165,8 +168,12 @@ mojoDescriptor.getGoal() + "\' has been deprecated. Use \'" + DEPRECATED_EXPRESSIONS.get( expression ) + "\' instead." ); } - - if ( "localRepository".equals( expression ) ) + if(expression.startsWith("mvn.")) { + + int index = expression.indexOf("."); + value = defaultExpression.getExpressionValue(expression.substring(index+ 1)); + + } else if ( "localRepository".equals( expression ) ) { value = context.getLocalRepository(); }