简体   繁体   English

Maven:如何使用随附的外部库执行可运行的jar(uberjar)

[英]Maven: how to do runable jar (uberjar) with included external libraries

Until now i made runnable jars with Ant and there were no problems with it. 直到现在,我还是用Ant制成了可运行的罐子,并且没有问题。
However i now try to mavenize my project and i realy can't figured out how to do runable jar with this tool. 但是,现在我试图使我的项目变得混乱,我真的不知道如何使用此工具来执行可运行的jar。
I've read tons of tutorials (also here, on Stackoverflow), helps, advices and... nothing. 我已经阅读了大量的教程(也在此处,关于Stackoverflow),帮助,建议和……什么都没有。 In my case all of them don't work which probably means i don't understand some basics. 就我而言,它们都不起作用,这可能意味着我不了解一些基本知识。
I have such simple project: 我有一个简单的项目:

我的项目

This is app, witch use mysql-connector-java-5.1.24-bin.jar (placed in 'lib' dir) to connect to MySQL database. 这是一个应用程序,请使用mysql-connector-java-5.1.24-bin.jar(放在“ lib”目录中)连接到MySQL数据库。
I want to include this jar into final jar (DBPreformatter.jar). 我想将此jar包含在最终jar(DBPreformatter.jar)中。 I used assembly and shaded plugins in many configurations, but they NEVER added this jar into DBPreformatter.jar. 我在许多配置中使用了汇编和阴影插件,但他们从未将这个jar添加到DBPreformatter.jar中。

This is my pom.xml: 这是我的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>

How to solve this problem? 如何解决这个问题呢?

有一个Maven插件Apache Maven Shade Plugin ,它将为您构建一个uber jar。

Add the Maven Assembly plugin with the descriptor jar-with-dependencies : 添加带有描述符jar-with-dependenciesMaven Assembly插件

<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>

Note that this doesn't add the JAR; 注意,这不会添加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). 取而代之的是,它解压缩所有列为依赖项的JAR,并将其内容添加到生成的JAR中(因此您将在结果中看到MySQL JAR中的所有类文件,而不是MySQL JAR本身)。

EDIT There is a caveat, though: Maven ignores JARs with scope=system for many operations. 编辑 ,但是有一个警告:对于许多操作,Maven都忽略了scope=system JAR。 See also: How to include external jars in maven jar build process? 另请参阅: 如何在Maven jar构建过程中包括外部jar?

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. 如果Maven没有将JAR添加到输出中,则必须使用mvn install:file-install命令将具有此范围的所有JAR安装到本地maven $HOME/.m2/repository$HOME/.m2/repository )中。 See http://maven.apache.org/plugins/maven-install-plugin/usage.html how to do that. 请参阅http://maven.apache.org/plugins/maven-install-plugin/usage.html如何做到这一点。

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). 例如, scope=system将不再使您感到困惑(因为许多插件以特殊方式处理它们)。 Plus you need to do this only once. 另外,您只需要执行一次。 Afterwards, you can use this library in many Maven projects. 之后,您可以在许多Maven项目中使用此库。

Before installing, you should check http://search.maven.org/ to see if the dependency isn't already known to Maven. 在安装之前,您应该检查http://search.maven.org/以查看Maven是否还不了解依赖项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM