Search code examples
eclipsevariablesantpropertiesbuild-process

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:

  1. Works on different machines with different paths to the project root (i.e. in a team environment)
  2. Allows the ant build.xml file to be executed inside eclipse
  3. Allows the ant build.xml file to be executed outside of eclipse (i.e. from command line)
  4. Allows the build.properties file to exist in a directory other than the project root

Solution

  • 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).