aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes Running .cmd file using Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Running .cmd file using "ant"" Watch "Running .cmd file using "ant"" New topic
Author

Running .cmd file using "ant"

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi all
i am using .cmd file to set certain environmental variables and using those variables inside the build file using myenv.VARIABLE_NAME.

Currently before running the ant file i am running the cmd and then taking the build. It will be easier for me if i ran the cmd file from the ant itself.


Any idea
Matt Harrah
Ranch Hand

Joined: Aug 05, 2006
Posts: 54
What you are attempting simply will not work from within Ant itself without some serious voodoo. If Ant were to invoke your CMD file, it will be creating a new process which will not be affecting the local environment in which Ant was started. As soon as the CMD file is finished, the environment variables set by the CMD file are gone and Ant will not see them. To test this for yourself, try this:

setenv.cmd


build.xml


Doesn't work.

BUT....

Ant provides a global preprocessing hook. At the beginning of ANT.BAT you will find the line

So you could make sure your HOME environment variable is set to some directory, create a batch file called antrc_pre.bat and put that in that directory, and have that batch file CALL your CMD file (make sure you use the call command or it will just execute without returning). Downside is that your CMD file would get executed anytime you run Ant on your machine, regardless of the project you're working on.

Hope this helps.

[ August 06, 2006: Message edited by: Matt Harrah ]
[ August 06, 2006: Message edited by: Matt Harrah ]
Dianne Doss
Greenhorn

Joined: Aug 18, 2006
Posts: 17
Do they need to be environment variables, or could you just set some local values within your build script (actually set within a property file, and pulled into the build xml)?

Do you need those variables to be available to the machine, or just to your file (as you mentioned referencing those variables in your script)?
Matt Harrah
Ranch Hand

Joined: Aug 05, 2006
Posts: 54
The HOME environment variable, for example, could not be set from ANT, since the very .bat file that invokes ANT reads it from the shell. These are .bat file variables, not ANT variables.

It's inconvenient, but basically the deal is that ANT scripts can read environment variables but not make changes to them. You need to do something outside of regular ANT tasks to get environment variable changes to stick like the original poster wanted.
Dianne Doss
Greenhorn

Joined: Aug 18, 2006
Posts: 17
But, if the poster is "using those variables inside the build file using myenv.VARIABLE_NAME" couldn't they just as easily create the variable that they need, and use that variable inside the build file? That is why I was asking if they truly needed to modify the environment variable - if someone outside of ant is using the variable that they are changing, as opposed to purely using that variable inside the build file.
Dianne Doss
Greenhorn

Joined: Aug 18, 2006
Posts: 17
e.g., instead of using %ANT_HOME% or %JUNIT_HOME% somewhere in their script, they could create the local property for the value they need and reference that instead...like creating a source.dir property or test.dir property?
Matt Harrah
Ranch Hand

Joined: Aug 05, 2006
Posts: 54
Sure, you could do that---

But I think the poster needs the batch file (which sets environment variables) for other things (not uncommon if you come from a C/C++ programming background). What he is trying to accomplish is to have the ANT build use the same batch file that his other process uses to set environment variables, and then read those environment variables and use them in the ANT script -- all without duplicating the settings in both the .cmd and the ant script.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Running .cmd file using "ant"