Maven Is a build tool , This build tool can help you better manage package dependencies 、 It can also realize the hot deployment of the project for you 、 And run all the test cases for you 、 You can also package your projects 、 Release 、 It can also realize the multi module construction of the project . This article talks about , How do I understand Maven And use .

Let's talk about it first Maven What can I do , It has the following functions :

1、 Good management of package dependencies in projects .

2、 It can also realize the hot deployment of the project ( The ability to publish projects directly to remote servers ).

3、 Package the project 、 Deploy .

4、 It can also run all test cases .

5、 Realize the multi module construction of the project .

This article , I will take you to build a project , To sum up systematically Maven Knowledge and practical thinking of , I hope it helped you . The content is divided into the following parts :

  • Maven Use

  • Use the command to create a java engineering

  • Project catalog analysis

  • pack

  • Construct a web engineering

  • Maven stay MyEclipse/eclipse On the usage of

  • stay Eclipse To create a Maven Of JAVA engineering

  • stay Eclipse establish Maven Of Web engineering

  • IDEA Use

  • IDEA Use skills

01 Maven Use  

1、 download MAVEN package    

1
Unzip to the specified directory   G:/maven Under the directory of 

2、 To use the command, first configure the environment variables    

1
M2_HOME=G:\maven\apache-maven-3.6.02
    stay path Quote from 3
   %M2_HOME%\bin

3、 test

‍‍‍‍

1
Enter at the command line  mvn  -version2
3
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)4
Maven home: G:\maven\apache-maven-3.5.0\bin\..5
Java version: 1.8.0_101, vendor: Oracle Corporation6
Java home: C:\Program Files (x86)\Java\jdk1.8.0_101\jre7
Default locale: zh_CN, platform encoding: GBK8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

4、 Indicating successful configuration .

02 Use the command to create a java engineering

1、 First, create a directory to store the project    

1
C:\Users\apple\Desktop\maven-project2、 Use the command to enter this directory 

1
cd /d C:\Users\apple\Desktop\maven-project3、 Use the following command 


1
mvn archetype:generate    ------> Get the created maven Engineering template 2
This will bring up 2300 Templates for multiple build projects   Selected by default 1311 Template No ( establish JAVA Engineering template )3
direct enter4
Select the version number of the template -------> By default 5
groupId: Group id   This is generally consistent with the main package 6
artifactId: The name of the project is usually written 7
Select the version number :------>  Generally, default is good 8
Write package name  ------> Default and groupId Agreement    Default is good 9
confirm  : Y4、 At this point, if you go to this directory again, you will have a folder for more projects 

03 Project catalog analysis

The directory is parsed as follows :

1
--------------src                               // The main thing is JAVA   resources    The test file 2
------------------------------------main                //JAVA File and running configuration file 3
------------------------------------------------java          // This one is JAVA Configuration file for 4
------------------------------------test                 // Put the test files and the resources needed for the test 5
-----------------------------------------------java          // The test of JAVA Code 6
---------------POM               // The coordinates of the project itself   Constant   Project dependency packages    Components that the project depends on 

 Project coordinates :


1
<groupId>com.qy.shopping</groupId>2
  <artifactId>MAVEN_01</artifactId>3
  <version>1.0-SNAPSHOT</version>4
  <packaging>jar</packaging>5
6
  <name>MAVEN_01</name>7
  <url>http://www.example.com</url>8
Library dependence :9
<dependencies>10
<dependency>11
   Of the group id12
      <groupId>junit</groupId>13
       Project name 14
      <artifactId>junit</artifactId>15
       Version number of the project 16
      <version>4.11</version>17
      When is the current library available 18
      <scope>test</scope>19
    </dependency>20
  </dependencies>21
22
library :23
    Local library      Remote library 04  pack  
1、 First, use the command to enter the root directory of the project    
1
C:\Users\apple\Desktop\maven-project\MAVEN_01\2
mvn package ----------------------> Generate target file 2、 Enter target file 1
Java -jar  MAVEN_01-1.0-SNAPSHOT.jar  The full path of the main class    Run it out
05  structure web Engineering steps   
1、 Enter the directory where the works are stored    
1
cd /d C:\Users\apple\Desktop\maven-project2、 Get all the templates
1
mvn archetype:generate3、 choice 1316 Template No  
1
Input 1316------> The back is the same as the front 4、 Analysis of project catalog    
1
-------------------------------src2
-------------------------------------------main3
---------------------------------------------------------java             Deposit java Of documents 4
---------------------------------------------------------webapp          Storage of shared resources 5
-------------------------------pom

Be careful :

Every different type of project is in maven There are different templates in

java The project has java Engineering template

web The project has web Engineering template

Suite The project has suite Engineering template ( Using my father as a project )

06 Maven stay MyEclipse/eclipse Usage of  

1、 download MAVEN stay Eclipse Plug in package on   

2、 Get into window----perfrence---- Input maven

3、installations----add Add local maven Root path

4、 Set local warehouse location and file location    

1
Be careful :setting.xml The location of the file is under the root path of the local warehouse  setting.xml The configuration file    In fact, this configuration file is not here    It was originally in maven Under the pressure of the road  conf Inside the directory    Then put this file copy To the root path of the library 2
Be careful : The default location of the library is c In the user directory of the disk .m2 In the document    This path can be changed 

07  stay Eclipse To create a maven Of JAVA engineering  

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍1、 Right click ----> newly build

1
maven engineering ---->Create  a  simple project----> Fill in the project coordinates ( Of the group id、 Project name 、 How to pack (jar)、 Dependent version )----> complete 

2、 Project directory resolution    

1
rc/main/java: Write JAVA Where to code 2
src/main/resource: This is where the configuration files are stored 3
src/test/java: Where to test the files 4
src/test/resoucre: Where test files are stored 5
target: This is where the packaged files and resources are stored 6
Pom: Deposit is ( Project coordinates 、 Dependency package 、 Dependent component 、 A variety of configurations ) 

3、 Write projects and test them

08  stay Eclipse To create a maven Of Web engineering  

1、 Right click ----> newly build

1
maven engineering ----->next----->maven archetype webapp( Templates )-----> next step ( Fill in the coordinates of the project )------>finish( complete )

2、 Directory resolution 

 1
----------------------------------------java Resource2
---------------------------------------------------------src/main/resource  : Where resource files are written 3
---------------------------------------------------------src/man/java      : To write JAVA Where to code 4
---------------------------------------src/main5
--------------------------------------------------------webapp: This directory is equivalent to the previous Web-content/Web app Catalog 

3、 Import web Common packages needed for Engineering

   

1
<!-- Import our Servlet Of API -->2
    <dependency>3
      <groupId>javax.servlet</groupId>4
      <artifactId>javax.servlet-api</artifactId>5
      <version>3.1.0</version>6
      <scope>provided</scope>7
    </dependency>8
 9
    <!-- Import US jstl The package of the tag library  -->10
    <dependency>11
      <groupId>javax.servlet.jsp.jstl</groupId>12
      <artifactId>javax.servlet.jsp.jstl-api</artifactId>13
      <version>1.2.1</version>14
    </dependency>15
 16
    <dependency>17
      <groupId>javax.servlet</groupId>18
      <artifactId>jsp-api</artifactId>19
      <version>2.0</version>20
      <scope>provided</scope>21
    </dependency>22
 23
    <dependency>24
      <groupId>org.apache.taglibs</groupId>25
      <artifactId>taglibs-standard-impl</artifactId>26
      <version>1.2.5</version>27
    </dependency>28
    29
    <!-- introduce junit class  -->30
    <dependency>31
      <groupId>junit</groupId>32
      <artifactId>junit</artifactId>33
      <version>4.12</version>34
      <scope>test</scope>35
    </dependency> 

4、 Create our jsp file    

1
<%@ page language="java" contentType="text/html; charset=UTF-8"2
    pageEncoding="UTF-8"%>3
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">4
<html>5
<head>6
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">7
<title>Insert title here</title>8
</head>9
<body>10
 11
  this is index jsp page12
 13
</body>14
</html>

5、 test ( Right click ---->run on server)

Be careful :

The project created by default is not written JAVA Where to code , So you need to create it manually

   

1
In the following src/main Right click above ----->new folder----> Name java  complete    It's on its own 

09 IDEA Use  

1、 Description of the home menu

1
Create new project : Create a new project 2
  Import project: Import a project (IDEA Project )3
  Open: Open a project 4
  Check out from version control: Represents checking out a project from the repository of version control tools 

2、 Create a java engineering    

1
Create new project ------- choice maven------ choice create from archetype------next----- Fill in the coordinates of the project 

10 IDEA Use skills  

1、 Set the shortcut key to Eclipse Shortcut keys    

1
File------setting-----keymap-----> choice Eclipse

2、 Coding settings 

 1
File-----setting----- Input encoding2
 3
File encoding  ----> Select global coding and engineering coding 3、 Font settings   
1
File------setting----- Input font----- find font----- The font size can be set on the right side
4、 Shortcut key customization 1
File------setting-----Editor----live templates----->  +  -----> add group (bobo1)-----> complete 2
                                        +  ----->live template ----> Input box input    Short for tips ------> stay template text Enter the content to be expressed in 3
 4
Select the created template -----> Right click   Change Context-----> Choose java( It means that java Prompt in the code )----- determine 5、 Change theme
   
1
File------------- setting------------------Appearance-------- On the right is the choice of themes ( Choose a theme that suits you )6、IDEA How to install maven 1
File------------setting------------------ Input maven--------> On the right side 7、Tomcat Installation
   
1
Run------Edit configrations----- top left corner  +  -----------> find Tomcat server-------->Local----->Application server( Later browsing )------>Configration-----> find Tomcat Root directory -----> complete 

Last words

without doubt , stay Java In ecology ,Maven Is the most popular software project auto build tool . It has replaced Ant For a long time , That's because it's in project management , Dependency management and solutions provide a simpler and declarative model , And it not only has a well-defined build phase ( Such as compiling and testing ), At the same time, it also supports the construction of , Configure and deploy code related plug-ins .

Friendship tips : If you are Maven Novice or inexperienced , So it's better to take some time to learn its basic usage first , Instead of learning through tips and techniques .

<END>

If you think that asamura's article will help you , Please search and follow on wechat 「 Shallow feathered IT hut 」 WeChat official account , I'll share my computer information knowledge here 、 Theoretical Technology 、 Tool resources 、 The software is introduced 、 The backend development 、 interview 、 A series of articles, such as thoughts on work and some thoughts on life . What you see and what you get , It's all about life . take your time , Work harder , You and I grew up together ...

I set up a technology group , If you want to know more about IT The technology of the industry and the problems in life , Welcome to join the group , Just add my wechat , Note that you can enter the group , We look forward to your participation .

Previous recommendation

One article for you to understand Spring Basic use of framework

 

Java What we need to pay attention to in generics

 

How to be in Java Create elegant objects to improve program performance

 

More pictures , Understand 8 A common data structure

 

dried food !MySQL Optimization principle analysis and optimization scheme summary

 

hardcore !15 Zhang diagram Redis Why so soon?

 

We media people's essential multi platform synchronization 、 More than one article

 

Programmer essential technology website Collection

 

Shallow feather

Message area

Point a praise , Prove that you still love me