简体   繁体   English

创建可执行 Jar,其中包含所有依赖项作为打包的 jar

[英]Create Executable Jar that includes all the dependencies as packaged jars

I'm trying to understand how to include all of the dependencies in an executable jar file as packaged jars (ie include jars within a jar) but I'm failing miserably.我试图了解如何将所有依赖项作为打包的 jar 包含在可执行 jar 文件中(即在 jar 中包含 jars),但我失败得很惨。

Could someone point me in the right direction please?有人可以指出我正确的方向吗?

Note that I wan't the resultant fat jar to have jars packaged within it not the un-packaged equivalent that you get using the maven-assembly-plugin请注意,我不希望生成的 fat jar 将 jars 打包在其中,而不是使用 maven-assembly-plugin 获得的未打包的等价物

What you want to create is called a fat jar .你想要创建的东西叫做fat jar You can generate it by doing next steps.您可以通过执行后续步骤来生成它。

Add Build section in your POM file as follows:在您的 POM 文件中添加构建部分,如下所示:

<build>
    <finalName>example</finalName>
    <plugins>

        <!-- Set a compiler level -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>

        <!-- Maven Assembly Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- MainClass in mainfest make a executable jar -->
                <archive>
                  <manifest>
                    <mainClass>com.example.Main</mainClass>
                  </manifest>
                </archive>

            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <!-- bind to the packaging phase -->
                <phase>package</phase> 
                <goals>
                    <goal>single</goal>
                </goals>
              </execution>
            </executions>
        </plugin>

    </plugins>
</build>

Above “Maven Assembly Plugin” is bind to the Maven’s packaging phase , to produces the final Jar, just package it using:上面的“Maven Assembly Plugin”绑定到Maven的打包阶段,生成最终的Jar,只需使用以下方式打包:

mvn package

Two jar files will be created in the target folder.将在目标文件夹中创建两个 jar 文件。

  • example.jar – Only your project classes example.jar – 只有你的项目类

  • example-jar-with-dependencies.jar – Project and dependency classes in a single jar. example-jar-with-dependencies.jar – 单个 jar 中的项目和依赖类。

You can now check the contents of your fat jar with:你现在可以检查你的 fat jar 的内容:

jar tf target/example-jar-with-dependencies.jar 

Include dependencies in the pom.xml.在 pom.xml 中包含依赖项。 Build the project using mvn clean install and the result will have these dependencies.使用mvn clean install构建项目,结果将具有这些依赖项。 You can then run the jar using然后您可以使用运行罐子

java -cp <jar Filename> <main class name>

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

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