XanderLynn
XanderLynn

Reputation: 883

Ant common usage calling java methods

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

Upvotes: 2

Views: 12804

Answers (3)

Konrad Garus
Konrad Garus

Reputation: 54015

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.

Upvotes: 5

Yishai
Yishai

Reputation: 91881

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.

Upvotes: 2

Tal
Tal

Reputation: 253

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>

Upvotes: 0

Related Questions