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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:52:16  来源:igfitidea点击:

Ant common usage calling java methods

javaant

提问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

Are you looking for the Javatask? 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.

您在寻找Java任务吗?你需要给它一个带有 main 方法的类的完全限定名,就像你从命令行运行它一样。

回答by Yishai

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 javatask.
  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 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).
  4. @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.
  1. 在这个类上创建一个 main 方法,它接受一个指示正确方法的参数,调用 main 方法并使用 Ant java任务。
  2. 创建几个虚拟类,它们有一个 main 方法,该方法在您的类上调用正确的方法并使用上述方法。
  3. 编写您自己的Ant 任务来调用这个类,或者只是让这个类扩展 Ant 任务类(如果它不需要扩展其他任何东西,这将起作用)。
  4. @sunnyjava 的解决方案非常聪明,使用 Ant junit任务来调用调用您的类的 JUnit 测试。我没有看到上面#2 的巨大优势,但是如果你使用 JUnit 4+,你可以只注释你需要使用 @Test 运行的方法。缺点是没有办法区分你在一个班级内的前后。

回答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>