[Maven’s third-party dependencies are packaged together]

Using the Maven Assembly plugin

If you want to package Maven's third-party dependencies together, you can use the jar-with-dependencies descriptor of the Maven Assembly plugin. This can be achieved by adding the following configuration in the POM file:

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

In the above configuration, the descriptorRef part specifies the descriptor type to be used as jar-with-dependencies, which will package all dependencies.
After executing the Maven packaging command (mvn clean package), you will find an executable JAR file containing all dependencies in the target directory.
Note that using the jar-with-dependencies descriptor will result in the final JAR file being very large because it contains all the dependencies. If you just want to package the application itself without dependencies, consider using other descriptors or customizing it with the Maven Shade plugin.

Using the Maven Shade plugin

When you need to customize the Maven packaging process, you can use the Maven Shade plug-in. The following is a case of customization using the Maven Shade plug-in:

  1. Introduce the Maven Shade plug-in and its configuration into the POM file:
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.example.App</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

In the above configuration, we set the version of the Maven Shade plug-in to 3.2.4 and customized an execution configuration with the execution stage as package. In this configuration, we use the ManifestResourceTransformer converter to specify the main class.
2. Execute the Maven packaging command:

mvn clean package

After the execution is completed, you will find a customized executable JAR file in the target directory. The JAR file retains the main class information and transforms the resources as needed.
By using the Maven Shade plug-in, you can configure the packaging process more flexibly and customized according to your personal needs.

Customize using the Maven Shade plugin

When customizing using the Maven Shade plugin, you can configure it to package third-party dependencies in your project so that the generated executable JAR file includes these dependencies. The following is a case of using the Maven Shade plug-in to package third-party dependencies:
3. Introduce the Maven Shade plug-in and its configuration in the POM file:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <artifactSet>
              <includes>
                <include>*:*</include>
              </includes>
            </artifactSet>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

In the above configuration, we introduced the Maven Shade plug-in and set its version to 3.2.4.
In the configuration section of the plug-in, we define an execution configuration whose execution phase is package, which means that the configuration will take effect when the Maven packaging command is executed.
In this configuration, we use <artifactSet> elements to configure the third-party dependencies to be packaged. Specifying the *: * element means that all dependencies will be packaged.
2. Execute the Maven packaging command:

mvn clean package

After the execution is completed, you will find a customized executable JAR file in the target directory. The JAR file will contain the project's compiled output and all third-party dependencies.

By using the Maven Shade plug-in and configuring the appropriate artifactSet, you can package third-party dependencies into the generated executable JAR file for easy deployment and running. This eliminates the need for external dependencies, making the executable JAR file more self-contained and easier to use.

Guess you like

Origin blog.csdn.net/weixin_43866250/article/details/132759990