How to get error code of ant command - ant

My ant files are already present and I am not allowed to change those for various reasons. I have created a batch file and I am writing all those ant commands in that batch file just to automate the process. The code looks something like this
1. cd abc
2. ant realclean && ant
3. cd..
4. cd pqr
5. ant realclean && ant
6. cd..
However now I have to check if the build fired on line 2 is successful then only goto line 3 otherwise exit.
I googled a bit and found %errorlevel% as one option but it is not working in my case.
Any suggestions?
Thanks in advance.

How about adding another ANT file which contains the tasks to execute the existing ANT files?
That would give you error handling without doing anything, you could reuse that file on different OSs and your CI server and the syntax would be saner than batch...
You can have ANT execute, say project.xml, with ant -f project.xml
[EDIT] You don't even have to change the existing projects: Just create a new project, assign paths to the old projects to properties in the new build.xml and then you can build the old projects from the new one.
This gives you:
A sane[*] syntax that you already know (no need to learn the ugly/buggy/handicapped rules of DOS batch programming; you did know that DOS was once named QDOS "Quick and Dirty OS", yes?)
Proper error handling
A simple way to pass arguments to the sub-builds
Cross-platform support
Useful error messages when something goes wrong
[*]: Compared to DOS...

Related

Jenkins Conditional Build

I am completely new with Jenkins and have this question. So I created two projects on Jenkins. The first project will fetch from github if it notices any kind of change on my repo and will store a local copy on my machine.
The second project will start only if the first project is stable. If it is stable, the second project will first compile the all the java files (which I put in a compile.bat file), then it will run the testng.xml file which runs junit test and selenium test (run.bat file).
So what I want to do is if there is no compilation errors on compile.bat, then proceed on run.bat, but right now even though there are some errors on the java file and when the compile.bat runs, it catches those errors, Jenkins still proceeds to the run.bat file and at the end passes the build. I want the build to fail when there is any kind of errors.
Here is the link to my repo for the batch file and other files if that helps:
https://github.com/na2193/Demo
I had figured it out. You need to use conditional step(single) and there you can define what you want to do

Netbeans build.xml to execute when using Ant

I'm relatively new to using build files and Ant to compile and execute from a command line so i wanted to know how it would be possible to modify the build-impl.xml file auto generated from netbeans so that after it compiles it also executes the program.
Currently when i type just "ant" in the command window where the build.xml file is listed it compiles and etc but then says
-do-jar-copylibs:
[copylibs] Nothing to copy.
[echo] To run this application from the command line without Ant, try:
[echo] java -jar "C:\Users\Eric\Documents\Word Documents\Java Test Code\NetbeansVersion\Cops\dist\Cops.jar"
I can run that command and it will run fine, but i want the program to execute by just typing ant.
The build.xml file - Pastebin
Build-impl.xml file - Pastebin
There are a couple "tasks" available in ant that you could use to accomplish this.
You can use either of these:
Java Task,
Exec Task
Those documentation pages provide examples as well. Before you go there though, you might want to start at the basic manual to get an idea of the structure of an ant build file to determine where and how you want to add this execution step in.
It feels a little "off" to me to have your build script executing the program, but I'm sure you've got a reason, so I might suggest adding a new target that does the build steps and then also runs this command to kick off the program. That way you've still got the option of running the build without running the program.

Ant Cannot Copy Error

I'm running an ant build through Jenkins and on the stage where it is deploying to windows-share its returning the following error:
Failed to copy FILE to FILE2 due to failed to create the parent directory for FILE2 (I've taken the paths out to keep the question shorter).
I'm guessing that there might be some problem with permissions with the jenkins default user but this problem has only just started occurring, and any help would be great.
Thanks
This is a pretty old question but I thought I'd come back and complete it with a short update as to what was actually going on. Someone had changed the password for the user that was logged on to the vm that jenkins was running on, and when it was trying to create the directory to stick the files into it was running into permissions errors. Only problem was the error message wasn't very descriptive.
So in the end this was an infrastructure problem rather than anything to do with the ant script.
I take it you're doing something like this:
<copy file="${from.dir}/${from.file}"
tofile="${to.dir}/${to.file}"/>
And, you're getting an error that ${to.dir} doesn't exist.
In earlier versions of Ant, you definitely had to create the directory before doing a copy:
<mkdir dir="${to.dir}"/>
<copy file="${from.dir}/${from.file}"
tofile="${to.dir}/${to.file}"/>
I think I also noticed that later versions of Ant will create the directory for you when that directory didn't exist. I've always been in the habit of putting <mkdir/> in front of any task that creates a new file in a new directory, including things like <zip/> and <tar/>.
Here are some questions:
Do your users also use Ant to run their builds? I know that this isn't always the case. Many users use Eclipse and don't bother with Ant.
Is the version of Ant your users have vs. what your users have the same?
Do you do a clean? Jenkins can emulate a clean either by doing an update, reverting and removing unversioned element, or by simply creating a brand new working directory. If your users don't remove the destination directory where ${to.file} is being placed, it might work locally, but not on Jenkins.
Can you manually run Ant from the Jenkins working directory. If so, what results do you get? (Remember to disable this Jenkins job before doing this. You don't want Jenkins to do a build while you're experimenting in the working directory.)

Running DUnit tests from Hudson

I finally got Hudson to build my project and the corresponding test project (using the XMLTestRunner2 unit provided in the Embarcadero forum). Running the test executable manually correclty produces a "dunit-report.xml" file with the test results.
I can't get Hudson to call my executable and produce this file though.
What I did is to create a build step as a Windows batch command and just call the executable. I tried several things:
bin\Test.exe
start bin\Test.exe
start /wait bin\Test.exe
start /wait /b bin\Test.exe
I cannot get it to work. It either returns immediately with some random exit code or it does not produce the XML output file. This must be very simple but I'm a little bit frustrated by now, because I don't get it to work.
What's the right way to execute the unit tests from Hudson?

Problem with dependencies using Ant from the shell

I'm having problems building my project, using an Ant script, from the command prompt using Ant itself. It can't find a certain import for a particular Java file in my project (which has nearly 5,000 source files as it is). The import is included in a .jar package whose location I have set in the Ant file itself. (As a pathelement, along with other needed JARs that either are fine, or haven't tried to been used when the crash occurs). The crash happens with javac, with the simple message of "import etc.ect.* cannot be found at line etc" Oddly enough, I can build the project just fine from the Ant file using an IDE like Eclipse. Any ideas what could be wrong? Thanks!
Wow, the solution was completely unrelated. It was a dumb fault in the java code where the class was trying to import .* from a directory that only had folders in it. For some reason, Eclipse didn't seem to mind, but javac did!
Eclipse's root classloader contains a lot of classes, when you run ant from console there's much less.
Just tell javac task to use the required .jar, and you'll be fine.
You should post the stack trace, does it say "import required by ..."? ( I forget the exact text). Likely there's a jar that's available in your eclipse environment that is not included in your ant script. Look in the stack trace for the missing class to identify the jar that's not being included in your build.

Resources