如何使用MAVEN运行方法? [英] How to run method with MAVEN?

查看:129
本文介绍了如何使用MAVEN运行方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个控制台应用程序(具有主要功能的.java类),我希望将其称为主程序 (或者,例如,在执行BIG项目之前使用一些静态方法)如何使用MAVEN(脚本?)执行此操作?

I have wrote a console app (.java class with main function), and I d'like to call this main (or, for example some static method before execution of my BIG project) how to do this with MAVEN (script?) ?

推荐答案

去看看 exec maven插件.您可以在构建的任何时候使用它来执行本机可执行文件,脚本或Java main().像这样:

go and look at the exec maven plugin. you can use it to execute either a native executable, a script, or a java main() at any point in the build. like so:

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
       <mainClass>com.example.Main</mainClass>
    </configuration>
  </plugin>
</plugins>
</build>

您不能只运行所需的任何Java方法,但是使用main()方法编写一个小型类来调用所需的任何东西都是很容易的.

you cannot run just any java method you want, but writing a small class with a main() method that invokes whatever you want is pretty easy.

另一个难题是,要运行的类必须在运行之前进行编译.这意味着此执行必须在编译阶段之后进行,或者您需要将项目拆分为2个模块-一个包含主类,另一个包含您的其余代码(这将取决于第一个模块以进行正确的构建)订单)

another gotcha is that the class you want to run has to be compiled before you run it. this means that either this execution has to happen after the compile phase or you need to split your project into 2 modules - one containing the main class and the other containing the rest of you code (that will depend on the 1st module to get proper build order)

如果您仍然坚持使用任意代码,则总是有 groovy maven插件,可让您编写在构建过程中内联执行的Groovy代码.

if you still insist on arbitrary code, there's always the groovy maven plugin, which allows you to write groovy code inline to be executed during the build.

这篇关于如何使用MAVEN运行方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆