2

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

1
  • 1
    Have you thought about writing a JUnit Testcase and calling that from ant? Mar 4, 2010 at 14:11

3 Answers 3

5

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.

2
  • 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?
    – XanderLynn
    Mar 4, 2010 at 14:42
  • 1
    For that you could write your own task. See ant.apache.org/manual/develop.html Mar 4, 2010 at 15:36
2

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.
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.