是否有一种使用maven运行可执行jar的好方法?

时间:2013-03-01 00:02:26

标签: java maven build jar embedded-jetty

我有一个多模块maven项目,其中一个模块用于分发。

Project \
| moduleA \
| moduleB \
| ...
| dist-module \

该发行版包含一个我想轻松执行的可执行jar。但是,要执行它,我必须输入类似的内容:

java -jar dist-module/target/dist-module-1.0-SNAPSHOT-full-dist/myJar.jar

最好只输入:

mvn exec:java \ OR
mvn exec:exec

不幸的是,我找不到让java目标执行.jar的方法。 exec目标实际上是这样做的,但是有一个问题:jar包含一个嵌入式jetty服务器,并且由于exec的工作方式(不使用与maven相同的JVM),服务器不会终止,除非我终止进程手动,这同样令人讨厌。

有关如何使用maven执行此操作的任何建议吗?

修改
为了澄清,该命令主要用于简单的手动烟雾测试;读取日志输出并确保程序在其分发环境中正确启动。因此,它应该是一个阻塞调用,ctrl-c终止服务器和maven。

嵌入式服务器专门用于运行一些特定的应用程序,并不会自动重新加载它们。因此,不需要长时间独立于maven运行服务器。

2 个答案:

答案 0 :(得分:27)

mvn exec:java直接从目标/下面的编译器输出运行应用程序 - 无需从jar运行:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <id>default-cli</id>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>uk.co.pookey.hibernate.App</mainClass>
                    <systemProperties>
                        <systemProperty>
                            <key>derby.stream.error.file</key>
                            <value>${basedir}/target/derby.log</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </execution>
        </executions>
    </plugin>            

如果你想用mvn exec运行jar:exec:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <id>default-cli</id>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>java</executable>
                    <!-- optional -->
                    <workingDirectory>/tmp</workingDirectory>
                    <arguments>
                        <argument>-jar</argument>
                        <argument>${basedir}/target/hibernate-derby-memory-${project.version}.jar</argument>
                    </arguments>
                </configuration>                        
            </execution>
        </executions>
    </plugin>         

如果您遇到jetty终止问题,我建议您运行集成测试(或者不要先停止jetty主线程而不先终止jetty,或者只使用jetty的停止端口)!可以在后台运行JVM,让maven在测试后再次停止它。这有三个阶段:预集成测试,集成测试或集成后测试。您可以在http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing了解详情。使用jetty maven插件时,pom.xml配置可能会像:

   <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <!-- test start/stop: -->
        <executions>                
            <execution>
                <id>start-jetty</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>deploy-war</goal>
                </goals>
                <configuration>
                    <daemon>true</daemon>
                    <systemProperties>
                        <systemProperty>
                            <name>some.prop</name>
                            <value>false</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </execution>
            <execution>
                <id>stop-jetty</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>stop</goal>
                </goals>
            </execution>
        </executions>    
    </plugin>

您可以绑定maven antrun插件来执行任意命令,而不是将jetty maven插件绑定到这些阶段。

答案 1 :(得分:3)

如果你想执行东西,我会使用maven ant或gmaven(然后使用groovy ant builder)

在您进行编辑后,听起来就像您使用DropWizard之类的内容。您可能想看看它们是如何solving that problem的。

根据评论进行编辑:

Spring Boot Maven将创建可执行的战争并执行它们,你不需要Spring Boot和Spring。我在这里回答的更多细节:Can spring-boot-maven-plugin be used for non spring project?