Maven Getting Started Tutorial (IDEA Version)

content

1. Introduction to Maven

1.1 How to import the jar package in the project?

1.2 What are the problems with the traditional way of importing jar packages?

1.3 Project Life Cycle

1.4 Introduction to Maven

Second, Maven installation and configuration

2.1 Maven download

2.2 Maven installation

2.3 Configure environment variables

3. Maven project structure

3.1 Maven project structure

3.2 pom.xml file

4. Dependency Management

4.1 Maven dependency management process

4.2 Introduction to Maven Warehouse

4.3 Maven repository configuration

5. Project life cycle management

5.1 Introduction to Life Cycle

5.2 Lifecycle Management Directives

Six, IDEA-based Maven use

6.1 Associate Maven in IDEA

6.2 Using IDEA to create a Maven project

6.2.1 Java Projects

6.2.2 web project

6.3 Using Maven for Dependency Management in IDEA

6.3.1 Finding dependent coordinates

6.3.2 Add dependencies

6.3.3 Dependency scope

6.4 Building a project with Maven in IDEA

6.4.1 Maven project build life cycle description

6.4.2 IDEA for life cycle management

Seven, private service

7.1 Private server setup

7.1.1 Download Nexus

7.1.2 Unzip nexus

7.1.3 Install and run nexus

7.1.4 Login to Nexus

7.2 Configure private server

7.2.1 In the servers tag of the maven settings.xml file

7.2.2 In the profiles tag of maven's settings.xml file

7.2.3 Configure the activation profile in the activeProfiles configuration of the maven settings.xml file

1. Introduction to Maven

1.1 How to import the jar package in the project?

  • Download the jar package (mvn)

  • Copy the downloaded jar package to the project (WEB-INF/lib)

  • Select the jar file - right click - Add as Library

1.2 What are the problems with the traditional way of importing jar packages?

  • Many steps (relatively) - tedious

  • If the same jar package is required in different projects, the jar file needs to be stored separately - redundant, large project size

  • In different environments, the project may not run (reconfigure) due to inconsistent jar file versions - poor portability

1.3 Project Life Cycle

The entire process of the project from compiling to running

Complete life cycle: clean cache - verify - compile - test - package - install - deploy

  • IDEA provides the ability to build a project with one click, but if we need custom lifecycle management, there is no ready-made tool (cleaning cache)

1.4 Introduction to Maven

Maven is a tool software for project dependency management and life cycle management based on the Project Object Model (POM).

Core functions

  • dependency management

  • life cycle management

  • Aggregation Engineering

Second, Maven installation and configuration

2.1 Maven download

2.2 Maven installation

Maven is developed based on the Java language, so it depends on JDK (JDK1.7+ is recommended)

Out of the box: just unzip it directly

  • Unzip (d:/mvn)

  • Directory Structure:

    • bin stores the instruction file (Maven provides a mvn instruction)

    • boot contains a jar file for the class loading framework

    • conf contains Maven's core configuration file settings.xml

    • lib stores the jar files required for maven to run

2.3 Configure environment variables

  • MAVEN_HOME D:\mvn\apache-maven-3.6.3

  • Path increases %MAVEN_HOME%\bin

test

 

3. Maven project structure

Another advantage of using Maven for projects is that the structure of the project is uniform no matter what development tool (eclipse/idea) is used.

3.1 Maven project structure

demo(project name)
--src
  --main (Store the source files of the project)
    --java (store java code, equivalent to the traditional project src content)
    --resources (The directory for storing configuration files and static resources, which is equivalent to that of traditional projects web content)
  --test (Store the unit test code of the project)
    --java(test code)
--pom.xml

3.2 pom.xml file

POM Project Object Model, Maven can perform dependency management on this project according to the configuration of the pom file; that is to say, if the project needs dependencies, it can be configured directly in pom.xml

<?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">
    <!--Specify project model version-->
    <modelVersion>4.0.0</modelVersion>
    <!--Specify the ID of the project: G Corporate logo A Project ID V Version-->
    <groupId>com.qfedu</groupId>
    <artifactId>fmwy</artifactId>
    <version>1.0.0</version>  
    <!--Configure the dependencies of the current project-->
    <dependencies>      
    </dependencies>
</project>

If you need to add dependencies in the current project, you only need to configure dependencies in the pom.xml file

Configure dependencies Find the coordinates of the dependencies and add the dependencies tag.

maven dependency repository URL: https://mvnrepository.com/

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

4. Dependency Management

4.1 Maven dependency management process

4.2 Introduction to Maven Warehouse

  • The local repository is a folder on the local computer (it can be any custom folder)

  • The remote repository is the jar file repository on the remote host

    • Central warehouse The warehouse provided by maven officially contains all the required dependencies (no configuration)

    • Public warehouses Third-party warehouses other than the central warehouse are public warehouses, such as aliyun (requires configuration)

    • Private server maven warehouse built by enterprises for internal use

maven repository

4.3 Maven repository configuration

Configure in maven_home/conf/settings.xml

Configure local repository

<localRepository>d:\repo</localRepository>

Configure public repository

<mirrors>
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
     </mirror>
</mirrors>

5. Project life cycle management

5.1 Introduction to Life Cycle

The declaration cycle of project construction: the process of deploying to the running environment after project development is completed

  • clear cache

  • an examination

  • compile

  • Test (the unit test in the test directory in the maven project will be executed)

  • Packaging (war, jar)

  • Install (jar will be installed to the local repository)

  • Deployment (put the package generated by the project into an external server - private server warehouse)

5.2 Lifecycle Management Directives

Execute the mvn command in the root directory of the project (this directory must contain pom.xml)

  • clear cache mvn clean

  • check mvn check

  • compile mvn compile

  • test mvn test

  • package mvn package

  • install mvn install

  • deploy mvn deploye

Six, IDEA-based Maven use

6.1 Associate Maven in IDEA

maven configuration

Note: IDEA itself integrates Maven. Considering the compatibility of IDEA and Maven versions, Idea does not recommend configuring a version newer than the default version. It is recommended to use the maven that comes with idea.

6.2 Using IDEA to create a Maven project

6.2.1 Java Projects

Create a Java project

6.2.2 web project

  • Create a maven project: maven-demo2

  • Set the packaging method to war in the pom.xml 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.qfedu</groupId>
        <artifactId>maven-demo2</artifactId>
        <version>1.0.0</version>
        <!--Set the project packaging method to war-->
        <packaging>war</packaging>
    </project>
  • Complete the web project structure

  • Configure web components - Tomcat

  • Deploy web project

6.3 Using Maven for Dependency Management in IDEA

6.3.1 Finding dependent coordinates

6.3.2 Add dependencies

  • Configure the coordinates of the dependencies to the dependencies tag of the project's pom.xml 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.qfedu</groupId>
    <artifactId>maven-demo2</artifactId>
    <version>1.0.0</version>
    <!--Set the project packaging method to war-->
    <packaging>war</packaging>
    <dependencies>
        <!--Configure the coordinates required in the project at this location GAV-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

6.3.3 Dependency scope

When adding a dependency through dependency, you can configure the scope of application of the current dependency through the scope tag

  • test only introduces current dependencies (compile, test) in the project testing phase

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>                
  • runtime is only used at runtime (run, test run)

  • provided in (compile, test, run)

  • compile is introduced in (compile, test, run, package)

6.4 Building a project with Maven in IDEA

6.4.1 Maven project build life cycle description

  • clean clean cache Clean the cache generated by the project

  • validate Validate that the project needs to be correct (project information, dependencies)

  • compile Compile the source code specific to the project

  • test test Run the unit tests in the project

  • package Package the compiled code of the project into a release format

  • verify check Check the results of integration tests to ensure that the quality of the project is up to standard

  • install install installs the package to maven's local repository so that this project (aggregation project) can be referenced in other local projects

  • deploy deploys the package to the private server's repository for sharing with other developers

6.4.2 IDEA for life cycle management

  • visualization

  • terminal command

    Select the project name---right click---Open in Terminal

    mvn clean

Seven, private service

Enterprise builds a Maven repository for internal use

  • Developers need to rely on downloading directly from private servers

  • The private server can realize the sharing of internal dependencies of the enterprise: when the enterprise develops a general plug-in (jar), it can be published to the private server, and other developers who can connect to the current private server can share the plug-in.

7.1 Private server setup

We can build private servers through specialized Maven warehouse management software. For example: Apache Archiva, Nexus

7.1.1 Download Nexus

7.1.2 Unzip nexus

7.1.3 Install and run nexus

  • Go to the nexus-2.14.11-01/bin directory
  • Open the cmd command line as an administrator and execute the command

7.1.4 Login to Nexus

Private server manager login address: http://localhost:8081/nexus

  • Warehouse type in private server

  • Configuration of private server warehouse group

7.2 Configure private server

7.2.1 In the servers tag of the maven settings.xml file

<!--Configure the account and password required to connect to the private server-->
<server> 
    <id>nexus-public</id> <!-- nexus certification id -->
    <username>admin</username> <!--nexus username password in-->
    <password>admin123</password> 
</server>

7.2.2 In the profiles tag of maven's settings.xml file

The value of the id subtag of repository and pluginRepository should be the same as the value of the id subtag of the server configured above.

<profile> 
    <id>nexus</id> 
    <repositories> 
        <repository> 
            <id>nexus-public</id>      
            <name>Nexus Release Snapshot Repository</name>   
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases><enabled>true</enabled></releases> 
            <snapshots><enabled>true</enabled></snapshots> 
        </repository>
    </repositories> 
    <pluginRepositories>
        <pluginRepository> 
            <id>nexus-public</id> 
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases><enabled>true</enabled></releases> 
            <snapshots><enabled>true</enabled></snapshots> 
        </pluginRepository> 
    </pluginRepositories> 
</profile>

7.2.3 Configure the activation profile in the activeProfiles configuration of the maven settings.xml file

 <activeProfiles>
	<activeProfile>nexus</activeProfile>
 </activeProfiles>

Tags: Java Web Development Maven intellij-idea

Posted by polybiosis on Tue, 03 May 2022 18:45:28 +0300