tags:

views:

530

answers:

3

What is the common usage of using ant to execute a java class or method.

I have multiple methods within on class that I need to call if ant run or restore are run. Both run and restore are methods of the same java class but, I cannot seem to get ant run to execute Class.beginExecution() and ant restore to execute Class.beginRestore()..

Thanks

+3  A: 

Are you looking for the Java task? You need to give it a fully qualified name of a class with a main method, just as if you ran it from command line.

Konrad Garus
Does it have to be the main of a class? Can I not execute a specific method? Or do I do that by passing arguments into main to call the correct method?
tathamr
For that you could write your own task. See http://ant.apache.org/manual/develop.html
Konrad Garus
A: 

You need to write regular java class with main method and run it with the following ant task:

<target name="run_main" depends="dist" description="--> runs main method of class YourMainClass">
    <java classname="test.com.YourMainClass"
          failonerror="true"
          fork="true">
        <sysproperty key="DEBUG" value="true"/>
        <arg value="${basedir}/"/>
        <classpath>
            <pathelement location="all.project.class.path"/>
        </classpath>
    </java>
</target>
Tal
+2  A: 

You have a few options:

  1. Create a main method on this class which takes a parameter indicating the correct method, calls the main method and uses the ant java task.
  2. Create a couple of dummy classes that have a main method that calls the correct method on your class and use the above.
  3. Write your own ant task that either calls this class, or just have this class extend the ANT task class (that will work if it doesn't need to extend anything else).
  4. @sunnyjava's solution is very clever, to use the ant junit task to call JUnit tests that call your class. I don't see a huge advantage over #2 above, but it will gain you that if you use JUnit 4+ you can just annotate the methods you need run with the @Test. The downside is there would be no way to distinguish between your before and and after within one class.
Yishai