如何编写一个Maven构建脚本来执行Java [英] How to write a Maven build script to execute 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屋!