Getting started with maven (1)

Now basically many projects start to use maven to build projects, instead of placing jar packages directly under the lib folder, it is still necessary to learn to master maven.

For maven. This article mainly introduces a few points. If everyone understands it, you can refer to this mind map and review it again. After all, the knowledge will be forgotten if it is not used for a long time.
enter image description here

1. What is maven

Maven's Apache open source project is a project build tool and is also used for dependency management.

Second, the benefits of maven

1. Since the project built by maven has no jar package, the size of the project must be that the maven project is relatively small.
2. The jar package is uniformly handed over to maven for management.
3. Maven can also build projects.

Maven is mainly about project construction and dependency (jar package) management

Three, maven installation

Prerequisites for maven program installation: The maven program is developed in java, and its operation depends on jdk.

1. First go to Maven's official website and download the Maven package at http://maven.apache.org/download.cgi
2. After downloading and unzipping, configure the environment variables, similar to the JDK environment variable configuration (as shown in the figure)
enter image description here

enter image description here
3. Query maven version information
enter image description here
4. Configure local warehouse
Find config/setting.xml in the decompressed directory, mine is E:\app\apache-maven-3.5.3\conf\setting.xml
Mainly modify 2 places
4.1 Modify Local warehouse path
enter image description here
4.2 is modified to Alibaba Cloud image

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

enter image description here

4.4 Copy the setting.xml you just set to the local warehouse path you set, mine is E:\dev_maven
enter image description here

5. What are the types of warehouses
enter image description here

Fourth, use maven to build the project

Here I created it with eclipse

1. Eclipse configuration

1.1 Configure the maven program
enter image description here
1.2 Configure the userSetting, know the warehouse location
enter image description here
1.3 Build the index to find the jar package (Window->show view->maven Repository)
enter image description here

2. Start to create a project
2.1 Select the normal project - Maven Project here, click next
http://p7zk4x9pv.bkt.clouddn.com/maven/TIM%E6%88%AA%E5%9B%BE20180430165750.png

enter image description here

2.2 Select war as the packaging method and complete.
enter image description here

2.3 web.xml is missing and an error is reported

At this point, an error will be reported, and WEB-INF/web.xml needs to be created under src-main-webapp

Directory Structure
enter image description here

web.xml content

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

</web-app>

Also create an index.xml_(src\main\webapp\index.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello maven</h1>
</body>
</html>

Set the jdk compiled version to 1.8. The default is 1.5
Modify the pom file

<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.zhb</groupId>
  <artifactId>maven_hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>  
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

update maven
enter image description here

  1. run the project

enter image description here

tomcat:run

enter image description here

4. You're done
enter image description here
enter image description here

Five, maven common commands

clean: clean up

Clean up the target directory in the project root directory

compile: compile

Compile the .java files in the project into .class files

test: unit test

The unit test class name is required: XxxxTest.java
will execute the unit test class in the src/test/java directory in the project root directory.

package: package

web project --- war package
java project --- jar package Package
the project and package the taget directory under the project root directory.

install: install

Multiple local projects share a jar package.
package to local warehouse

Here, try it yourself, enter the project directory,
and execute mvn package if it is packaged.
enter image description here
enter image description here

Well, I wrote it for a long time. Finally done. Have fun.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167308&siteId=291194637