简体   繁体   中英

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 (ie 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 (ie from command line)
  4. 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

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

参考截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM