繁体   English   中英

如何使用 IntelliJ 制作具有所有依赖项的 jar 文件,即 Fat jar

[英]How to make jar file with all dependencies a.k.a. Fat jar with IntelliJ

我正在尝试使用我的项目中的所有依赖项制作 jar 文件。 我正在使用 IntelliJ。

我尝试了两种方法。

  1. 在 IntelliJ 右上角的 maven 窗口中,我点击了 pom.xml 下面的“clean”和“package”。
    但它不包含任何依赖项。
    我在窗口中使用 IntelliJ,所以我不能按照下面的 CLI 链接。 我在 IntelliJ 的右上角使用 GUI maven 窗口。
    如何使用 Maven 创建具有依赖项的可执行 JAR?

  2. 我点击了 Build-Build artifact - build 以下链接。 如何正确地从 IntelliJ 构建 jars? 似乎包含其他库,但由于某种原因我无法执行此文件。 所以,请让我使用 maven 窗口,而不是 build-artifact。

<?xml version="1.0" encoding="UTF-8"?>

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

  <groupId>secret.App</groupId>
  <artifactId>mt</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>mt</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-streaming-kafka-0-10_2.12</artifactId>
      <version>2.4.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.12</artifactId>
      <version>2.4.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-catalyst_2.12</artifactId>
      <version>2.4.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.12.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.12</artifactId>
      <version>2.4.3</version>
    </dependency>


    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql-kafka-0-10_2.12</artifactId>
      <version>2.4.3</version>
    </dependency>




  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.6</version>
          <configuration>
            <archive>
              <manifest>
                <mainClass>com.jungwoo.netmarble.App</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <appendAssemblyId>false</appendAssemblyId>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>

        </plugin>

        <plugin>
          <artifactId>maven-shade-plugin</artifactId>
          <version>3.2.1</version>
          <configuration>
            <filters>
              <filter>
                <artifact>*:*</artifact>

                <excludes>

                  <exclude>META-INF/*.SF</exclude>

                  <exclude>META-INF/*.DSA</exclude>

                  <exclude>META-INF/*.RSA</exclude>

                </excludes>
              </filter>
            </filters>
          </configuration>
        </plugin>




        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>


        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>

        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.jungwoo.netmarble.App</mainClass>
              </manifest>

            </archive>
          </configuration>

        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <executions>
            <execution>
              <id>copy-dependencies</id>
              <phase>package</phase>
              <goals>
                <goal>copy-dependencies</goal>
              </goals>
              <configuration>
                <outputDirectory>C:\Users\kimjungwow\Desktop\check5</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

您可以使用 Maven 程序集插件。 在你的 pom 中添加以下内容:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      [...]
</project>

然后从命令行执行:

mvn package

或从 intellij 执行生命周期目标“包”

按照此了解更多信息: http : //maven.apache.org/plugins/maven-assembly-plugin/usage.html

要让 Maven 从您的项目构建 Fat JAR,您必须在项目的 POM 文件中包含 Fat JAR 构建配置。

在 IntellijIDEA pom.xml文件中添加一个插件:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

如果您不知道它在哪里或根本不知道,请在此处放置或创建它:

    <build>
    <finalName>my-project-name</finalName>
    <plugins>

/// ADD THE PLUGIN HERE then delete this line ///

     </plugins>
     </build>

使 Maven 为您的项目构建 Fat JAR 的 Maven 命令是:

mvn clean package

当您使用前面显示的 maven-assembly-plugin 配置执行 Maven 包阶段时,Maven 将在目标目录中输出一个 Fat JAR,Maven 将其所有其他构建产品(例如已编译的类、生成的 JavaDocs 等)输出到其中。 Fat JAR 将被命名为:

my-project-name-jar-with-dependencies.jar

上述 Fat JAR 文件名的 my-project-name 部分来自本教程前面所示示例中构建 XML 元素顶部包含的 finalName XML 元素。

要构建项目,只需转到您拥有 pom.xml 的项目目录,然后运行以下命令。

mvn clean install -DskipTests

暂无
暂无

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

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