Forums Register Login

invoking java task from ant

+Pie Number of slices to send: Send
Hi all. I have searched google and searched the forums here but I just cannot seem to get this working. I do my development on Linux (in Eclipse) but everyone else at my work uses IntelliJ on Windows. There is interest in moving everyone to Eclipse -- even the guys running Windows. Unfortunately, our Java code isn't in the normal structure that Eclipse likes (src/com/companyname/package/Foo.java). To get around this on Linux I just wrote a shell script that figures out the package name in each Java file by recursively traversing the Java directory, and then creates symbolic links in the appropriate place. I tried running this script in Cygwin on Windows but it took like 10 minutes to run for some reason, which is unacceptable.

Thus, today I wrote up a little Java program that uses the FileUtils.copyFile() method of the Commons IO project, and it copies all of the files to the appropriate structure -- works beautifully in Windows. Now in Eclipse on Windows I can connect up the debugger to a remote Tomcat environment and Eclipse will let me properly step through the source code (since everything is copied under the src directory as desired).

With that out of the way, I would like to further automate the Windows build. Essentially I would like the ant task to invoke a Java target, essentially calling this Java class that I just wrote after a successful build. So the logic is something like this:


- ant compile task runs
- if compilation is successful, then invoke this external Java program I wrote

I don't think I need to fire up a separate JVM, so it should be relatively simple. I have been trying to use this tutorial but I just can't seem to get things working properly:

http://www.java-tips.org/other-api-tips/ant/how-to-use-java-task.html


Let's just suppose for the sake of argument that the bytecode is at C:\build-tools\CopyFiles.class and that commons-io-2.0.1.jar is in this same directory. To invoke the class manually I would just do this:




I am just using the default package in CopyFiles.java (e.g. I don't define any package statement in there). How exactly do I invoke my Java class upon a successful ant build? It seems like it should be something like this:




Is that right? If so, where does it go -- inside the <target name="compile" ...> tag, or outside of it? I'm confused...any help is appreciated. If we can get this working that would be great. Maybe in the future I can convince them to change the code in SVN to reflect the package structure that Eclipse likes...
+Pie Number of slices to send: Send
A classname is just that - the name of a class, not of a class file. So the value should be "CopyFiles", and then the classpath element needs to include it.

It would go inside of an existing target - at the spot where it should execute, just like any other task.

Another option would be to refactor the Java class into an Ant task - it's not particularly hard. See https://coderanch.com/how-to/java/AntTask for an example.
+Pie Number of slices to send: Send
 

Ulf Dittmer wrote:A classname is just that - the name of a class, not of a class file. So the value should be "CopyFiles", and then the classpath element needs to include it.



Thanks for the quick response! I'm totally with you on the first part -- that's an easy change. I don't understand the second half of your sentence though. I have never really used ant before so talk to me like a 5 year old. How does one include the class in the classpath element? Can you give me a concrete example? I'm guessing something like this???

+Pie Number of slices to send: Send
Yes, I think that will work, but in case there is more than one class (or the class has inner classes), it's better to list the directory where the class(es) resides. So it would be <pathelement location="..." /> like in the first element.
+Pie Number of slices to send: Send
Awesome...it works! The only thing I had to change was to fully qualify the path to the commons-io-2.0.1.jar file. Otherwise it wasn't able to locate the FileUtils class so the Java code was failing. As soon as I changed that it worked perfectly. So whenever I set this up on someone's machine they'll just have to modify to whatever path they have this stuff in but that's simple enough. Many thanks!

Poop goes in a willow feeder. Wipe with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1609 times.
Similar Threads
Problem with Eclipse and the servicegen task of weblogic
Want to run multiple Test Cases by Ant ?
java.lang.VerifyError:
Anyone ever seen this before?
java.lang.VerifyError
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:16:42.