如何编写一个Maven构建脚本来执行Java [英] How to write a Maven build script to execute Java

查看:234
本文介绍了如何编写一个Maven构建脚本来执行Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在构建期间或构建完成后执行Java程序?是否可以直接从pom这样做?

How can I execute a Java program during the build, or after the build has just finished? Is it possible to do this directly from pom?

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main

编辑

假设我要执行 org.eclipse.content.MyClass 。我需要编写代码?

Let's say I want to execute org.eclipse.content.MyClass. How would I needed to write the code?

这将构建项目,但不执行我的课程:

This builds the project but it doesn't execute my class:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <phase>deploy</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>org.eclipse.content.MyClass</mainClass>
                </configuration>
            </plugin>


推荐答案

使用您的pom配置maven-exec-plugin p>

Configure maven-exec-plugin with your pom

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <phase>yourPhase</phase>
        ...
        <goals>
          <goal>java</goal>
        </goals>
           <configuration>
             <mainClass>mainClass=org.sonatype.mavenbook.weather.Main</mainClass>
          </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>

< phase> yourPhase< / phase> insert maven 阶段在什么这个插件应该运行。

In line <phase>yourPhase</phase> insert maven phase in what this plugin should run.

这个类需要在pom类路径中可用(作为源或依赖)。在其他情况下,如果不是项目依赖关系,请阅读本文如何配置exec插件。

This class need to be avaliable in pom classpath (as source or as a dependency). In other case if it shouldn't be a project dependency read this article how to configure a exec plugin.

这篇关于如何编写一个Maven构建脚本来执行Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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