1. Overview of Maven for Maven Learning


 Maven is currently the most used project management tool, and it is a project of apache. First introduce the maven installation.

1. Download the appropriate maven version from the maven website, here is 3.0.5, the URL is as follows: http://maven.apache.org/download.cgi   . Download screenshots:



 After the download is complete, decompress the apache-maven-3.0.5-bin.zip, and then copy the decompressed folder to the place where maven is to be installed. Here we install it directly in the root directory of the d drive. As shown below:

 

Then put the maven installation directory into the windows environment variable. As shown below:



 After the addition is complete, then put the mavenbin directory in the path of windows. As shown below:

After completing the above operations, even if maven3.0.5 is installed, open the command window and enter mvn -v in the window, you can view some information of maven, as shown below: (Here, make sure that the environment variables of java have been Setting, if the java environment variable is not set, an error will occur. There are many java environment variable settings on the Internet)

 

Let's take a look at the basic use of maven, so as to have an intuitive understanding of maven.

First create a simple maven project, create the following directory D:\item\maven\maven-ch01 in the computer, and create a new pom.xml file in the maven-ch01 directory after the establishment, as shown below:

<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.szzx.maven</groupId>
  <artifactId>maven-ch01</artifactId>
  <version>0.0.1-SNAPSHOP</version>
 </project>

 Then create a new java file in the D:\item\maven\maven-ch01\src\main\java\cn\szzx\maven directory, as shown below:

package cn.szzx.maven;
public class HelloMaven{
	public String sayHello(String name){
		return "hello:"+name;
	}	
}

 At this time, you can compile the class HelloMaven. The way to compile is to open the command line window, enter the directory where the project maven-ch01 is located, and enter the command in it: mvn compile, as shown in the following figure:

 

After executing mvn compile, maven will perform some operations. These operations are mainly for maven to download some packages that compile depends on from the mvn repository. And compile the class HelloMaven, as shown below:


 At this time, there will be an additional folder target in the project maven-ch01 directory, as shown in the following figure:

 

In the target directory of the directory, there is a class directory that stores the HelloMaven.class file of the compiled class HelloMaven.java. This class file is generated by the mvn compile command. Then test the class HelloMaven.java, and create a new test class TestHelloMaven.java in the D:\item\maven\maven-ch01\src\test\java\cn\szzx\maven directory.

code show as below:

 

package cn.szzx.maven;
import org.junit.*;
import static org.junit.Assert.*;
public class TestHelloMaven {
	@Test
 public void testSayHello () {
   
	HelloMaven hm = new HelloMaven ();
   
	String str = hm.sayHello("maven");
   
	assertEquals(str,"hello:maven");
  
}	
}

 Then execute the test on the project maven-ch01, enter mvn test in the command line window, then there will be an error that junit cannot be found, as shown in the following figure: 

 This is because maven does not know where junit is, and we are looking for What is the version of junit? We need to use dependencies in the pom.xml file to configure the junit information. The dependencies indicate the coordinates of the junit package to be loaded, that is, the name and version information. After the preparation is completed in this, maven knows that the program depends on this jar package in use, and then it will be downloaded from the mavn repository. As follows: 

<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.szzx.maven</groupId>
  <artifactId>maven-ch01</artifactId>
  <version>0.0.1-SNAPSHOP</version>
  <dependencies>
  		<dependency>
  			  <groupId>junit</groupId>
			  <artifactId>junit</artifactId>
			  <version>4.10</version>
  		</dependency>
  </dependencies>
  
 </project>

 After the above execution is completed, then execute the mvn test command in the command line window. At this time, the following will appear, go to the warehouse to download junit.

 
 Then something else will be downloaded, and when all dependencies are downloaded, something like this should appear: 
 

 Indicates that a test was performed successfully and there were no errors. At this time, if the test code is modified to the following content. That is, the places painted in red have been modified. Then execute mvn test again,



 At this time, the error message and the location of the error message will be printed, as shown below:



 

At this time, there will be a test report in the D:\item\maven\maven-ch01\target\surefire-reports directory, which will indicate whether the test is completed correctly. If not, what is the error will be here. pointed out inside. After the operation is completed, there will be an additional compressed file in the D:\item\maven\maven-ch01\target directory, as shown in the following figure:



 It types the project mavn-ch01 into a jar package. This jar package can be used elsewhere. In order to test the usage of the jar package, a second mavn project is built here. Its structure is as follows, in the D:\item\maven\maven-ch02 project, and then create a new pom.xml file in it, the content is as follows:

<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.szzx.maven</groupId>
  <artifactId>maven-ch02</artifactId>
  <version>0.0.1-SNAPSHOP</version>
  <dependencies>
  		<dependency>
  				  <groupId>junit</groupId>
					  <artifactId>junit</artifactId>
					  <version>4.10</version>
  		</dependency>
  </dependencies>
  
 </project>

 Then create a new Hello.java class with the following content:

package cn.szzx.maven.ch02;
import cn.szzx.maven.HelloMaven;
public class Hello{
	public String say(String name){
		 HelloMaven hm = new HelloMaven ();
			return hm.sayHello(name);
		}	
}

 

Then go to the mavn-ch02 directory in the command line, execute the command mavn compile command, an error will appear, indicating that the class HelloMaven cannot be found, as shown below:



 At this time, add the jar package code that depends on the maven-ch01 project in the pom.xm file of the project maven-ch02, as shown below:

<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.szzx.maven</groupId>
  <artifactId>maven-ch02</artifactId>
  <version>0.0.1-SNAPSHOP</version>
  <dependencies>
  		<dependency>
  				  <groupId>junit</groupId>
					  <artifactId>junit</artifactId>
					  <version>4.10</version>
  		</dependency>
  		<dependency>
  				  <groupId>cn.szzx.maven</groupId>
					  <artifactId>maven-ch01</artifactId>
					  <version>0.0.1-SNAPSHOP</version>
  		</dependency>
  </dependencies>
  
 </project>

 Then install the jar package of the project maven-ch01 into the local maven repository, on the command line, go to the maven-ch01 directory, and then execute mvn install, as shown in the following figure:



 

In this way, the jar package of the mavn-ch01 project can be placed in the mavn local repository, and the maven local repository is in the following directory by default. As shown below:


 

The above command can put the package made by the project maven-ch01 into the local warehouse. In this way, go back to the maven-ch02 directory and execute the command mvn compile, then it can pass normally.

 

With this function, all projects in the future can be made into modules, and then these modules can be typed into jars, and then depended on other projects. maven brings us great benefits. And we don't need to introduce jar package in maven,

 

 

 

Then we use the mvn package command and enter this command directly at the command prompt. This command can make our project into a package. When maven runs, it is compiled first, then tested, and then packaged.

 

In a company, if the project is put on svn, and a new employee comes to the company, then he needs to get the project from svn, and then proceed and configure, if we have mavn, we don't need to do this, mavn did this for us directly.

 

 

 

mvn compile compile

mvn test test

mvn install installs the jar package to the local repository

mvn clean clears the log.

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705620&siteId=291194637