Maven:如何使用包含的外部库做可运行的jar(uberjar)

Maven:如何使用包含的外部库做可运行的jar(uberjar)

问题描述:

直到现在我用Ant制作了可运行的罐子并且它没有任何问题。

但是我现在尝试对我的项目进行整理,我真的无法弄清楚如何用这个工具做可运行的jar 。$
我已经阅读了大量的教程(也在这里,在*上),帮助,建议和......没有。在我的情况下,所有这些都不起作用,这可能意味着我不理解一些基础知识。

我有这么简单的项目:

Until now i made runnable jars with Ant and there were no problems with it.
However i now try to mavenize my project and i realy can't figured out how to do runable jar with this tool.
I've read tons of tutorials (also here, on *), helps, advices and... nothing. In my case all of them don't work which probably means i don't understand some basics.
I have such simple project:

这是应用程序,女巫使用mysql- connector-java-5.1.24-bin.jar(放在'lib'dir中)连接到MySQL数据库。

我想把这个jar包含到最后一个jar(DBPreformatter.jar)中。
我在许多配置中使用了汇编和阴影插件,但他们从未将此jar添加到DBPreformatter.jar中。


这是我的pom.xml:

This is app, witch use mysql-connector-java-5.1.24-bin.jar (placed in 'lib' dir) to connect to MySQL database.
I want to include this jar into final jar (DBPreformatter.jar). I used assembly and shaded plugins in many configurations, but they NEVER added this jar into DBPreformatter.jar.

This is my pom.xml:

<modelVersion>4.0.0</modelVersion>
<groupId>com.icd4you</groupId>
<artifactId>DBPreformatter</artifactId>
<version>1.0.0</version>
<name>DBPreformatter</name>
<description>DB processing and cleaning tool</description>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>mysql-connector-java-5.1.24-bin</groupId>
        <artifactId>mysql-connector-java-5.1.24-bin</artifactId>
        <version>5.1.24</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/mysql-connector-java-5.1.24-bin.jar</systemPath>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <!-- WHAT SHOULD I USE HERE? -->

    </plugins>
</build>

如何解决这个问题?

添加 Maven Assembly插件使用描述符 jar-with-dependencies

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.pany.your.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

请注意,这不会添加JAR;相反,它解包所有列为依赖项的JAR,并将它们的内容添加到生成的JAR中(因此您将在结果中看到MySQL JAR中的所有类文件,而不是MySQL JAR本身)。

Note that this doesn't add the JAR; instead it unpacks all JARs which are listed as dependencies and adds their content to the resulting JAR (so you'll see all the class files from the MySQL JAR in the result instead of the MySQL JAR itself).

编辑但有一点需要注意:Maven使用 scope = system 忽略JAR对于许多操作。另请参阅:如何在maven jar build中包含外部jar进程?

EDIT There is a caveat, though: Maven ignores JARs with scope=system for many operations. See also: How to include external jars in maven jar build process?

如果Maven没有将JAR添加到输出中,那么必须将具有此范围的所有JAR安装到本地maven仓库中( $ HOME / .m2 / repository )使用 mvn install:file-install 命令。请参阅 http://maven.apache.org/plugins/maven-install -plugin / usage.html 如何做到这一点。

If Maven doesn't add the JAR to the output, then you must install all JARs with this scope into your local maven repo ($HOME/.m2/repository) using the mvn install:file-install command. See http://maven.apache.org/plugins/maven-install-plugin/usage.html how to do that.

注意:在本地仓库中安装库是首选方式;你应该考虑一下。首先, scope = system 将不再混淆你(因为许多插件以特殊方式处理它们)。另外,你只需要做一次。之后,您可以在许多Maven项目中使用此库。

Note: Installing libraries in your local repo is the preferred way; you should really consider it. For one, the scope=system will no longer confuse you (since many plugins handle them in a special way). Plus you need to do this only once. Afterwards, you can use this library in many Maven projects.

在安装之前,您应该检查 http://search.maven.org/ 查看Maven是否已经知道依赖关系。

Before installing, you should check http://search.maven.org/ to see if the dependency isn't already known to Maven.

MySQL是: http://search.maven.org/ #artifactdetails%7Cmysql%7Cmysql-connector-java%7C5.1.32%7Cjar