File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ant, Maven and Other Build Tools and the fly likes invoking java task from ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "invoking java task from ant" Watch "invoking java task from ant" New topic
Author

invoking java task from ant

B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

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...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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 http://www.coderanch.com/how-to/java/AntTask for an example.


Ping & DNS - my free Android networking tools app
B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

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???

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.
B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

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!

 
Don't get me started about those stupid light bulbs.
 
subject: invoking java task from ant