开发者

How to write a Maven build script to execute Java

开发者 https://www.devze.com 2022-12-20 01:18 出处:网络
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?

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

EDIT

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>
                    <execu开发者_开发百科tion>
                        <phase>deploy</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>org.eclipse.content.MyClass</mainClass>
                </configuration>
            </plugin>


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>

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

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.


Try the exec maven plugin... Oops, I should've read more carefully :-(

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号