Create spring project using maven

concept

Maven is a dependency management tool for java. The so-called dependency refers to various jar packages and third-party libraries introduced in the Java development process, and these libraries may also refer to other libraries in this province, so that we directly refer to as direct dependencies , and library dependencies are called indirect dependencies . If we manually introduce these dependencies, the process will become very cumbersome, so we need a warehouse Maven to manage and introduce these dependencies for us. Each dependency package is called a component in Maven .

So how do we find each component accurately from the warehouse? This needs to be done through coordinates . The coordinates of the maven warehouse are composed of groupId, artifactId, and version. Where groupId is your company name, artifactId is your project name, version is the project version number, and package is the name of the package you need to create. That is, when we look for dependencies in the maven repository, we first find the company name groupId, then the project name, and finally find the version number to uniquely determine a dependency.

Maven's warehouse is divided into local warehouse and remote warehouse. Local warehouse means that maven will download the dependency package to a local location. When searching for dependencies, first find the local warehouse. If it is not found, return to the online remote warehouse to find and download . If everyone visits the same maven warehouse, it will cause too much load pressure and slow access, so maven provides mirror warehouses in different places for people to visit nearby.

Maven also provides many plug-ins , we can use the plug-in to easily complete the project construction, compilation, packaging and other operations.

In Maven, if A depends on B, B depends on C, and C depends on D, and A depends on E and E also depends on D, then A will depend on D, which is closer to E. If the two dependencies are as close as A, they will be dependent on the order of dependency resolution.

Installation and configuration

Installation: First download the required maven compression package from the Maven official website , then unzip it, and finally add the bin folder in the unzipped directory to the PATH path to use mvn --v on the command line to view the version number.

Configure the repository: edit the maven / conf / settings.xml file and specify the location of the local repository in the <localRepository> tag.

<localRepository>D:/Java/maven/repository</localRepository>

Add a domestic mirror source: downloading dependencies from a domestic mirror source is faster than using the default download source directly. Configure as the mirror source of Alibaba Cloud in the <mirros> tag as follows

  <mirrors>
    <mirror>
    <id>aliyunmaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/public </url>
    </mirror>
  </mirrors>

Create project

The commonly used commands of maven are as follows:

mvn -compile

Compile the project

mvn -test

Test items

mvn -package

Package the project

mvn -install

Install jar package to local warehouse

mvn -clean

Delete the compiled object file

Create a project through the archetype plugin: enter mvn archetype: generate on the command line to automatically create a maven project. As shown below, you will be prompted to select the maven type number to be created, and enter groupId, artifactId, version, package, etc.

You can also specify groupId, artifactId, version, package, etc. in a command line

mvn archetype:generate -DgroupId=com.tory -DartifactId=spring-mvc-study -DarchetypeArtifactId=maven-archetype-webapp

Finally, use the jetty container to run the project: mvn jetty: run, pay attention to the need to introduce the jetty plugin in the pom, of course, you can also introduce the Tomcat plugin to run the project in the container.

It is easier to create a Maven project in IntelliJ IDEA. First, configure maven in IDEA: Open-> File | Settings | Build, Execution, Deployment | Build Tools | Maven in IDEA to configure the following three option bars, corresponding to maven Installation directory, configuration files and warehouse directory

 Then create a Maven project, click File | New | Project to pop up the following interface, select the maven type and click Next. If you need to create a template, you can check create from archetype and select the desired template

Next, set the project name and location, and set the GroupId, ArtifactId, Version information

Finally, select maven information, click Finish to complete

pom.xml file

POM is short for Project Object Model. After creating a maven project, a pom.xml file will be generated in the root directory to manage dependencies. As follows,

First, <modelVersion> specifies the version of the POM.

<groupId>, <artifactId>, <version> respectively specify the three coordinates of the project, groupId is usually composed of "company name. Project name", artifactId is composed of "project-module name". Version generally consists of three digits, the first digit represents the major version number, the second digit represents the branch version number, and the third digit represents the minor version number. The following Snapshot represents the snapshot version, alpha represents the beta version, beta represents the public beta version, RC (Release Candidate) is the last release candidate, release represents the stable version, and GA (General Availability) is officially released.

<packaging> specifies the packaging method of the project, the default is jar package

<name> represents the name of the project, <url> is the URL of the project

In <dependencies>, the specific dependencies required by the project are introduced by means of coordinates. <scope> represents the scope of dependency usage. Here, test represents that the dependency can only be used in test. The default is compile, which is effective in compiling, testing, and running, provided is effective in compiling, testing, and runtime is effective in toilets and running. You can also use the <exclusion> tag to exclude transitive dependencies that refer to dependencies, such as A depends on B, B depends on C, and you do not want to reference C when introducing B dependencies in A, then you can use exclusion to exclude C.

Introduce the used maven plugin under <build>

<?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">
  <!--指定POM版本-->
  <modelVersion>4.0.0</modelVersion>

  <!--项目的maven坐标-->
  <groupId>org.example</groupId>
  <artifactId>SpringMVC</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SpringMVC Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <!--项目所需依赖-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <!--所用maven插件-->
  <build>
    <finalName>SpringMVC</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Pom's dependencies can be inherited. For example, the junit dependency introduced in the parent project's <dependencyManagement> is as follows. After the child project depends on the parent project, it can inherit junit without introducing it again.

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

 

Published 124 original articles · Like 65 · Visit 130,000+

Guess you like

Origin blog.csdn.net/theVicTory/article/details/105611536