nAnt script needs to get parent directory - nant

I am writing a nAnt script and need to get the parent directory. I have used:
<property name="perforceWS" value="${project::get-base-directory()}"/>
this returns the current directory. How can I get the property to be the parent's directory?

Assuming You mean the parent directory of the project directory (the directory where the NAnt build script is located) this is the answer:
<property
name="perforceWS"
value="${directory::get-parent-directory(project::get-base-directory())}"/>

Related

Why move task does not move symlinks?

I have a deployment script where I move a directory to another directory. In this directory I have a symlink relative to a parent folder like this:
abc -> ../dir/file
dir/file is also in the directory which gets deployed.
I move the directory with the following lines of xml:
<move todir="${deploy.dir}">
<fileset dir="${to.deploy}">
<include name="**/*" />
</fileset>
</move>
Everything gets moved but not the symlinks. The stay in the ${deploy.dir}. Why? Maybe Ant does not know how to set the new symlink (the new location)?
How can you fix this? Delete the symlink and use the symlink target to make a new one?
Symbolic links are special files. Ant is not fully capable of handling them. Here is the note.
Ant is not symbolic link aware in moves, deletes and when recursing
down a tree of directories to build up a list of files. Unexpected
things can happen.
You need a custom solution , probably using readlink. Question How do I move a relative symbolic link? and answers points to additional solutions (nothing to do with ant, though)

Retrieve a Relative Path to a Directory from another Directory in NAnt

I have a NAnt task create copies of MSBuild files. The copies are located in other directories than their originals.
The source files should remain where they are. Therefore, I'm using NAnt's <style> task to run an XSLT on the MSBuild files. It replaces the <Compile/> elements with <Compile><Link/></Compile> subtrees (attributes omitted for the sake of legibility).
I've encountered the following problem:
In the resulting MSBuild files, the Include attribute of the Compile elements should receive the relative path to the source file from the new location of the MSBuild files. As the original MSBuild files contain the relative paths to the files, what I am actually looking for is the relative path from the new MSBuild file location to the location of the original MSBuild files.
Workarounds that I know how to use, but which I'd like to avoid:
custom NAnt task
using substring to remove the mutual prefix of the directory paths (the NAnt base dir)
Is there any better way; possibly a NAnt function or an ingenious way to use several NAnt functions in conjunction to achieve this?

How to set Ant properties based on variables in Eclipse?

I have a common problem and there are probably countless ways to solve it. I'm looking for an elegant, simple solution to this typical scenario:
I have a project in Eclipse with an Ant build file (build.xml) the build file uses a property file (build.properties). In that property file, I want to set a property that points to the root directory of the eclipse project such as:
project.root = /path/to/eclipse/workspace/projectName
or preferably:
project.root = ${path.to.eclipse.workspace}/projectName
How do I do this in such a way that:
Works on different machines with different paths to the project root (i.e. in a team environment)
Allows the ant build.xml file to be executed inside eclipse
Allows the ant build.xml file to be executed outside of eclipse (i.e. from command line)
Allows the build.properties file to exist in a directory other than the project root
See Window -> Preferences -> Ant -> Runtime -> Properties to define custom ant properties that should be available to any ant script invoked from Eclipse. The simply set the same property manually when invoking script from command-line.
Your build.properties file can exist wherever you like. Use normal Ant facilities to import it into your script.
I think what I'm looking for is to add the following to the build.properties file:
project.root = ${basedir}
alternatively, I can just use the basedir property whenever project.root is needed.
I happened to be looking at the source code for ivy.properties and I saw the basedir property being used. I just tested and verified that this property works on different machines both from inside eclipse and from the command line as well as when making a call to ant from a different directory such as:
ant -f /path/to/eclipse/workspace/projectName/build.xml
When I get a minute, I will verify that this also works when importing the property file in different locations (such as inside src/main/resources/config/ivy/ivysettings.xml).
For my project archieve.
ProjectName <dir>
|_ ant <dir>
|_ ant.xml
Your case can just simply change the ant xml file, the <project default="main" basedir="../"/>
Then I can get the project root using variable of
e.g. <echo message= "Project Root: ${basedir}" />
if you need more than the trivial basedir stuff =
Ant4Eclipse - a bunch of ant tasks for access to eclipse configurations from within ant - may help you. Just use it as is or grep the code and pick the relevant parts..
You can set eclipse relative properties for your ANT Build from eclipse
Go to your ANT Builder properties and in arguments section you can set properties using -D as below
-Dworkspace="${workspace_loc}" -Dproject_dir="${project_loc}"
(here workspace_loc and project_loc are eclipse variables). These properties can be accessed in your ANT build script like regular properties, for example:
<echo message="${workspace}" />
<echo message="${project_dir}" />

nant tasks to copy zip folder from one machine to other

How do i copy a zip folder from my local machine to a server on same domain using nant script
Use the nant copy task.
<copy
file="myfile.zip"
tofile="\\servername\folder\myfile.zip"
inputencoding="latin1"
outputencoding="utf-8" />

MSTEST folder deployment question

Is there a way to preserve folder structure with MSTEST deployment?
I have a situation with some existing code where I have .config files in a subfolder (called "Configuration"). I can specify this folder using MSTEST deployment but, in it's infinite wisdom, MSTEST just copies the files from this folder to the run folder (TestResult\\Out), i.e. it does not create a subfolder called Configuration. This royally screws up the code and it fails. I don't really want to have to start using complicated pre-test scripts to create folders etc.
Any ideas gratefully received.
Matt
I think I had the same problem...
My tests used a folder called xsd and I wanted to deploy the folder to the test \OUT directory. When I did this, the files inside the xsd folder were copied to the test \OUT directory, but I wanted the folder xsd into the test \OUT directory...
To solve this I read this. (Wayback machine has an archive of this page here)
If you're using the DeploymentItem attribute, it takes a second argument for the name of the directory to copy the files into. If you use the same name as your folder it preserves everything.
To use your test case you'd do:
[DeploymentItem("Configuration", "Configuration")]
class TestClass
....
and it would work.
Yes, you can. read the article Do MSTest deployment items only work when present in the project test settings file?
It explains how to map deployment items.
In Visual Studio 2012 the output directory is the working directory which means that the DeploymentItem attribute isn't needed for the general case (where you don't have specific per-test or per-class deployment items). You can simply click Project | Show All Files and include the subfolder and files in Visual Studio with the 'Copy Always' or 'Copy if newer' attribute to your project and the files will be copied to your output directory with hierarchy intact. The same applies when running vstest.console.exe from the command line.
See here for more information about Deployment Items in Visual Studio 2012.