here i have one bat file
test.bat ->
#echo off
echo this is piyush
pause
dir c:\
another file is xml(create file .xml) file for creating two text files
my question is that how can i execute xml file in bat file to creating two text file.
Createfile.xml ->
<?xml version="1.0" ?>
<project name="piyush" default="Create_XMLfile">
<target name="Create_XMLfile">
<echo file="peak1.xml" message="This is creation of the peak1"/>
<echo file="peak1.xml" message="This is creation of the peak1"/>
</target>
</project>
That looks like an ant build script.
If you have ant installed, you could try ant -buildfile Createfile.xml.
Related
How can we pass command line argument to ant target
for example I have target in build.xml defined as below
<target name="test">
<echo>Hello,</echo>
</target>
if I invoke ant as
cmd>ant -buildfile build.xml test USERNAME
It should print echo as "Hello, USERNAME"
is it possible some way?
Thanks in advance for help
ant -buildfile build.xml test USERNAME would mean run the two targets, test and USERNAME (see Ant manual).
Instead, do ant -buildfile build.xml -Dname=USERNAME test and
<target name="test">
<echo>Hello, ${name}</echo>
</target>
According to the man page of make, -n option does the following job:
Print the commands that would be executed, but do not execute them.
I am looking for an option which acts the same in Apache Ant.
Horrific, but here it is. We can hack the targets at runtime using some code inside a <script> tag*. The code in do-dry-run below sets an unless attribute on each of your targets, and then sets that property so that none of them executes. Ant still prints out the names of targets that are not executed because of an unless attribute.
*(JavaScript script tags seem to be supported in Ant 1.8+ using the Oracle, OpenJDK and IBM versions of Java.)
<?xml version="1.0" encoding="UTF-8"?>
<project default="build">
<target name="targetA"/>
<target name="targetB" depends="targetA">
<echo message="DON'T RUN ME"/>
</target>
<target name="targetC" depends="targetB"/>
<target name="build" depends="targetB"/>
<target name="dry-run">
<do-dry-run target="build"/>
</target>
<macrodef name="do-dry-run">
<attribute name="target"/>
<sequential>
<script language="javascript"><![CDATA[
var targs = project.getTargets().elements();
while( targs.hasMoreElements() ) {
var targ = targs.nextElement();
targ.setUnless( "DRY.RUN" );
}
project.setProperty( "DRY.RUN", "1" );
project.executeTarget( "#{target}" );
]]></script>
</sequential>
</macrodef>
</project>
When I run this normally, the echo happens:
$ ant
Buildfile: build.xml
targetA:
targetB:
[echo] DON'T RUN ME
build:
BUILD SUCCESSFUL
Total time: 0 seconds
But when I run dry-run, it doesn't:
$ ant dry-run
Buildfile: build.xml
dry-run:
targetA:
targetB:
build:
BUILD SUCCESSFUL
Total time: 0 seconds
Ant has no dry-run option as make or maven have. But you could run the ant file step by step it in debugging mode under eclipse.
No I belive. There is no such way by default in Ant. And many unstisfying attempts you would find on google. But I have searched once and was unsuccessful.
It would be a useful feature, but not easily implemented.
Make and ANT are architecturally quite different. ANT doesn't run external OS commands, instead, most ANT "tasks" execute within the same Java thread.
It would be possible to emulate a "dry run" as follows:
<project name="Dry run" default="step3">
<target name="step1" unless="dry.run">
<echo>1) hello world</echo>
</target>
<target name="step2" depends="step1" unless="dry.run">
<echo>2) hello world</echo>
</target>
<target name="step3" depends="step2" unless="dry.run">
<echo>3) hello world</echo>
</target>
</project>
Running ANT as follows will print the target name but won't execute the enclosed tasks:
$ ant -Ddry.run=1
Buildfile: build.xml
step1:
step2:
step3:
BUILD SUCCESSFUL
Total time: 0 seconds
Create a special target in your buildscript that does some echoing only i.e. to check whether properties, path .. are resolved correctly.
see https://stackoverflow.com/a/6724412/130683 for a similar question answered.
For checking the details of your ant installation use ant -diagnostics
I have the following ant build file which is supposed to package all class files in the bin directory into a jarfile:
<?xml version="1.0" encoding="utf-8"?>
<project name="RemoteJunitXletServer.makejar" default="makejar" basedir=".">
<target name="makejar" description="Build a jarfile based on the JunitServer project">
<jar jarfile="JunitServer.jar" includes="**.class" basedir="bin" />
</target>
</project>
Unfortunately, including "**.class" only goes two directories deep, and does not copy any files that are deeper than two directories inside of the bin folder. Do these directories HAVE to be explicitly declared? Or is there a way to tell Ant to just copy all class files inside of the bin folder regardless of location while preserving the folder structure?
Try includes="**/**.class" ...
I'm trying to use the -lib option to specify a directory containing Ant tasks. But they're not loading. For example:
$ ant -lib /path/to/libraries
Buildfile: build.xml
BUILD FAILED
/path/build.xml:3: taskdef class com.oopsconsultancy.xmltask.ant.XmlTask cannot be found
The build file contains:
<project name="test">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" />
</project>
This is failing on RHEL5, but the exact same thing works on several other operating systems that I've tried.
When I add the --execdebug flag, I get this:
exec "/usr/local/jdk1.6.0_20/bin/java" -classpath "/usr/share/java/ant.jar:/usr/share/java/ant-launcher.jar:/usr/share/java/jaxp_parser_impl.jar:/usr/share/java/xml-commons-apis.jar:/usr/local/jdk1.6.0_20/lib/tools.jar" -Dant.home="/usr/share/ant" -Dant.library.dir="/usr/share/ant/lib" org.apache.tools.ant.launch.Launcher -cp "" "-lib" "lib"
Any ideas?
Others seem to have the same problem on RHEL.
One workaround suggested here is to set your jar in the taskdef like so
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"
classpath="xmltask.jar"/>
Also see this thread which was a corrupt ant package
I need to create a WAR file that just contains static content files (gifs, jpgs, html, js, etc). I have a directory structure that contains all the files and need to create the WAR file via an ANT (1.5.1) build task. Right now I just have this in the ANT task:
<war destfile="${output.file}" webxml="WEB-INF/web.xml" basedir="${basedir}">
<fileset dir="${basedir}/sales" />
</war>
The files I want to include are in C:/basedir/sales and its subdirectories. When I try to run this task I get "A zip file cannot include itself". So clearly putting that fileset in there is not the right way to do it. I am unclear as to what I need to put in the task and in the web.xml file to specify what files to include within the archive.
I think the basedir="${basedir}" is causing you the problems. Also, I think the way you have it written will require that web.xml exist inside WEB-INF dir relative to where you run ant from.
So, try creating /WEB-INF/web.xml as follows:
<?xml version="1.0" encoding="utf-8" ?>
<web-app>
</web-app>
Then try updating /build.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project name="yourproject" basedir="." default="war" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="war" description="--> build war file">
<war destfile="./mywar.war" webxml="WEB-INF/web.xml">
<fileset dir="C:/basedir/sales" />
</war>
</target>
</project>
Then you should be able to run "ant war" from command line and it should create "mywar.war" in your current directory.