如何编写Maven构建脚本来执行Java

Gan*_*row 4 java eclipse maven-2

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

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main
Run Code Online (Sandbox Code Playgroud)

编辑

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

这会构建项目,但它不会执行我的类:

<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>
Run Code Online (Sandbox Code Playgroud)

cet*_*nar 5

用你的pom配置maven-exec-plugin

<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>
Run Code Online (Sandbox Code Playgroud)

<phase>yourPhase</phase>插入maven 阶段的插件应该运行的插件.

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