1 Introduction to maven
Official website link
1.1 stages in software development
Demand analysis: analyze the specific functions completed by the project, what requirements are there and how to implement them.
Design stage: according to the analysis results, what technology is used in the design project and solve the difficulties.
Development stage: coding to realize functions. Compile code and test yourself.
Test stage: professional testers test whether the function of the whole project meets the design requirements. Make a test report.
Release phase: package the project and give it to users according to the project.
1.2 what can Maven do
1) The automatic construction of the project helps developers to compile, test, package, install and deploy the project code.
2) Manage dependencies (manage the various jar packages used in the project).
Dependency: other resources that need to be used in the project, most commonly jar packages. For example, the project should use mysql driver. Let's say that the project depends on mysql driver.
1.3 how to manage dependencies without maven
To manage the jar package, you need to download a jar separately from the network
You need to select the correct version
Manually handle the dependencies between jar files, such as the class of b.jar to be used in a.jar.
1.4 what is maven
maven is an open source project of apache foundation, which is developed using java syntax.
maven is an automated build tool for projects. Manage project dependencies.
Each link in the construction process: cleaning, compiling, testing, reporting, packaging, installation and deployment
1.5 concepts in maven
1)POM
2) Agreed directory structure
3) Coordinates
4) Dependency management
5) Warehouse management
6) Life cycle
7) Plug ins and targets
8) Inherit
9) Aggregate
1.6 maven tool acquisition and installation
Official website download link
Installation:
1) Determine JAVA_HOME specifies the installation magic of jdk if there is no JAVA_HOME, you need to create it in the environment variable of windows, and its value is the installation directory of jdk.
Directory structure after maven decompression
2) Unzip the installation files and unzip them into a directory without Chinese or spaces.
3) Add the bin path in the maven installation directory to the path.
4) Test the installation of maven and execute mvn -v on the command line
Other installation methods of maven:
1) Determine Java_ Is home valid
2) In the environment variable, create one called M2_HOME (or MAVEN_HOME), whose value is the installation directory
3) In the path environment variable, add% M2_HOME%\bin
4) Test the installation of maven and execute mvn -v from the command line
2 Maven's core concepts
2.1 agreed directory structure
The directory structure used by most people in the maven project is called the agreed directory structure.
A maven project is a folder. For example, there is a project called Hello
Hello Project folder \src \main Main program directory (code and configuration file for completing project functions) \java Source code (package and related class definitions) \resources configuration file \test Place test program code \java Test code junit \resources Configuration files required by the test program \pom.xml maven Configuration file, core file
maven can be used independently: create projects, compile code, test programs, package, deploy, etc.
maven and idea are used together: coding, testing, packaging and so on are realized through idea and maven.
Create the file directory structure as above.
└─Hello │ pom.xml │ └─src ├─main │ ├─java │ └─resources └─test ├─java └─resources
Create a package under main/java, and then write a java program.
package com.tt.stu; public class HelloMaven{ public int addNumber(int n1,int n2){ System.out.println("HelloMaven addNumber"); return n1+n2; } public static void main(String args[]){ HelloMaven hello = new HelloMaven(); int res = hello.addNumber(10,20); System.out.println("stay main Method, execute hello Method of="+res); } }
Then we go to the project directory, that is, the Hello directory, open the command line window and use the command
mvn compile
You can complete the compilation of the project. You may need to download some files for the first compilation.
2.2 POM
POM: Project Object Model. maven treats the project as a model. Operating the entire model is operating the project. maven through POM XML file realizes project construction and dependency management
<!-- pom.xml --> <?xml version="1.0" encoding="UTF-8"?> <!-- project Is the root tag, followed by the constraint 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"> <!-- pom The version of the model is 4.0.0 --> <modelVersion>4.0.0</modelVersion> <!-- coordinate --> <groupId>com.tt.stu</groupId> <artifactId>ch01-maven</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project>
2.3 coordinates
The coordinate composition is groupid, artifactld and version. The concept of coordinates comes from mathematics.
Coordinate function: the function of determining resources is the unique identification of resources. In maven, each resource is a coordinate, and the coordinate value is unique. gav for short
<groupId>com.tt.stu</groupId> <artifactId>ch01-maven</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> groupId: Organization name, code, identification of company, group or unit. This value is often used to invert the company domain name. artifactId: Project name, if groupId If there are items, the current value is the sub item name. The project name is unique. version: Version, the version number of the project, and the number used. Three. For example: major version number.Minor version number.Minor version number. Note: the version number has -SNAPSHOT,Indicates a snapshot, not a stable version. packaging: Types of project packaging, including jar, war, ear, pom Wait, the default is jar
gav is used in the project
1) Each maven project needs to have its own gav
2) To manage dependencies, you need to use other jar s and gav as the identity.
2.4 dependency
Dependency: other resource jar s to be used in the project
Dependency needs to use maven to represent dependency and manage dependency. Use dependency and gav together to complete the use of dependency
Need to be in POM In the XML file, use dependencies and dependency, and gav to complete the dependency description.
Format:
<dependencies> <!-- journal --> <dependency> <groupId>log4j</goupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- mysql drive --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.16</version> </dependency> </dependencies> maven use gav As an identification, Download dependent from the Internet jar,Download to this computer by maven Manage the information used by the project jar
Dependency on POM XML under the project tag.