Start learning Today Maven,maven is a project management and construction tool, very convenient to use, with Maven we can easily compile the project, package run, etc., MAVEN can easily help us manage the project report, build the site, manage jar files, and so on.
Maven Download
First you need to download maven on MAVEN's website:
http://maven.apache.org/download.cgi#
Here i download to the E:\maven\apache-maven-3.3.3 directory
Environment variable Configuration
After the download is complete, you need to configure the environment variables to configure the bin directory under the MAVEN project root directory to the PATH environment variable
After configuring the PATH environment variable, check the following, whether the configuration is successful, enter at the command line: mvn-version
You can see the MAVEN version number used here.
Below is a look at the role of each directory of MAVEN projects:
Create a MAVEN project manually create a pom.xml file
First, in the project to create a Pom.xml file, the schema can be used in spring or hibernate and other open source files to search for Pom.xml (because these are used by MAVEN to manage), copy them over.
<?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"></project>
Note here that the root tag of the Pom file for each of our projects is "project", and we must first configure the following four configurations for each project:
1.modelVersion: Refers to the version of the model, the current value is fixed 4.0.0
2.groupId: Specifies the project name of the current project and the same as the package name
3.ARTIFACTID: Specifies which module in the current project, typically using the form "project name _ Module name".
4.version: Represents the version of the current project
<?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>com.maven.test01</groupId> <artifactid>Test01_hello</artifactid> <version>SNAPSHOT-0.0.1</version></Project>
Here I use the snapshot is a snapshot version, is a special version, will be explained in detail later.
Create a Java file
The next step is to create a plain Java file, and it's important to note that in Maven, our Java files are stored in directories such as "Src/main/java".
Here I create a new Hello.java file:
package com.maven.test01;publicclass Hello { publicsayHello() { return"hello world"; }}
After the pom.xml and the Java files have been created, we can start compiling our MAVEN project, enter the directory where the Pom.xml file is located in command line mode, enter MVN compile
PS: After entering MVN compile, maven will download the required files from its central repository, which may fail, depending on the individual's network quality.
When the MAVEN compilation is complete, a target folder is generated, which contains the class file generated by the compilation. The default maven download is stored under the "C:\Users\admin.m2" path.
Setting up the Maven warehouse location
You can see that MAVEN defaults to storing the files downloaded from the central repository in the C drive, which can be specified in the storage location.
1. Create a new directory "E:\maven\mavenrepo" to store the local repository
2. Copy the repository downloaded from the Internet in the first step to this directory
3. Change the settings.xml file of the apache-maven-3.3.3\conf directory for the downloaded MAVEN project
4. Copy the settings.xml file to the E:/maven/mavenrepo directory at the same time:
PS: Whether it is when we compile, package, or run Maven project, for the required files, MAVEN will first find in the local repository, if the local repository is not found, will go to the central warehouse to download.
Write a test project
OK, so far the Hello class has been compiled, and the following is written in test for testing the SayHello method in the class. Test classes are similar to normal classes, and all test classes must be placed in the "Src/test/java" directory.
Testhello.java
package com.maven.test01;import org.junit.*;importstatic junit.framework.Assert.*;publicclass TestHello { @Test publicvoidtestHello() { new Hello(); assertEquals(hello.sayHello(),"hello world"); }}
Here we use JUnit, in the usual way, to manually download JUnit jar files and then manually copy the project catalog, using MAVEN, You only need to configure JUnit in the Pom.xml file, and because our test class relies on JUnit files, we need to configure its dependencies. Add the following code to the Pom.xml file:
<dependencies> <dependency> <groupId>Junit</groupId> <artifactid>Junit</artifactid> <version>4.10</version> <scope>Test</Scope> </Dependency> </dependencies>
Then run the "mvn Test" command to test our Hello class.
At this point, the birthday test report, such as the target directory, and the test class will be compiled, placed in the target folder.
MVN clean
Next, execute the "mvn clean" command to see the effect:
After executing the command, it is found that the target folder generated during compilation and testing has been deleted.
We can also execute this command: "MVN clean Package", where the clean command is executed first and then the Package command is executed.
At this point, all files in target are generated by the Execute Package command, and it can be found that the "compile", "test", and "pack to jar" Tasks are automatically completed when the packages command is executed.
If you need to package our project into a local warehouse, you can execute the "mvn clean install" command.
Create a second project
So what's the use of packaging test01 to a local repository?? Next we create a second project to illustrate this point. Create a new project for test002.
Under the test002 root directory, create a new pom.xml
<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>com.maven.test02</groupId> <artifactid>Test02_second</artifactid> <version>SNAPSHOT-0.0.1</version> <dependencies> <dependency> <groupId>com.maven.test01</groupId> <artifactid>Test01_hello</artifactid> <version>SNAPSHOT-0.0.1</version> <scope>Compile</Scope> </Dependency> </dependencies></Project>
You can see that the test001 is referenced here, and the dependent Groupid,artifactid is defined in the test001 pom.xml file.
Create Secondhello.java
Create a new Secondhello.java in the "Src\main\java" directory with the following contents:
package com.maven.test02;import com.maven.test01.Hello;publicclass SecondHello { publicvoidrunHelloSay() { new Hello(); hello.sayHello(); }}
You can see that the Hello.java class in the TEST001 project is referenced here, but I did not import the relevant jar file, but I just configured the corresponding dependency in the Pom.xml file to use it.
You can see that it is convenient to use MAVEN to manage the project, and it is convenient to execute the specified command in accordance with its constraints. Well, today's study on MAVEN is here, I hope you can enjoy it.
SOURCE download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MAVEN Environment Setup and manual maven project creation