• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

invoking java task from ant

 
Ranch Hand
Posts: 47
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
B Mayes
Ranch Hand
Posts: 47
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic