Suppose the company is developing a new Web project, using the current popular struts2, spring, and mybatis to develop new projects. So the first thing to do next is to download the jar packages for each framework. It is common practice to download the Struts2 jar package from Struts2 's official website, download the Spring jar package to Spring's official website, and download the MyBatis jar package and some other related third-party jar packages to the MyBatis website.
The project has not started, our Lib directory has Bai jar package, useful, useless, each version conflicting. No conflict, delete it, do not know which of the links between, do not delete it, the project is not bloated. Fortunately, thanks to the great God of the open source community, we have provided maven, ANT, Gradle Excellent project management and project building tools that make it easier for us to manage and build our projects.
The following will learn about Maven, the main points to learn:
1. Maven Quick start;
2. maven Core knowledge;
3. MAVEN builds a Web project.
This article focuses on MAVEN and its environment building.
First, Maven introduction and environmental construction 1. The concept of Maven
MAVEN is a software project management tool that enables you to manage project builds, reports, and documents through a small section of information that is based on the Project object Model (POM).
In simple terms, MAVEN can help us manage projects more effectively.
Download maven, need to visit: maven.apache.org
Under the list download option on the left:
The Maven3.3 version requires more than JDK1.7 support.
After downloading, unzip to view the appropriate directory:
View Bin directory: (Bin directory contains mvn run script)
The bin directory contains the run script for MVN. This script is called when you enter MVN on the Windows command line.
M2.conf This is a configuration file.
To view the boot directory: (The boot directory contains a framework for a class loader)
The boot directory contains a framework for a classloader, which Maven uses to load its own class library.
View conf directory: (conf is the profile directory)
Conf is the configuration file directory, such as the setting.xml we used to use later
Lib Directory: (contains the class library that Maven normally uses, in addition to its own, also contains a third-party dependent class library)
2. MAVEN environment variable Configuration
Environment variables are divided into user variables and system variables.
If your computer is used by multiple people, you should set it in the user variable in order not to affect others. (User variable: A variable that belongs to each user of the computer. )
If your computer is private, you can set it in system variables. (System variable: A variable shared by all computer users.) )
- M2_home: The variable value is set to the MAVEN installation directory;
- Path: set to%m2_home%\bin;
How do I verify that the environment variable is configured successfully?
Open cmd command prompt dos: input mvn-v
If you can view the relevant version information, the environment variable is configured successfully.
Second, build the MAVEN version of Hello World
Get to know MAVEN's directory structure first:
Pom.xml
Note:
The value of GroupID is the package name of the project
The value of the Artifactid is the module name, which is generally recommended to use the project name
The value of version is that the revision name is typically a snapshot
A small example of using MAVEN builds:
1: Build directory structure using MAVEN conventions
2: Build the project according to the agreed directory structure
3: Write a simple test code
4: Configure the Pom configuration file
5: Run maven basic command management corresponding small example
6: Solve the mistakes made in the build project according to the instructions of the command, and understand that each command actually did those specific things.
Iii. maven Common Build commands
- Mvn-v: View maven version
- MVN compile: Compiling
- MVN Test: Testing
- MVN Package: Packaging
- MVN Clean: Clears the MAVEN-generated target directory (target contains compiled bytecode files and test reports for the project)
- MVN Install: Installing the generated jar into the local warehouse
The process of MAVEN compiling code:
1. Execute the MVN compile command to compile the Java code file
2. If you find other package files (jar packages) referenced in the code
3. First go back to the core configuration file Pom.xml file to find the appropriate configuration dependencies
4. Find the Jar package resource in the local repository based on configuration dependencies
5. If the local does not exist, then it will be connected to the MAVEN central repository to find
Iv. automatic creation of the directory skeleton
There are two ways to create a directory skeleton.
MAVEN provides us with the archetype plugin for creating a directory skeleton that conforms to the MAVEN rules.
According to MAVEN regulations, in:
- MVN archetype:generate: Prompt for version after running the command, Groupid,artifactid,version,package
- MVN archetype:generate-dgroupid: Organization name, company domain name anti-write + project name-dartifactid: Project name-module name-dversion: Version-dpackage: Package name. Created once.
Mode 1:
Mode 2: Set up all the parameters directly, build the MAVEN directory skeleton
V. Coordinates and warehouses in Maven
- The concept of coordinates: components
The only identification of a component in the Maven world is generally made up of three parts of Groupid/artifactid/version, and the author suggests that the Artifactid and project names be consistent so that they are clearer and easier to find.
Place a variety of dependencies. Warehouses are divided into two types: local warehouses and remote warehouses.
The local repository cannot find the artifact we need, it will go to the central repository, find it and download it to the local repository.
C:\apache-maven-3.3.9\lib\maven-model-builder-3.3.9\org\apache\maven\model\pom-4.0.0.xml-- MAVEN provides us with super Pom all of our pom will record this pom there is a global central repository, and the central warehouse has a resource--jar package for almost all open source projects.
MAVEN's central server is placed abroad, and sometimes we can not access the external network, the country also has his mirror warehouse, so that better access.
Apache-maven-3.3.9\conf\settings.xml This file is configured with the image of the information, you can configure the domestic image file path, so that the faster and better access.
- Change the location of the warehouse:
Maven downloads resources from the Remote central Library by default in the C:\Users\Administrator\.m2\repository directory, for security, generally do not choose to put in the C-disk can modify the local repository directory, designated to a more secure directory, in addition, Setting.xml This file also needs to be saved, this is to update the version without having to re-modify the configuration file.
Vi. Installing the Maven plugin in Eclipse and creating a MAVEN project
If your computer is eclipse4.0 or myeclipse, you won't have to install the Maven plugin anymore.
To see if your Eclipse installs plugins:
Eclipse Preferences to see if there is a MAVEN option.
Eclipse runs on the JRE by default, and the Maven plugin requires JDK support.
Steps:
- Download the Maven plugin, unzip it into Eclipse's dropins or online update (HTTP://M2ECLIPSE.SONATYPE.ORG/SITES/M2E, M2e-extras for extensions)
- Add JDK in Eclipse.ini
+v+
C:\Program Files\java\jdk1.7.0_75\bin\javaw.exe
- Modify MAVEN's Maven directory and setting location in the eclipse-option.
- Create a MAVEN project
- Run as-->build as---in goals Complile-->run
- If-dmaven.multimoduleprojectdirectory error is reported, configure in option-->java-->installed JREs- dmaven.multimoduleprojectdirectory= $M 2_home
Vii. life cycle and plugins for Maven
- The complete project build process includes:
Clean, compile, test, package, integrate test, validate, deploy.
Each of these steps corresponds to a plugin to complete.
- Maven life cycle: Clean, compile, test, package, install
- The life cycle of Maven is divided into three separate lifecycles (each life cycle is divided into several phases, with Defaul being the core):
- Clean: Cleanup Project
- Default: Build Project
- Site: Build Project site
- The Clean life cycle contains the stages:
Pre-clean: Perform pre-cleanup work
Clean: Cleans up all the files that were built last time
Post-clean: Performing a clean-up file
- Default Build project (most core)
The default life cycle contains more stages than the compile, test, package, install, and more commonly used
- Site Build Project sites:
-Pre-site the work to be done before building the project site
-Site document for project generation
-Post-site the work to be done after building the project site
-Site-deploy publish the generated site to the server
Note that Maven's life-cycle commands are executed sequentially, and Maven runs automatically by default if no one executes in an artificial order.
Project management tool--maven