java - 有没有使用 maven 运行可执行 jar 的好方法?

标签 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 运行。

最佳答案

mvn exec:java 直接从 target/下面的编译器输出运行应用程序 - 无需从 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:exec 运行 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>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>

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

关于java - 有没有使用 maven 运行可执行 jar 的好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15148044/

相关文章:

Android 工作室 - Gradle - GitHub repo

git - Jenkins 多分支管道 - 在分支中配置属性?

java - 编码问题(4字节日语字符)

Java:绘制缩放对象(缓冲图像和 vector 图形)

java - Maven:运行程序的生命周期阶段?

android - Android ADT 更新后每个项目都显示错误

android - 更新Android Studio插件MultipleCompilationErrorsException启动失败后,React Native Android获取错误

java - 作为kafka消费者同时消费多个主题

java - 解析 PDF 文件时 Ubuntu 机器上的字体问题

maven - 如何使用 maven shade 构建 jar 和 war