1. Why use Maven?

The third-party JAR packages that the project depends on are in the classpath of the project (usually the lib directory), as shown below:

This way, we have no way of knowing the dependencies of third party libraries. For example, when importing a particular JAR package, that JAR package may depend on other Jars, which in turn depend on more Jars, which is the main cause of the ClassNotFound exception we encounter on a regular basis.

To solve this problem, we urgently need to introduce a project build tool. At present, the mainstream project building tools include: Maven, Gradle, etc.

Here we chose Maven as the project build tool.

2. Maven profile

Apache Maven is a software project management tool.

Based on the concept of the Project Object Model (POM), Maven can be used to manage Project dependencies, compilations, documentation, and more.

When using Maven to manage a project, the jar packages that the project depends on are no longer included in the project. By default, the jar packages are centralized in the.m2 folder in the user directory, although this path can be customized to your preference.

3. The Maven installation

3.1 download Maven

Download at maven.apache.org/.

3.2 install Maven

Zip: E:\Program Files\ apache-maven-3.6.0-bin.zip: E:\Program Files\apache-maven-3.6.0

3.3 Configuring Environment Variables

Environment Variables popup opens in: Computer — Properties — Advanced System Settings — Advanced TAB — Environment Variables.

1) Create the system environment variable MAVEN_HOME (E:\Program Files\apache-maven-3.6.0).

2) Edit system variable Path and add:; %MAVEN_HOME%\bin

3.4 Verifying the Configuration

Open the CMD window and run the MVN -version command. If the following version information is displayed, the Maven configuration is successful.

3.5 Modifying Maven Configuration Files

Open settings. XML file in maven conf, go to localRepository node, and configure the localRepository path: E:\Java\MavenRepository

<localRepository>E:/Java/MavenRepository</localRepository>
Copy the code

Note: the path in the file is not the same as the path under Windows. After copying it, replace \ with/and do not include Chinese.

3.6 IDEA Configuring Maven

After opening IDEA, open File–Settings and configure Maven as shown below:

Note: This setting is for the current project only.

4. Maven POM files

Because Maven operates on the concept of the Project Object Model (POM), Maven projects have a POM.xml file that manages Project dependencies and Project compilation.

4.1 dependencies elements

<dependencies>
</dependencies>
Copy the code

This element contains multiple
that the project needs to rely on.

4.2 the dependency elements

Internally identifies unique dependencies by groupId, artifactId, version, which can be called coordinates.

GroupId: unique identifier of an organization.

ArtifactId: Unique identifier of the project.

Version: indicates the version of the project.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
Copy the code

4.3 the properties element

you can define variables to reference in dependency as follows:

<properties>
    <java.version>1.8</java.version>
    <spring-framework.version>4.3.18. RELEASE</spring-framework.version>
</properties>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-framework.version}</version>
</dependency>
Copy the code

4.4 Compiling Plug-ins

Maven provides a compilation plug-in that allows you to set the Java compilation level as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code

4.5 How Maven works

Maven automatically downloads dependency packages from the Dependency repository (mvnRepository.com/) via the Internet…

If you don’t know what you rely on the jar package dependency how to write, can go to https://mvnrepository.com/ to retrieve.

5. Manage Spring projects using Maven

5.1 Project Adding Maven dependencies

In IDEA, select the project root directory and right-click Add Framework Support. In the dialog box that is displayed, select Maven:

In the project directory, a pom.xml file will be generated with the following original content:

<?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>groupId</groupId>
    <artifactId>spring-action</artifactId>
    <version>1.0 the SNAPSHOT</version>

    
</project>
Copy the code

Here I changed the value of groupId to my domain name: com.zwwhnly.

<groupId>com.zwwhnly</groupId>
<artifactId>spring-action</artifactId>
<version>1.0 the SNAPSHOT</version>
Copy the code

5.2 Migrating configuration Files

Migrate the previous spring-config. XML configuration file to the SRC /main/resources directory.

5.3 Adding JAR dependencies for POM Files

Open File–>Project Structure–>Modules and delete the third-party JAR package introduced in the previous way:

Then add the JAR package dependency via the POM file:

<dependencies>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>com.github.stefanbirkner</groupId>
        <artifactId>system-rules</artifactId>
        <version>1.16.0</version>
    </dependency>
</dependencies>
Copy the code

After modifying the POM file, click “Import Changes” :

The project structure diagram after import is as follows:

As you can see, the jar packages are now managed by Maven. You can also view the dependency tree for your project:

5.4 Final POM file

<?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>com.zwwhnly</groupId>
    <artifactId>spring-action</artifactId>
    <version>1.0 the SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <spring-framework.version>4.3.18. RELEASE</spring-framework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>com.github.stefanbirkner</groupId>
            <artifactId>system-rules</artifactId>
            <version>1.16.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code

The method described above applies to the procedure for converting a new project to a Maven project if it is not a Maven project.

In fact, we can directly create a Maven project through IDEA, and then modify the POM file to introduce Spring dependencies for the above content (this method is recommended) :

6. Source code and reference

Source code address: github.com/zwwhnly/spr… Welcome to download.

Configuring Maven with IntelliJ IDEA (Getting started)

[Java] Maven installation and configuration

Convert regular Web projects to Maven projects with IDEA

Java EE Development Disruptor: Spring Boot Combat by Wang Yunfei