有没有使用maven运行可执行jar的好方法? [英] Is there a good way to use maven to run an executable jar?

查看:25
本文介绍了有没有使用maven运行可执行jar的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a multimodule maven project, and one of the modules is for distribution.

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

该发行版包含一个我想轻松执行的可执行 jar.但是,要执行它,我必须键入以下内容:

The distribution contains an executable jar that I'd like to easily execute. However, to execute it I have to type something like:

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 包含一个嵌入式码头服务器,并且由于 exec 的工作方式(不使用与 maven 相同的 JVM),除非我终止进程,否则服务器不会终止手动,这同样很麻烦.

Unfortunately, I can't find a way to get the java goal to execute a .jar. The exec goal will actually do it, but there's a catch: the jar contains an embedded jetty server, and due to the way exec works (doesn't use the same JVM as maven) the server doesn't terminate unless I kill the process manually, which is similarly bothersome.

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

Any recommendations on how to do this using maven?


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


For clarification, this command will primarily be used for simple manual smoke-testing; to read the log output and ensure the program started up properly in its distribution environment. As such, it should probably be a blocking call with ctrl-c terminating both the server and maven.

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

The embedded server is specialized to run a few specific applications, and does not automatically reload them. So, there's not a big need to keep the server running independently of maven for long periods of time.

推荐答案

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

mvn exec:java runs the app directly from the compiler output below target/ - no need to run from the 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 :

If you want to run the jar with mvn exec: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/阅读更多相关信息显示/MAVENUSER/Maven+and+Integration+Testing.使用 jetty maven 插件时,pom.xml 配置可能像:

If you have problems with jetty terminating, I'd recommend running an integration test (or just don't exit the jetty main thread without terminating jetty first, or just use jetty's stop port)! It is possible to run a JVM in the background and let maven stop it again after testing. There are three phases for this: pre-integration-test, integration-test or post-integration-test. You can read more about this at http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing . When using jetty maven plugin, the pom.xml config could like:

   <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插件绑定到这些阶段.

Instead of binding the jetty maven plugin to these phases, you could bind the maven antrun plugin to execute arbitrary commands.

这篇关于有没有使用maven运行可执行jar的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆