java Ant调用java方法的常见用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2379811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ant common usage calling java methods
提问by XanderLynn
What is the common usage of using ant to execute a java class or method.
使用ant执行java类或方法的常见用法是什么。
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()..
我在类中有多个方法,如果运行 ant run 或 restore ,我需要调用这些方法。run 和 restore 都是同一个 java 类的方法,但是,我似乎无法让 ant run 执行 Class.beginExecution() 和 ant restore 来执行 Class.beginRestore()。
Thanks
谢谢
回答by Konrad Garus
回答by Yishai
You have a few options:
您有几个选择:
- Create a main method on this class which takes a parameter indicating the correct method, calls the main method and uses the Ant javatask.
- Create a couple of dummy classes that have a main method that calls the correct method on your class and use the above.
- Write your own Ant taskthat 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).
- @sunnyjava's solution is very clever, to use the Ant junittask 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.
回答by Tal
You need to write regular java class with main method and run it with the following ant task:
您需要使用 main 方法编写常规 java 类并使用以下 ant 任务运行它:
<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>

