是否有运行非胖 jar 的 Maven 插件? [英] Is there a Maven plugin that runs a non-fat jar?

查看:63
本文介绍了是否有运行非胖 jar 的 Maven 插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个普通的 java maven 项目,它有一个 Main 类,它生成工件 project-a.jar.此项目依赖于 project-b.jar.

Imagine a normal java maven project with a Main class that produces the artifact project-a.jar. This project has a dependency on project-b.jar.

是否有允许通过这样的命令运行该 jar 的 Maven 插件?

Is there a Maven plugin that allows to run that jar by a command like that?

mvn run-plugin:run org.mygroup:project-a:3.1 <args>

该插件将解析运行时依赖项(使用 META-INF/maven/(...)/pom.xml),将项目及其依赖项安装到本地 maven 存储库(如果尚未存在),构建类路径并调用

The plugin would resolve the runtime dependencies (using META-INF/maven/(...)/pom.xml), install the project and its dependencies to the local maven repository (if not already there), construct the classpath and invoke

java -cp (...)/project-a-3.1.jar;(...)/project-b-2.1.jar org.mygroup.Main <args>

我知道通常的方法是构建一个包含依赖项的可执行(胖)jar,但这不是我要的.

I know that the usual way is to build an executable (fat) jar that contains the dependencies, but that's not what I am asking for.

实际上,甚至不需要从 jar 中读取 pom,因为 maven 可以从给定坐标的存储库中下载它.

Actually, it is not even necesary to read the pom from the jar, because maven can download it from the repositories given the coordinates.

为什么这个问题与 Maven 运行项目问题不同:

Why this question is different to the Maven Run Project question:

我不想从已经签出项目的源代码开始.所以通常使用的 exec 插件不适用. Maven Run Project 问题的 OP 显然假设存在源代码项目文件夹.她的目的是测试,她接受了一个显然需要一个项目的答案.这两个问题的措辞也是正确的.project"和jar"这两个词之间存在差异,它们在各自上下文中的实际含义也大不相同.

I do not want to start from having the project's source already checked out. So the usual use of the exec plugin is not applicable. The OP of the Maven Run Project question obviously assumed the presence of a source code project folder. Her purpose was testing and she accepted an answer that clearly needs a project. The wording of both questions is correct, too. There is a difference between the words "project" and "jar" and their actual meaning in their respective contexts is quite different.

推荐答案

您可以使用 appassembler-maven-plugin 插件,它会为您创建一个在类路径中具有依赖项的 shell 脚本.这是一个示例配置

You can use the appassembler-maven-plugin plugin, it creates a shell script that has the dependencies in the classpath for you. Heres an example config

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extraJvmArguments>-Xms256m -Xmx1536m</extraJvmArguments>
                <programs>
                    <program>
                        <mainClass>com.package.MyMainClass</mainClass>
                        <name>TestFormattingUtils</name>
                    </program>
                </programs>
            </configuration>
        </plugin>

您可以在 .../target/appassembler/bin 中找到输出脚本您可以手动检查脚本,您会看到它执行了您想要的命令类型,并在其中添加了通过命令行将 jars 添加到类路径.即 java -jar (...)/project-a-3.1.jar -cp (...)/project-b-2.1.jar

You can find the output script in .../target/appassembler/bin You can manually inspect the script and you'll see that its doing the type of command you wanted where it adds the jars to classpath via the command line. ie java -jar (...)/project-a-3.1.jar -cp (...)/project-b-2.1.jar <args>

这篇关于是否有运行非胖 jar 的 Maven 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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