使用 Maven 生成一个可运行的 jar 并在其中包含库

Posted

技术标签:

【中文标题】使用 Maven 生成一个可运行的 jar 并在其中包含库【英文标题】:Generate a runnable jar and include libraries in it with Maven 【发布时间】:2014-12-19 00:55:47 【问题描述】:

我正在尝试使用 Maven 和 Eclipse 编译一个 Java 项目,但我尝试了很多在网上看到的解决方案,但似乎都没有。我只想构建应用程序,创建一个可运行的 jar 并包含所需的库。我尝试了 maven-dependency 或 maven-assembly 但我肯定会错过一些东西,因为我每次都失败了。

这是我的 pom.xml,可以吗?还是漏掉了什么?

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- Project Description -->
    <groupId>org.awax</groupId>
    <artifactId>toolbox</artifactId>
    <version>0.1.0</version>
    <name>ToolBox</name>
    <packaging>jar</packaging>
    <url>http://maven.apache.org</url>
    <description>Custom library containing useful code</description>

    <!-- Project Properties -->
    <properties>
        <jdk.version>1.7</jdk.version>
        <!-- Libraries Version -->
        <log4j.version>1.2.17</log4j.version>
        <jdom.version>2.0.5</jdom.version>
        <miglayout.version>3.7.4</miglayout.version>
        <jfreechart.version>1.0.19</jfreechart.version>
        <bounce.version>0.18</bounce.version>
        <!-- Maven Plugins Version -->
        <eclipse.version>2.9</eclipse.version>
        <compiler.version>3.2</compiler.version>
        <jar.version>2.5</jar.version>
        <assembly.version>2.4.1</assembly.version>
        <dependency.version>2.5.1</dependency.version>
    </properties>

    <!-- Project Libraries -->
    <dependencies>
        <!-- LOG4J -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>$log4j.version</version>
        </dependency>
        <!-- Jdom -->
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>$jdom.version</version>
        </dependency>
        <!-- MigLayout -->
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>$miglayout.version</version>
        </dependency>
        <!-- JFreeChart -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>$jfreechart.version</version>
        </dependency>
        <!-- Bounce -->
        <dependency>
            <groupId>org.bounce</groupId>
            <artifactId>bounce</artifactId>
            <version>$bounce.version</version>
        </dependency>
    </dependencies>

    <!-- Build Options -->
    <build>
        <pluginManagement>
            <plugins>
                <!-- Attach source code and Javadoc -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>$eclipse.version</version>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>false</downloadJavadocs>
                    </configuration>
                </plugin>
                <!-- Set a JDK compiler level -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>$compiler.version</version>
                    <configuration>
                        <source>$jdk.version</source>
                        <target>$jdk.version</target>
                    </configuration>
                </plugin>
                <!-- Copy project dependencies -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>$dependency.version</version>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeScope>provided</includeScope>
                                <outputDirectory>$project.build.directory/dependency</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- Create the Jar -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>$jar.version</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <!-- Jar file entry point -->
                                <mainClass>$project.groupId.$project.artifactId.tools.WaveformGenerator</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <!-- Add dependencies to generated Jar -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>$assembly.version</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

【问题讨论】:

删除标签 pluginManagemen 并保留其余部分...除此之外,请检查您的构建命令,而不是在 Eclipse 中。 【参考方案1】:

使用maven-assembly-plugin 生成带有依赖关系的可执行jar

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <groupId>org.apache.maven.plugins</groupId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <id>make-executable-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.coderplus.sample.MainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

它不适合你,因为你已经在 pluginManagement 部分添加了插件执行。 pluginManagement 应该用于管理插件版本和配置。

构建标签的最小版本:

<build>
<!-- if you have a multimodule project, I will probably manage this in the
    parent pom -->
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>$assembly.version</version>
      </plugin>
    </plugins>
  </pluginManagement>

  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <groupId>org.apache.maven.plugins</groupId>
      <executions>
        <execution>
          <id>make-executable-jar-with-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <archive>
              <manifest>
               <addClasspath>true</addClasspath>
               <mainClass>com.coderplus.sample.MainClass</mainClass>
             </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
      </execution>
    </executions>
  </plugin>
  </plugins>
</build>

【讨论】:

是的,它有效!谢谢 !但是我得到了两个 jar,一个 toolbox-0.1.0 和 toolbox-0.1.0-jar-with-dependencies,为什么? 这就是它应该做的:生成一个包含依赖项的可执行 jar 和一个常规 jar。如果您不需要常规的,则将 appendAssemblyId 设置为 false。 maven.apache.org/plugins/maven-assembly-plugin/…

以上是关于使用 Maven 生成一个可运行的 jar 并在其中包含库的主要内容,如果未能解决你的问题,请参考以下文章

Maven学习Maven打包生成普通jar包可运行jar包包含所有依赖的jar包

Spring Boot Maven插件

maven工程编译并生成可执行JAR包命令

Maven项目生成 jar直接运行

图文介绍MyEclipse (2015) 中创建简单的Maven项目的步骤(用于生成可运行jar文件)

图文介绍MyEclipse (2015) 中创建简单的Maven项目的步骤(用于生成可运行jar文件)