Full of maven dry goods, are you sure you don't want to take a look?

Foreword

Whether you are a newbie who has just come into contact with maven, or a master of maven, I believe you can gain something in this blogger's blog.

The role of maven

I still remember that when I just started learning java and needed to introduce external dependencies, I looked for jar packages everywhere on the Internet. The feeling of helplessness is still engraved in my mind until now. However, since I learned about the maven tool, I have never looked for a black history of jar packages. Because, maven can be unified management of jar package dependencies in the project! This is also the biggest use of maven.

If we don't use maven or similar tools in the project, the dependencies in the project require us to download manually, which is very troublesome; but when we use maven to build the project, the pom file provided by maven for the project can be used for the project. All the dependencies in the project are managed in a unified manner, and the dependencies needed in the project can be directly introduced in the pom file, because maven will automatically download the jar package from the remote warehouse to the local warehouse.

What is maven?

After everyone understands the functions of maven, it is also time for us to define maven.

Maven is a project management tool, it has high reusability, only a few lines of maven build script can build a simple project. Multiple projects in maven share a local warehouse, each project's jar package depends on this, and the required dependencies need to be specified in the pom file.

Common maven commands

The number of maven commands is actually quite a lot. Let's pick a few commonly used ones to explain. After all, listing all the maven commands here is too confusing, right? It is better to remember a few commonly used ones. (All the maven commands, how can bloggers!)

So which maven commands are common? These maven commands that can be used directly in idea may be frequently used by programmers in their work.

Insert picture description here

  1. mvn clean: will clean up the project and delete the compiled content in the target directory
  2. mvn validate: verify that the project is correct and that all required resources are available
  3. mvn compile: compile project code
  4. mvn test: run unit tests
  5. mvn package: package the project
  6. mvn verify: run any check to verify that the package is valid
  7. mvn install: install it in the local warehouse after packaging
  8. mvn site: generate site directory
  9. mvn deploy: install it to the remote repository configured in the pom file after packaging

An example of a pom file

As we said before, the pom file can manage all the dependencies in the project in a unified way. The dependencies required in the project can be directly introduced in the pom file. So, what is a pom file, or what does a pom file look like? Let's take a look at the pom file of a Spring Boot project.

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ok</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ok</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. modelVersion: model version
  2. groupId: the unique logo of the company or organization, the path generated during configuration is also generated from this
  3. artifactId: the unique ID of this project, there may be multiple projects under one groupId, which depends on artifactId to distinguish
  4. version: the current version number of this project
  5. packaging: packaging mechanism
  6. properties: define some constants that can be directly referenced elsewhere in the pom
  7. dependencies: define the dependencies of this project
  8. dependency: Each dependency corresponds to a jar package
  9. scope: domain, including compile (compiled scope), provided (provided scope), runtime (runtime scope), test (test scope), system (system scope)
  10. exclusions: shield dependencies
  11. plugins: plugins

To sum up, the pom file contains the dependencies of the maven project, the packaging mechanism and detailed information of some projects. It can also be called the most important file in maven.

Three ways to package projects

The three methods are pom packaging, jar packaging and war packaging, of which the packaging method needs to be specified in pom.xml.

  1. pom: aggregation project, that is, parent project, to do unified dependency management, such as managing the version of the jar package, etc.
  2. jar: The most conventional packaging method, which can be a sub-project of the pom project
  3. war: web project, that is, a project that can be run directly in tomcat

Reference: Usage of maven and several commonly used commands

Published 151 original articles · praised 317 · 40,000+ views

Guess you like

Origin blog.csdn.net/Geffin/article/details/105608522