MAVEN - What is the application scenario of using maven-dependency-plugin?

brief description

maven-dependency-plugin is a plugin for MAVEN.

effect

This plug-in is mainly used to manage dependencies in the project. Using this plug-in, you can easily view, download, copy and decompress dependencies, and also support generating dependency trees and dependency reports.

Function

The plugin has many available GOALs, most of which are related to dependency building, dependency analysis, and dependency resolution. These GOALs can be directly operated with MAVEN commands.
All GOALs officially defined: https://maven.apache.org/plugins/maven-dependency-plugin/index.html

insert image description here

Common operations are as follows:
(1) mvn dependency:tree, display the dependency tree of the project;
insert image description here
(2) mvn dependency:analyze, analyze project dependencies, determine which dependencies are used and declared, used and undeclared, undeclared Use declared?
insert image description here

(3) mvn dependency:copy, copy the JAR package configured in the plugin to the specified location;
copy configuration: https://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html
(4 ) mvn dependency:copy-dependencies, copy all dependencies defined in the project POM file and their transitive dependencies to the specified location;
configuration of copy-dependencies: https://maven.apache.org/plugins/maven-dependency-plugin/ copy-dependencies-mojo.html
(5) mvn dependency:unpack, copy the JAR package configured in the plug-in to the specified location, and decompress the JAR package; (6)
mvn dependency:unpack-dependencies, copy the project POM file All dependencies defined in and their transitive dependencies are copied to the specified location, and the JAR package is decompressed;

Application Scenario

(1) A special JAR package cannot be obtained directly through MAVEN dependencies or does not exist in the MAVEN warehouse, so how to put the JAR package we need into our production JAR package? Use: mvn dependency:copy can solve this problem
(2) A special JAR package contains the files we need, or how do we extract the required files from the JAR package and put them in the specified location? Use: mvn dependency:unpack can solve this problem

how to use

The first step is to add the following content to the POM file:
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>3.2.0</version>
      <!--<executions> 标签中添加需要执行的操作,可以添加的操作如下: -->
      <executions>
        ...
      </executions>
    </plugin>
  </plugins>
</build>
In the second step, add related operations as needed:
  1. Copy all dependencies to the target/libs directory.
<execution>
  <id>copy-dependencies</id>
  <phase>package</phase>
  <goals>
    <goal>copy-dependencies</goal>
  </goals>
  <configuration>
    <outputDirectory>${
    
    project.build.directory}/libs</outputDirectory>
  </configuration>
</execution>
  1. Unzip all dependent packages to the target/classes/lib directory.
<execution>
  <id>unpack-dependencies</id>
  <phase>package</phase>
  <goals>
    <goal>unpack-dependencies</goal>
  </goals>
  <configuration>
    <outputDirectory>${
    
    project.build.directory}/classes/lib</outputDirectory>
  </configuration>
</execution>
  1. Output the dependency list to target/dependency-report.txt report file.
<execution>
  <id>dependency-report</id>
  <phase>compile</phase>
  <goals>
    <goal>list</goal>
  </goals>
  <configuration>
    <outputFile>${
    
    project.build.directory}/dependency-report.txt</outputFile>
  </configuration>
</execution>
  1. Copy junit-4.11.jar to the ${project.build.directory}/lib/lib1 directory, and copy slf4j-log4j12-1.7.7.jar to the ${project.build.directory}/lib/lib2 directory.
<execution>
	<id>copy</id>
	<phase>package</phase>
	<goals>
		<goal>copy</goal>
	</goals>
	<configuration>
		<artifactItems>
			<artifactItem>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.11</version>
				<outputDirectory>${
    
    project.build.directory}/lib/lib1</outputDirectory>
			</artifactItem>
			<artifactItem>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>1.7.7</version>
				<outputDirectory>${
    
    project.build.directory}/lib/lib2</outputDirectory>
			</artifactItem>
		</artifactItems>
	</configuration>
</execution>
  1. Copy all the files under META-INF/assembly/bin in the dubbo-2.8.4.jar file to the ${project.build.directory}/dubbo directory.
<execution>
	<id>unpack</id>
	<phase>package</phase>
	<goals>
		<goal>unpack</goal>
	</goals>
	<configuration>
		<artifactItems>
			<artifactItem>
				<groupId>com.alibaba</groupId>
				<artifactId>dubbo</artifactId>
				<version>2.8.4</version>
				<outputDirectory>${
    
    project.build.directory}/dubbo</outputDirectory>
				<includes>META-INF/assembly/bin\**</includes>
			</artifactItem>
		</artifactItems>
	</configuration>
</execution>

Guess you like

Origin blog.csdn.net/goodjava2007/article/details/126519696