Getting to know the project management tool Maven

Preface

  • Maven Project Object Model (POM), a software project management tool that can manage project construction, reporting and documentation through a short description of information

  • In project development, Maven can manage the dependencies between the jar package and the project, integrate the tomcat plug-in, and the maven project can be automatically published to tomcat.

  • The jar package is stored in the maven warehouse, which can be downloaded at one time and is common to all projects.

1. Install Maven

1.1 Download Maven

Download the official website address of maven: http://maven.apache.org/download.cgi
After downloading, unzip it to the installation path.

Insert picture description here

1.2 Modify the configuration file

There is a settings.xml
Insert picture description here
edit configuration file in the conf folder , specifying the path of the local warehouse, that is, where the downloaded jar package is stored.
Insert picture description here

1.3 Configure environment variables

Add the variable MAVEN_HOME, the variable value is the decompression path in 1.1.
Insert picture description here
Modify the Path variable and append %MAVEN_HOME%\bin\ to the original Path

1.4 Testing

Open a command window, run mvn -version
Insert picture description here
here, Maven is installed successfully.

2. Configure the JDK version

Java projects created using Maven need to set the default jdk version in the configuration file.
Modify the conf/setting.xml file of the Maven installation directory and find the node where the jdk is configured:

 <profile>
  <id>jdk18</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>

Remarks: Do not put the configuration information here in the comment. Many people paste it and put it directly in the comment, and the configuration here will be slightly different because of the different versions of Maven, so it is not recommended to copy directly

3. Configure mirroring

The central warehouse of Maven is located abroad. If the network is restricted, the download of the jar package will be slow or impossible
. You can find some domestic mirrors on the Internet:
find the node of setting.xml and add the following content:

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

Note: The configuration here is Alibaba Cloud image, readers can search for other images

4. How to configure Maven in IDEA

Insert picture description here
Insert picture description here
Note: The configuration of Maven is completed here, but some people will enter the IDEA interface and configure it through FILE-SETTINGS-search for Maven. This configuration is not a global configuration. It can be modeled as follows:
Insert picture description here
Insert picture description here
Insert picture description here
At this time, the Maven configuration is found after creating a new project Information is perfect

5. How to create a Maven project in IDEA

5.1 Maven project

Insert picture description here
Insert picture description here
The GroupId here is the organization name, which is unique in the world. ArtifactId is usually the name of the project. There can be multiple projects under one organization. Version is the version number. It is not recommended to use the SNAPSHOT snapshot version when the project is officially released.
Insert picture description here
Insert picture description here
Insert picture description here
If the creation is successful, you will see the complete directory structure in IntelliJ.
If the creation fails, you can view EventLog in the console.
Some resources Maven need to be downloaded online. If the download fails, the project may fail to be created.
If you create a project with the same name, an error will also be reported, as follows:
Insert picture description here
At this time, you need to delete the corresponding folder in the project directory and delete the corresponding file in the recycle bin.
Note: The creation here will be different due to different IDEA versions

5.2 Create directory

Right-click main->New->Directory,
Insert picture description here
Insert picture description here
right-click the java folder ->Mark Directory As ->Sources Root, change the folder to a source file
Insert picture description here
. Create the resources folder in the same steps, right-click resources->Mark Directory As -->Resources Root
java The folder is used to store the package, which is equivalent to the src directory in the eclipse common project.
Resources are used to store configuration files and property files.
You can also create a test folder under src for junit testing. Also create java and resources under test.
Insert picture description here
Note: If you use a newer version of IDEA, some of these folders will be created by themselves

5.3 Maven configuration file

pom.xml is the core configuration file of maven. In pom.xml, you can define the dependencies of the project and
the information of the reference jar package.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

The jar package of junit is quoted here, the version is 4.12

5.4 Configure the plugin

<build>
 <finalName>test001</finalName>
  <plugins>
   <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
     <artifactId>tomcat7-maven-plugin</artifactId>
      <configuration> <path>/</path> <port>8080</port>
      </configuration>
    </plugin>
  </plugins>
</build>

Pligins are used to define plug-ins, and the tomcat plug-ins are referenced here. Path is the access path. If we run
multiple projects at the same time , the access path is the root path. You can access the project by modifying the port number.

5.5 Testing

Insert picture description here
Insert picture description here
Insert picture description here
Command: clean tomcat7:run After the
configuration is successful, you can see the command you just configured in the toolbar, click the green triangle and
Insert picture description here
click Run, you can access the resource in the browser (provided that the resource has been created). For
example:Insert picture description here

5.6 View jar package

Usually, after adding configuration information in pom.xml, maven will automatically download the jar package
. You can see the jar downloaded by maven in the project’s External Libraries:
Insert picture description here
if you add it in pom.xml, you can’t see the jar package on the left, You can right-click pom.xml and select Maven->Reimport
Insert picture description here
or click Maven Project on the right side of IDEA: When the
Insert picture description here
jar package is not imported correctly, an error message will appear in pom.xml:
Insert picture description here
obvious red

5.7 Eliminate jar package conflicts

<dependency>
 <groupId>junit</groupId>
  <artifactId>junit</artifactId>
   <!-- 排除冲突jar包 -->
<exclusions>
 <exclusion>
   <groupId>org.hamcrest</groupId>
   <artifactId>hamcrest-core</artifactId>
   </exclusion> 
 </exclusions> 
</dependency>

6. Inheritance of Maven

6.1 pom project

There is only one pom.xml file in the pom project, which does not contain the java and resource directories. It is only used to store some
dependency information.
Add dependency information in pom.xml, such as junit, mysql, etc., or define plug-in information:

<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.maven.test</groupId> <artifactId>test-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> 
<!-- 自定义属性 -->
<properties>
 <junit.version>4.12</junit.version> 
 <mysql.version>5.1.32</mysql.version>
</properties> 
<!--继承自该项目的所有子项目的默认依赖信息。这部分的依赖信息不会被立即解析, 而是当子项目声明一个依赖(必须描述group ID和artifact ID信息),如果group ID和 artifact ID以外的一些信息没有描述,则通过group ID和artifact ID匹配到这里的依 赖,并使用这里的依赖信息。-->
<dependencyManagement>
 <dependencies>
  <!--用于junit测试 -->
<dependency>
 <groupId>junit</groupId>
  <artifactId>junit</artifactId> 
  <version>${
    
    junit.version}</version>
</dependency> 
   <!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId> 
<artifactId>mysql-connector-java</artifactId>
<version>${
    
    mysql.version}</version>
</dependency>
</dependencies> 
</dependencyManagement> 
<build> <!--产生的构件的文件名,默认值是${
    
    artifactId}-${
    
    version}-->
<finalName>${
    
    project.artifactId}</finalName> 
<!--使用的插件列表 。-->
<plugins> 
<!-- 资源文件拷贝插件 -->
<plugin>
 <!--plugin元素包含描述插件所需要的信息。-->
<groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> 
 <!--作为DOM对象的配置-->
<configuration> 
<encoding>UTF-8</encoding> 
</configuration>
 </plugin> 
 <!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-compiler-plugin</artifactId>
 <version>3.2</version> 
 <configuration>
  <source>1.8</source>
   <target>1.8</target> 
   <encoding>UTF-8</encoding>
   </configuration> 
  </plugin> 
</plugins> <!--子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或 绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置-->
<pluginManagement>
 <plugins> 
 <!-- 配置Tomcat插件 -->
<plugin>
 <groupId>org.apache.tomcat.maven</groupId> 
 <artifactId>tomcat7-maven-plugin</artifactId>
 <version>2.2</version>
 </plugin> 
</plugins> 
</pluginManagement>
</build> 
</project>

6.2 Create subproject

Insert picture description here
Add the dependencies defined in the parent project to the subproject:

<dependencies> 
<dependency> 
<groupId>junit</groupId>
 <artifactId>junit</artifactId> 
 </dependency> 
</dependencies>

No need to write the version number, it will automatically inherit the version defined in the parent project.

Seven. Packing

Insert picture description here
clean clean the compilation result
package package
install install to local warehouse

Guess you like

Origin blog.csdn.net/weixin_44726976/article/details/106358121