【问题标题】:Maven: Running created Jar-File with depedencies [duplicate]Maven:运行创建的具有依赖关系的 Jar-File [重复]
【发布时间】:2015-02-14 16:05:48
【问题描述】:

我写了一个小的 Java 命令行工具,我想通过它来运行(例如)

java -jar myJarFile.jar de.my.path.MainClass arg0 arg1 arg2

为了构建 jar 文件和管理我使用 Maven 的依赖关系。现在我发现,Maven 在 Jar-File 中不包含 POM-File 的依赖关系。所以,当我运行我的 jar 文件时,我得到了 ClassNotFoundExceptions.. 但是要执行我的 jar 文件,我需要类路径中的那些库。

如何使用 Maven 管理这个?

感谢您的帮助!!

【问题讨论】:

    标签: java maven


    【解决方案1】:

    把这个 maven 程序集插件放在你的 pom.xml 中:

        <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>de.my.path.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    

    【讨论】:

      【解决方案2】:

      有多种选择。

      • maven 应用程序组装插件

        这个打包所有依赖的 jars 并创建 linux/windows 脚本来运行你的应用程序。有关如何使用它的更多信息,请参阅Here

      • Maven 组装插件

        它创建包含所有依赖项的单个可执行 jar。 More 在这里。但是我发现一些第三方 jars(spring) 存在同名文件的问题。

      【讨论】:

        猜你喜欢
        • 2010-10-09
        • 1970-01-01
        • 2014-09-02
        • 1970-01-01
        • 2021-11-01
        • 1970-01-01
        相关资源
        最近更新 更多