The core function of Maven is to build dependencies between projects. In general, it is to get jar packages from the pom.xml configuration file, without manually adding jar packages. How to get jar packages from the POM.xml configuration file? Where does the POM.xml configuration file come from? If you need to use POM.xml to retrieve jar packages, you must first have a Maven project. You can think of a Maven project as a layer of Maven on top of a Java project and a Web project. Essentially a Java project is still a Java project. A Web project is still a Web project, but with Maven wrapped around it, you can use some of the functionality provided by Maven (adding jars via POM.xml).

The first step:

What happens to importing maven dependencies.

We wrote a POM dependency as follows

      

Typically, we use the groupId, artifactId, version tags to identify the unique coordinates of this dependency, just as we use YXZ to locate a point in the coordinate system.

When we get this coordinate, we go to the local repository ($user.home/.m2/repository) to find the dependency (i.e., the jar package). Did not go to the private server warehouse (is generally set up by the company itself, only for the company’s internal shared use. It can be used either as an internal company component for collaboration and archiving, or as a common library image cache, reducing the frequency of external access and download. In order to reduce the access to the central warehouse, private servers can use the LAN, and the central warehouse must use the Internet, which means that the general company will create such a third-party warehouse, to ensure that the project development, the required JAR from the warehouse, everyone’s version is the same. Note: Connecting to a private server requires separate configuration. If the server is not configured, it is not used by default), there is no central repository (which is repo1.maven.org/maven2) to find, and then…

The second step:

After maven is found and introduced, let’s talk about the Maven lifecycle.

The maven lifecycle is as follows: The Maven lifecycle is shown in IDEA from top to bottom.

  • Clean: Removes all files generated from the last build
  • Validate: Verifies that the project is correct and all required resources are available
  • Compile: compile the source code of the project
  • **test: ** Test the compiled source code using the appropriate unit testing framework. These tests do not need to be packaged and deployed.
  • **package: **package compiled code into a distributable format such as JAR, WAR, etc.
  • **verify: ** runs all checks to verify that the package is valid and meets quality standards.
  • **install: **install the package into maven’s local repository so that it can be used as a dependency by other projects
  • Site: Generates site documents for the project
  • Deploy: Executes in an integration or distribution environment, copying the final package to a remote Repository so that other developers or projects can share it.

** Note that _ : _** from bottom to bottom is an execution containing relationship. If I execute clean, I will only execute clean, removing the build file. But if I run compile, it will run clean, validate, and compile.

The third step

Finally, let’s talk about how Maven eliminates dependencies, which is a bit of a headache when using Maven.

Method 1: Use the IDEA plug-in Maven Helper.

There is no one who can’t download and use IDEA plug-in, no, no, no.

Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper: Maven Helper.

The ICONS have the following meanings

  • Conflicts (see Conflicts)
  • View All Dependencies as List
  • View All Dependencies as Tree

The figure above shows that there are three jars in conflict. Click the jar in conflict to view which jar is in conflict with, as shown in the figure below

Method 2: Run the MVN -dverbose dependency:tree command in Terminal under IDEA

(Maven has added environment dependencies)

You can see the dependency tree for the project as follows:

This is the emergence of dependency conflict, some will not be reported wrong, unless you are a Virgo don’t like it, it is not necessary to remove.

When a conflict is found, how can it be resolved?

Method 1: Use Maven Helper

Switch to the Maven dependencies view and select the conflict option. If there is a conflict, it will be shown in red in the lower left area.

To resolve the conflict, right-click the red area and select Exclude from the menu.

The result of exclusion is as follows:

<dependency> <groupId>com.juejin.business</groupId> <artifactId>juejin-image-web</artifactId> < version > 1.0.0 - the SNAPSHOT < / version > < exclusions > < exclusion > < groupId > com. Juejin. Business < / groupId > <artifactId>juejin-convert-web</artifactId> </exclusion> </exclusions> </dependency>Copy the code

Method 2:

Or we can add it directly under the POM file

It produces the same effect as using a plugin:

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> < version > 5.2.7. RELEASE < / version > < exclusions > < exclusion > < artifactId > spring - the core < / artifactId > <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency>Copy the code

Or use OptionNal to make the dependency optional. (Optional is an option when Maven relies on jars, meaning that the dependency is optional. Not passed by dependencies) **

<dependency> <groupId>com.juejin.business</groupId> <artifactId>juejin-convert-web</artifactId> < version > 1.0.0 - the SNAPSHOT < / version > < optional > true < / optional > < / dependency >Copy the code

Why use optional

  • Reduce unnecessary dependency passing
  • Reduce JAR conflicts

For example,

  • B relies on the logging frameworkLogback, log4j, and Apache Commons log
  • In this case, the dependency is as follows: A->B

Because Maven has a dependency delivery mechanism. Project A will have three JAR packages,logback, log4J, and Apache Commons log. In fact, we usually only use one logging framework for our projects. Then we will have redundant dependencies in the project. When this happens more and more often, the entire project’s JAR packages end up with many redundant dependencies, resulting in a bloated project.

How to optimize

Set logback, log4j, Apache Commons log to
true

For example,

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <optional>true</optional>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <optional>true</optional>
</dependency>
Copy the code

When project A is dependent on project B, the logback, Log4J, and Apache Commons log JAR packages are not available in the project.

It’s time to leave, so I’ll stop there and talk about it next time.