JavaWeb: Maven explained

1. Introduction to Maven

What is Maven

Maven is a tool specifically used to manage and build Java projects. Its main functions are:

  1. Provides a standardized project structure
  2. Provides a set of dependency management mechanisms (no need to manually import jar packages)
  3. Provides a set of standardized build processes (compile, test, package, release...)

Maven standardized project structure

Maven provides a set of standardized project structures. The project structures built by all IDEs using Maven are exactly the same. Maven projects created by all IDEs can also be used universally.

image-20231102205711547

2. Maven warehouse

Warehouse classification

  1. **Local warehouse:** A directory on your own computer

  2. **Central Warehouse: **The only warehouse in the world maintained by the Mavne team

    Address: https://repo1.maven.org/maven2

  3. **Remote warehouse (private server): **Private warehouse generally built by the company team

Implementation process

When the corresponding dependent jar package is introduced using coordinates in the project, it will first check whether the local warehouse has the corresponding jar package.

  • If not, go to the central warehouse to download the corresponding jar package to the local warehouse.
  • If so, reference it directly in the project

To build a remote warehouse, the search order for jar packages is

  • Local warehouse --> Remote warehouse --> Central warehouse
3. Maven installation

Maven is a green software, you can install it by unzipping it.

Download location:Maven – Download Apache Maven

  • Follow two files
    1. bin/mvn.cmd executable file
    2. conf/settings.xml configuration file
  • Be careful not to put it in the Chinese directory
4. Maven configuration

Maven three configuration methods

  1. Configure the local warehouse: modify line 55 in conf/settings.xml to a specified directory

    <localRepository>D:\software\mvn_resp</localRepository>
    
  2. Configure Alibaba Cloud Private Server: Modify the tag on line 160 in conf/settings.xml and add the following subtags to it

        <mirror>
          <id>nexus-aliyun</id>
          <mirrorOf>central</mirrorOf>
          <name>Nexus aliyun</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
    
  3. Maven uses the jdk1.5 version by default. The label on line 188 can be modified or it can be left unchanged. It is upwardly compatible.

    <profile>    
        <id>jdk-1.8</id>    
        <activation>    
            <activeByDefault>true</activeByDefault>    
            <jdk>1.8</jdk>    
        </activation>    
        <properties>    
            <maven.compiler.source>1.8</maven.compiler.source>    
            <maven.compiler.target>1.8</maven.compiler.target>    
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
        </properties>    
    </profile>
    
5. IDEA configures Maven

点击file -> Settings -> Build Execution Deployment -> Build Tools -> Maven

Fill in your Mavne address

image-20231103092253399

6. Maven coordinates

what are coordinates

  • Coordinates in Maven are unique identifiers of resources
  • Use coordinates to define the project or introduce the dependencies needed in the project

The main components of Maven coordinates

  • groupId: defines the name of the organization that the current Maven project belongs to (usually the domain name is written in reverse)
  • artifactId: defines the current Mavne project name
  • version: defines the current project version number
7. IDEA creates Mavne module

Create steps

  1. Create the module, select Maven, and click Next
  2. Fill in the module name and coordinate information, click finish, and the creation is completed.
  3. Write HelloWorld and test run

Creation process

Select the new project and click Maven

image-20231103094439177

Check the box below to modify the module name and coordinate information.

image-20231103094519088

Click Finish again

image-20231103094551522

8. Maven dependency management

Import jar package using coordinates

  1. Write tags in pom.xml

        <dependencies>
            
        </dependencies>
    
  2. Use imported coordinates in labels

    <dependencies>
        <dependency>
        </dependency>
    </dependencies>
    
  3. grupId, artifactld, version that defines coordinates

        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.46</version>
            </dependency>
        </dependencies>
    
  4. Click the refresh button to make the coordinates effective

    image-20231103102157562

Use shortcuts to import jar packages

  1. Press alt+insert in pom.xml and select Dependency

    image-20231103102529924

  2. Search for the corresponding coordinates in the pop-up panel, then double-click to select the corresponding coordinates

    image-20231103102610833

  3. Click the refresh button to make the coordinates effective

    image-20231103102704242

Query jar package dependencies online

  1. Search dependencies through the official website: https://mvnrepository.com

    image-20231103103055376

  2. Select the corresponding resources and copy them to the pom.xml file.

    image-20231103103258702

**Note:** For the same jar package, the later one will replace the earlier one.

9. Common Maven commands

IDEA operates Maven commands

Order illustrate
mvn clean Delete target directory
mvn compile Compile main directory
mvn test Execute test directory
mvn package Package to target directory
mvn install Install into local repository

Case presentation

  1. mvn clean

    image-20231103144833351

    image-20231103144924306

  2. mvn compile

    After compilation, the target directory will be regenerated.

    image-20231103145036723

  3. mvn test

    There are relatively few tests done here.

    image-20231103145719267

  4. mvn package

    After packaging, a jar package will be generated in the target.

    image-20231103145940907

10. Maven life cycle
  1. The Maven build project life cycle describes how many events a build process has gone through.

  2. Maven divides the project construction life cycle into three sets

    • clean: cleaning work
    • default: core work, such as compilation, testing, packaging, installation, etc.
    • site: generate reports and publish sites
  3. There is no way to skip it during the build process

    For example: when we are testing, there is no way to skip compilation and directly test.

  4. overall life cycle

    image-20231103152139687

11. Maven dependency management

By setting the dependency range (scope) of the coordinates, you can set the scope of the corresponding jar package.

When not set, the default is the scope of compile, that is, all paths can be used

image-20231103152605869

dependency scope Compile classpath (main) Test classpath(test) Run classpath(target) example
compile AND AND AND logback
test - AND - Junit
provided AND AND - servlet-api
runtime - AND AND jdbc driver

code demo

  • Import druid

    	<!--        druid连接池依赖-->
    <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid</artifactId>
         <version>1.0.9</version>
         <scope>compile</scope>
    </dependency>
    
  • When nothing is configured, both the main directory and the test directory are available.

    image-20231103153646837

  • Add scope in pom.xml file

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.9</version>
                <scope>test</scope>
            </dependency>
    
  • Found that only the test directory can be used

    image-20231103153842008

Guess you like

Origin blog.csdn.net/weixin_53961667/article/details/134205307