dion 2002/06/13 21:59:57
Modified: src/java/org/apache/maven/build PropertyResolver.java
src/java/org/apache/maven Build.java
src/test/org/apache/maven BuildTest.java
src/test/org/apache/maven/build PropertyResolverTest.java
Log:
Added resolution from project object
Revision Changes Path
1.7 +5 -5
jakarta-turbine-maven/src/java/org/apache/maven/build/PropertyResolver.java
Index: PropertyResolver.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/build/PropertyResolver.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- PropertyResolver.java 12 Jun 2002 23:30:21 -0000 1.6
+++ PropertyResolver.java 14 Jun 2002 04:59:56 -0000 1.7
@@ -60,7 +60,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.util.Iterator;
+import java.util.Enumeration;
import java.util.Properties;
import org.apache.commons.logging.Log;
@@ -115,11 +115,11 @@
{
String key = null;
log.debug("Adding: " + properties);
- for(Iterator i = properties.keySet().iterator(); i.hasNext();)
+ for (Enumeration e = properties.propertyNames(); e.hasMoreElements();)
{
- key = (String)i.next();
+ key = (String)e.nextElement();
log.debug("setting key: " + key);
- setProperty(key, properties.get(key));
+ setProperty(key, properties.getProperty(key));
}
}
1.6 +143 -5 jakarta-turbine-maven/src/java/org/apache/maven/Build.java
Index: Build.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/Build.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Build.java 11 Jun 2002 02:20:29 -0000 1.5
+++ Build.java 14 Jun 2002 04:59:56 -0000 1.6
@@ -56,13 +56,19 @@
* ====================================================================
*/
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.UnrecognizedOptionException;
+import org.apache.commons.logging.Log;
import org.apache.maven.project.Project;
+import org.apache.maven.build.PropertyResolver;
/**
* The 'driver' for maven, in java form.
@@ -103,8 +109,15 @@
private String projectDescriptorFileName;
/** the maven project */
private Project project;
-
- /** Creates a new instance of Build */
+ /** used to handle properties */
+ private PropertyResolver resolver = new PropertyResolver();
+ /** log output */
+ private static final Log log = org.apache.commons.logging.LogFactory.getLog(
+ Build.class);
+
+ /**
+ * Creates a new instance of Build
+ */
public Build()
{
}
@@ -165,12 +178,85 @@
*/
public void run()
{
+ // load properties: system, user, build, project, maven defaults
+ loadProperties();
// verify project is ok first
verifyProject();
// resolve action to run
// execute action(s)
}
+ /**
+ * <p>Load the properties available to maven. Note, this may become more
+ * parameterised as time goes by.</p>
+ *
+ * <p>Currently the order of precedence is as follows:
+ * <ol>
+ * <li>project.xml properties</li>
+ * <li>{@link System#getProperties System properties}</li>
+ * <li>${user.home}/build.properties - user's defaults</li>
+ * <li>${basedir}/build.properties - this build's defaults</li>
+ * <li>${basedir}/project.properties - project defaults</li>
+ * <li>${maven.home}/plugins/core/default.properties - maven defaults</li>
+ * </ol>
+ * </p>
+ */
+ public void loadProperties()
+ {
+ // add user defaults and system properties to pick up user.home and
+ // maven.home and basedir?
+ String userDefaults = System.getProperty("user.home") + "/build.properties";
+ loadOptionalPropertiesFromFile(userDefaults);
+ resolver.addProperties(System.getProperties());
+ // load properties in least significant first, so that overrides work
+ String mavenDefaults = getProperty("maven.home") +
+ "/plugins/core/default.properties";
+ loadOptionalPropertiesFromFile(mavenDefaults);
+ String projectDefaults = getBasedir() + "/project.properties";
+ loadOptionalPropertiesFromFile(projectDefaults);
+ String buildDefaults = getBasedir() + "/build.properties";
+ loadOptionalPropertiesFromFile(buildDefaults);
+ // reload defaults in correct place
+ userDefaults = getProperty("user.home") + "/build.properties";
+ loadOptionalPropertiesFromFile(userDefaults);
+ resolver.addProperties(System.getProperties());
+ // add maven project
+ setProperty("maven", project);
+ }
+
+
+ /**
+ * Load properties from a file that may not exist
+ *
+ * @param fileName the name of the file to load properties from
+ */
+ private void loadOptionalPropertiesFromFile(String fileName)
+ {
+ log.debug("Loading properties from :'" + fileName + "'");
+ File propertiesFile = new File(fileName);
+ if (propertiesFile.exists() && propertiesFile.canRead())
+ {
+ try
+ {
+ resolver.addPropertiesFromFile(fileName);
+ }
+ catch (FileNotFoundException fnfe)
+ {
+ log.error("File not found, even though isExists and canRead",
+ fnfe);
+ }
+ catch (IOException ioe)
+ {
+ log.error("Exception loading properties", ioe);
+ }
+ }
+ else
+ {
+ log.info("File: '" + fileName + "' is not readable or doesn't " +
+ "exist");
+ }
+ }
+
/**
* <p>Verify the project as done in init:
* <ol>
@@ -179,11 +265,17 @@
* </ol>
* </p>
*/
- private void verifyProject()
+ public void verifyProject()
{
if (!project.isPomCurrent())
{
-
+
+ // check from->to dvsl exists, where from == project.pomVersion
+ // and to == MavenConstants.POM_VERSION
+ // dvsl file == ${maven.home}/update/
+ // v${maven.fromVersion}-v${maven.toVersion}/
+ // update-descriptor.dvsl
+ // update descriptor using dvsl if possible and reload
}
}
@@ -222,4 +314,50 @@
project = MavenUtils.getProject(projectDescriptorFileName);
}
+ /**
+ * Retrieve the 'base directory' for the build, shamelessly stolen from
+ * an ant concept :)
+ *
+ * @return the "basedir" {@link System#getProperties() system property},
+ * if set, or the "user.dir" system property otherwise
+ */
+ public String getBasedir()
+ {
+ String basedir = getProperty("basedir");
+ if (basedir.equals("basedir"))
+ {
+ basedir = getProperty("user.dir");
+ if (basedir.equals("user.dir"))
+ {
+ basedir = ".";
+ }
+ setProperty("basedir", basedir);
+ }
+
+ return basedir;
+ }
+
+ /**
+ * Set a project property. This can be a simple string value, a java object,
+ * or an expression based on other properties.
+ *
+ * @param name the property name
+ * @param value the value of the property.
+ */
+ public void setProperty(String name, Object value)
+ {
+ resolver.setProperty(name, value);
+ }
+
+ /**
+ * Retrieve a property value, evaluating it, if it's an expression.
+ * Note, in contrast to ant, property resolution to a value is done at get
+ * time rather than at set.
+ *
+ * @param name the name of the property to retrieve
+ */
+ public String getProperty(String name)
+ {
+ return resolver.getProperty(name);
+ }
}
1.4 +50 -5 jakarta-turbine-maven/src/test/org/apache/maven/BuildTest.java
Index: BuildTest.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/BuildTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BuildTest.java 10 Jun 2002 16:44:34 -0000 1.3
+++ BuildTest.java 14 Jun 2002 04:59:56 -0000 1.4
@@ -71,7 +71,9 @@
/** test project 1's filename */
String project1;
- /** Creates a new instance of the test case
+ /**
+ * Creates a new instance of the test case
+ *
* @param testName the name of the test
*/
public BuildTest(java.lang.String testName)
@@ -81,6 +83,7 @@
/**
* Initialize per test data
+ *
* @throws Exception when there is an unexpected problem
*/
public void setUp() throws Exception
@@ -95,13 +98,19 @@
}
- /** test that the instance got created */
+ /**
+ * test that the instance got created
+ */
public void testConstructor()
{
assertNotNull("instance not available", instance);
}
- /** test that configuration works for pom short param name */
+ /**
+ * test that configuration works for pom short param name
+ *
+ * @throws Exception if any unexpected error occurs
+ */
public void testConfigureProjectDescriptorShort() throws Exception
{
instance.configure(new String[] {"-p", project1});
@@ -109,12 +118,48 @@
instance.getProjectDescriptorFileName(), project1);
}
- /** test that configuration works for pom with long args */
+ /**
+ * test that configuration works for pom with long args
+ *
+ * @throws Exception if any unexpected error occurs
+ */
public void testConfigureProjectDescriptorLong() throws Exception
{
instance.configure(new String[] {"--project", project1});
assertEquals("--project option not parsed",
instance.getProjectDescriptorFileName(), project1);
}
+
+ /**
+ * Test the properties
+ */
+ public void testProperties()
+ {
+ instance.loadProperties();
+ assertTrue(!instance.getProperty("lib.repo").equals("lib.repo"));
+ assertTrue(!instance.getProperty("maven.home").equals("maven.home"));
+ }
+
+ /**
+ * Test the basedir property
+ */
+ public void testBasedir()
+ {
+ instance.loadProperties();
+ assertEquals("basedir should be the system property value",
+ System.getProperty("basedir"), instance.getBasedir());
+ }
+ /**
+ * Test that the maven project is loaded and accessible via properties
+ *
+ * @throws Exception when any unexpected error occurs
+ */
+ public void testProject() throws Exception
+ {
+ testConfigureProjectDescriptorShort();
+ testProperties();
+ assertEquals("maven descriptor not loaded correctly", "1.0-b5-dev",
+ instance.getProperty("maven.currentVersion"));
+ }
}
1.6 +3 -3
jakarta-turbine-maven/src/test/org/apache/maven/build/PropertyResolverTest.java
Index: PropertyResolverTest.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/build/PropertyResolverTest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- PropertyResolverTest.java 12 Jun 2002 23:30:39 -0000 1.5
+++ PropertyResolverTest.java 14 Jun 2002 04:59:56 -0000 1.6
@@ -140,11 +140,11 @@
// method call
assertEquals("string length was wrong", length,
- instance.getProperty("${" + name+".length()}"));
+ instance.getProperty("" + name+".length()"));
// nested properties
assertEquals("string class name is wrong", "java.lang.String",
- instance.getProperty("${" + name + ".class.name}"));
+ instance.getProperty(name + ".class.name"));
// expression using previously defined variable
instance.setProperty("test.length", "${" + name + ".length()}");
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>