Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 841
gMale Avatar asked Jan 11 '11 17:01

gMale


People also ask

How do you set an Ant property?

The <property> task is used to set the Ant properties. The property value is immutable, once the value is set you cannot change it. To set a property to a specific value you use Name/value assignment. To set a property to a location you use Name/location assignment.

How do I override property value in Ant?

Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it. This way: Anything set at the command line takes precedence over build.

Where are Ant properties set?

The home directory for Ant library files - typically ANT_HOME/lib folder. Ant also makes the system properties (Example: file. separator) available to the build file. In addition to the above, the user can define additional properties using the property element.

How do I run Ant target in Eclipse?

Tip. You also can start an Ant build by highlighting the project in the Package Explorer and selecting Run→ External Tools→ Run As→ Ant Build. If you want to select the targets to run, you also can select Run→ External Tools→ External Tools→ Targets, select the targets you want to run, and click Run.


2 Answers

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}" />

reference screenshot

like image 159
deepak Avatar answered Sep 20 '22 07:09

deepak


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.

like image 31
Konstantin Komissarchik Avatar answered Sep 20 '22 07:09

Konstantin Komissarchik