Using dependency plug-in to solve dependency package conflict

Time:2021-3-1

By chance, I went through the official documents of Maven dependency plug-in, and got quite a harvest. Let’s make a record. There may be some suspicions in the title party, but since you have printed out the dependency tree, the conflict relationship is clearly displayed in the tree.

Dependency tree

dependency:treeIt is probably the most commonly used function to check dependency conflicts. When no parameters are specified, all dependency information is printed. The amount of information is slightly large, which can be accessed through theincludesParameters specify which dependencies you want to see. You can also use theexcludesParameter specifies what you don’t want to see.includesandexcludesIt can be used together. For example:

#I just want to see that the dependency tree contains groupid javax.serlet  The branches of a tree
mvn dependency:tree -Dincludes=javax.servlet
#Don't want to see dependency tree containing groupid javax.serlet  The branches of a tree
mvn dependency:tree -Dexcludes=javax.servlet

The format of the parameter is defined as follows:

[groupId]:[artifactId]:[type]:[version]

Each part (colon divided part) is supported*If you want to specify multiple formats, you can use,Segmentation, such as:

mvn dependency:tree -Dincludes=javax.servlet,org.apache.*

By defaultdependency:treeThe print is Maven
The tree after the conflict is solved (the strategy to solve the conflict is: the nearest principle, that is, the dependency close to the root is adopted)-Dverbose
Parameters can display the original dependency tree, so that you can clearly see which branches a package appears on.

Purge local repository

Sometimes there are some puzzling problems when packing, but after clearing the local warehouse, the problem is solved (just like restarting the computer). Before, I went to the local private service directory to delete or delete the jar package corresponding to a groupid
If you delete everything, you will inevitably delete some innocent dependencies
The plug-in provides a goal, which can easily delete the jar package that the project depends on in the local directory

mvn dependency:purge-local-repository

Copy dependency or some jar packages to the specified directory

Before copying the dependent jars to the specified directory with the copy dependency goal of dependency, configure the POM file as follows:

<project>
[...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>/path/to/dest</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
[...]
</project>

For more parameter information, seedependency:copy-dependencies

If you only want to copy a few jar packages to the specified directory, you can usecopy goal:

<project>
[...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>junit</groupId>
                                    <artifactId>junit</artifactId>
                                    <version>3.8.1</version>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>/path/to</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
[...]
</project>

allegedlyanalyzeGoal can analyze which of the declared dependencies are not used, and which of the undeclared packages are dependent. It seems that it is not very good to try.

reference resources

Recommended Today

Emacs plug in ( two ) ——Persistent scratch

1、 Reference Contents of Emacs series articles — updating Re-open scratch buffer in Emacs? persistent-scratch Lisp Interaction Buffers 2scratch 2.1 basic introduction When startedeamcsWhen I was young, (1) It will enter automaticallyLISP interaction mode (2) Will automatically generate aLISP interactive buffer——scratch buffer 2.2 access*scratch* (1) C-x b *scratch* Execute the above command, it will enter […]