Maven Study Notes (1) - Getting Started with Maven

First, the basic concept of Maven

Maven (translated as "expert", "expert") is a cross-platform project management tool. It mainly serves project construction, dependency management and project information management based on the Java platform.

1.1. Project construction
  The project construction process includes the steps of [clean project] → [compile project] → [test project] → [generate test report] → [package project] → [deploy project], these six steps are a project the complete build process.
 write picture description here
 The ideal project construction is highly automated, cross-platform, reusable components, standardized, and maven can help us complete the above-mentioned project construction process.

1.2. Dependency management
  Dependency refers to the interdependence between jar packages. For example, when we build a Struts2 development framework, the jar package of struts2-core-2.3.16.3.jar is not enough, and struts2-core-2.3 .16.3.jar also depends on other jar packages. Dependency management refers to using Maven to manage the jar packages used in the project. The way of Maven management is “automatically download the jar packages required by the project, and manage the jar packages in a unified manner. dependencies".

1.3. Advantages
  of using Maven The conventions used in Maven stipulate which directory the java source code must be placed in, and which directory the compiled java code must be placed in. These directories have clear conventions.

  Every action of Maven has a life cycle. For example, executing mvn install can automatically execute the build process of compilation, testing, packaging, etc.

  Just define a pom.xml, and then put the source code in the default directory, Maven handles other things for us

  Using Maven allows for highly automated project builds, dependency management (this is the biggest benefit of using Maven), and warehouse management.

2. Maven download

  Download address: http://maven.apache.org/download.cgi
 After the download is complete, get a compressed package, unzip write picture description hereit, you can see the maven composition directory
 write picture description here
 Maven directory analysis

  • bin: contains scripts run by mvn
  • boot: contains the plexus-classworlds class loader framework
  • conf: contains the settings.xml configuration file
  • lib: contains the java class library required by Maven runtime
  • LICENSE.txt, NOTICE.txt, README.txt brief introduction to Maven version, third-party software, etc.
    LICENSE.txt, NOTICE.txt, README.txt brief introduction to Maven version, third-party software, etc.

3. Maven installation

  1. First, make sure that the JDK has been installed on the computer (the version of jdk 1.6+ is required), configure the environment variables of the JDK, and use the following two commands to check the installation of the JDK.
  

Echo %JAVA_HOME%
Java -version

write picture description here
 2. Unzip apache-maven-3.2.3-bin.zip

  Unzip apache-maven-3.2.3-bin.zip, for example, unzip it to the following directory (the unzip directory should not contain Chinese)
  write picture description here
 3. Set the system environment variable: MAVEN_HOME
  write picture description here
 4. Set the environment variable Path, put %MAVEN_HOME%\ When adding bin to Path, be sure to use a semicolon; it is separated from other values, as shown in the following figure:
 write picture description here
 %MAVEN_HOME%\bin represents the "E:\apache-maven-3.2.3\bin" directory

 5. Verify that the Maven installation was successful

  Open the cmd window and
  write picture description here
 enter the "mvn -v" command to view the relevant information of Maven, as shown in the following figure:
 write picture description here
 If such information appears, it means that the installation of Maven has been successful.

 6. Set the MAVEN_OPTS environment variable (optional configuration)

  Since the Maven command actually executes the Java command, the MAVEN running parameters can be set by means of the JAVA command parameters. The MAVEN_OPTS environment variable is used for exactly this purpose

  MAVEN_OPTS

  -Xms128m -Xmx512m, respectively set the minimum and maximum memory of the JVM, as shown in the following figure:
  write picture description here

Fourth, the simple use of Maven

4.1 , Maven project
directory convention spring, hibernate configuration files | |—-test | | |—-java ——Store all test .java files, such as JUnit test classes | | |—-resources ——Store project resource files, such as spring, hibernate configuration files |— -target ——Project output location |——-pom.xml ——Used to identify the project as a Maven project








4.2. Manually create a Maven project and use Maven to compile
  
  1. Create a project root folder, such as Maven01
write picture description here

2、在Maven01文件夹中创建“pom.xml”文件,如下图所示:
![这里写图片描述](https://images0.cnblogs.com/blog/289233/201312/30175341-e03f57eeea3940209064d8f5b33127ab.png)
pom.xml文件中的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--所有的Maven项目都必须配置这四个配置项-->
    <modelVersion>4.0.0</modelVersion>
    <!--groupId指的是项目名的项目组,默认就是包名-->
    <groupId>cn.gacl.maven.hello</groupId>
    <!--artifactId指的是项目中的某一个模块,默认命名方式是"项目名-模块名"-->
    <artifactId>hello-first</artifactId>
    <!--version指的是版本,这里使用的是Maven的快照版本-->
    <version>SNAPSHOT-0.0.1</version>
</project>
  1. To write Java class files, the Maven project stipulates that all *.java files must be placed in the java directory under the main directory under the src directory, create a src directory in the root directory of the Maven01 project, and then create a main directory in the src directory, Create a java folder in the main directory, as shown in the following figure:
    write picture description here
    Create a Hello.java file in the java folder, as shown
    write picture description here
    in the Write the following code in the Hello.java file:
public class Hello{
     public static void main(String[] args){
         System.out.println("Hello Maven");
     }
 }

4. Use Maven to compile Hello.java, first enter the project root directory, and then use the command "mvn compile" to compile, as shown in the following figure:
write picture description here
write picture description here
After compiling with Maven, a target folder will be generated in the project root directory, as follows As shown in the figure:
write picture description here
Open the target folder, you can see that there is a classes folder in it, as shown in the figure below:
write picture description here
The classes folder stores the .class files generated after the compilation is successful, as shown in the figure below:
write picture description here
5. Use "mvn clean" ” command to clear the compilation result, that is, delete the target folder generated by compilation, as shown in the following figure:
write picture description here
After executing the “mvn clean” command, the target folder will be deleted.

5. Modify the default storage location of the jar package downloaded from the Maven central repository to the local

  The default storage of the jar package downloaded from the Maven central repository to the local " u s e r . h O m e / . m 2 / r e p O s i t O r and middle {user.home} represents the user directory currently logged in to the system (such as "C:\Users\gacl"). As shown in the figure below,
  write picture description here
  write picture description here
   it is not very good to store the Jar package in this location. We hope to be able to define the storage of the downloaded jar package by ourselves. location, so we can set the storage directory of the jar package when it is downloaded to the local.

  Create a "repository" folder in the "E:\" directory
  write picture description here
  Find the settings.xml file in the apache-maven-3.2.3\conf directory, as shown below:
  write picture description here
  Edit the settings.xml file and add the following code

<localRepository>E:/repository</localRepository>

write picture description here
In this way, the jar package can be downloaded to the E:/repository directory we specified, as shown in the following figure:
write picture description here
 The advantage of downloading the jar package to the local is that when compiling, it will be found first from the local jar package. If it exists, use it directly. If it does not exist, download it from Maven's central repository. As shown in the following figure:
 write picture description here
 When the two commands "mvn compile" and "mvn clean" are executed for the first time, Maven will go to the central repository to download the required jar package, and when the two commands are executed for the second time, due to the required The jar package has been stored in the local warehouse, so it can be used directly, which saves the time to download the jar package from the central warehouse.

  The above is a simple introduction to Maven.
  

Guess you like

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