Hi Dustin,
To include a file of property definitions use the include task.

http://nant.sourceforge.net/nightly/help/tasks/include.html

Just create a build file that contains only property definitions. Somthing like:

<project name="common build properties" >

   <!-- place global properties here -->
   <property name="foo" value = "bar"/>

</project>

This is mentioned in the FAQ at http://nant.sourceforge.net/wiki/index.php/FAQ

Unfortunately you won't be able to share this file with Ant so you may want to stick with your current solution. Correct me if I'm wrong but I think an include task was a recent addition to Ant whereas its been in NAnt from the beginning. As such we decided to use it as the standard mechanism for including property definitions rather than extending the property task as Ant does.

The Ant task sounds pretty cool. You're taking the right approach by implementing it as a script first. When you come to implement it as a task you'll proably want to inherit from ExternalProgramBase as you'll be wrapping java.exe. Feel free to post any further questions you might have here.

Ian



[EMAIL PROTECTED] wrote:



Hi everyone!  I joined this list because there are some differences between
Nant and Ant which have been causing me trouble.  I wanted to post my
solutions in the hopes that they may be useful.  If it's stuff that's
already been thought of, just tell me to shut up :)

The first problem I ran up against was running Ant from Nant.  On the
Windows platform, Ant is run through a batch file which does not
communicate the program exit code.  So the Nant build always believes that
the Ant build succeeded.  To get around this, I actually call the Java
class file like so:

       <exec
           program="${java.home}/bin/java.exe"
           workingdir="${mbpa.home}"
           commandline="-classpath ${ant.cp}
org.apache.tools.ant.launch.Launcher build"
           failonerror="true"
           />

This is obviously not a problem, however getting the classpath correct is a
pain because you have to add all the jars in the ant/lib directory since
Nant doesn't have any clue what a Java classpath is.  To achieve this, I
wrote a little script:

       <script language="C#">
           <code><![CDATA[
               public static void ScriptMain( Project project ) {
                   string ald = project.Properties["ant.home"] + "/lib";
                   string jh = project.Properties["java.home"];
                   string cp = jh + "/lib/tools.jar";
                   DirectoryInfo di = new DirectoryInfo(ald);
                   FileInfo[] files = di.GetFiles("*.jar");
                   foreach (FileInfo fi in files)
                       cp += ";" + ald + "/" + fi.Name;
                   project.Properties["ant.cp"] = cp;
               }
           ]]></code>
       </script>

It's a simple script and it has been working very well for me.  Is there a
better way to do this?  I guess what I want to achieve eventually is an Ant
task for Nant.  I imagine it would require that a Java task also be
created.  Is anybody else working on this kind of thing?  I'd appreciate
some suggestions on what it would take to start working on an Ant task.
And if you don't think this is desirable, please speak up so that I don't
waste my time.

Alright, the second thing I ran into is a big annoyance to me.  Ant has the
ability to load a properties file using the following:

<property file="build.properties" />

I could not find anything similar to this in Nant, so please tell me if I
just missed something.  I want to share a properties file between Nant and
Ant, so I needed something that would load the properties from that file.
To do this, I wrote the following script outside of a target:

   <script language="C#">
       <code><![CDATA[
           public static void ScriptMain( Project project ) {
               try {
                   string prop_file = project.Properties["prop.file"];
                   using (StreamReader sr = new StreamReader(prop_file)) {
                       string line;
                       while ((line = sr.ReadLine()) != null) {
                           Regex re = new
Regex("^\\s*([\\w_\\-\\.]+)\\s*=\\s*(.*?)\\s*$");
                           if (re.IsMatch(line)) {
                               Match m = re.Match(line);
                               string prop_name = m.Groups[1].ToString();
                               string prop_val = m.Groups[2].ToString();
                               // The following line does semantic
processing on the property before
                               // adding it to the list of properties.
                               prop_val =
project.Properties.ExpandProperties(prop_val, Location.UnknownLocation);
                               project.Properties[prop_name] = prop_val;
                           }
                       }
                   }
               }
               catch (Exception exc) {
                   throw new BuildException("Exception caught while
loading properties file:\n" + exc.Message);
               }
           }
       ]]></code>
   </script>

This is also a bit of functionality that I'd like to add to Nant.  This
code doesn't verify the property file at all, it just finds lines that look
like properties and ignores the rest.  Also, there's this line:

   prop_val = project.Properties.ExpandProperties(prop_val,
Location.UnknownLocation);

This line seems troublesome to me because if something later changes one of
those properties, the dependent properties will not change.  I did try to
mark the properties as dynamic through the PropertyDictionary, but Nant
comes back with an error explaining that MarkDynamic is not defined.  Any
suggestions on how to fix this?  And how much validation would be required
for a properties file in order to add the same properties file capabilities
that Ant has to Nant?

Thanks for your time,

Dustin



-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
nant-developers mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nant-developers




--
Ian MacLean, Developer, ActiveState, a division of Sophos
http://www.ActiveState.com




-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
nant-developers mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nant-developers

Reply via email to