maven's introductory notes

Posted by bodge on Tue, 30 Jul 2019 17:27:09 +0200

Installation of JDK( Baidu Disk Extraction Code: rus0  | Official website

Configuring environment variables

CTRL+R enters sysdm.cpl and then chooses Advanced - > Environment Variables (or right-click the computer - > Properties - > Advanced System Settings - > Environment Variables)

New variable name: JAVA_HOME variable value: JDK installation path

New variable name: classpath variable value:;% JAVA_HOME% Lib

Edit path add;% JAVA_HOME% bin

Verification: CTRL+R input cmd

C:\Users\Administrator>java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13) 
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode) 
C:\Users\Administrator>javac -version
javac 1.8.0_101

II. Installation of Maven( Baidu Disk Extraction Code: drtx | Official website)

Configuring environment variables

CTRL+R enters sysdm.cpl and then chooses Advanced - > Environment Variables (or right-click the computer - > Properties - > Advanced System Settings - > Environment Variables)

New variable name: M2_HOME variable value: Maven path

Edit path add;%M2_HOME%\bin

Verification: CTRL+R input cmd

C:\Users\Administrator>mvn -version
Apache Maven 3.6.1 
Maven home: D:\Program Files\maven-3.6.1\bin\.. 
Java version: 1.8.0_101, vendor: Oracle Corporation, runtime: D:\Program Files\Java\jdk1.8.0_101\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows" 

Change Local Warehouse Location and Domestic Source

Edit settings.xml file in conf directory under maven directory

Add Local Warehouse Location
<localRepository>D:/Code/repository</localRepository>
find<mirrors>Label and add Aliyuan under this label:
<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirror>

The first maven program

maven project structure

There are also resources folders and pom.xml(Project Object Model) files under src

(2) Coding

Under D: Documents Code MVN maven01 src main java com dvvnv maven01 > create HelloWorld.java class, which is as follows:

package com.dvvnv.maven01;
public class HelloWorld{
    public String hello(){
        return "Hello World!";
    }
} 

Create HelloWorldTest.java class under D: Documents Code MVN maven01 src test java com dvvnv maven01 > as follows

package com.dvvnv.maven01;

import org.junit.*;
import org.junit.Assert.*;

public class HelloWorldTest{
	@Test
	public void testHello(){
	    Assert.assertEquals("Hello World!",new HelloWorld().hello());	
	}
}

Create a pom.xml file under the project root directory (at the same level as the src directory):

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dvvnv.maven01</groupId>
    <artifactId>maven01-modeltest</artifactId>
    <version>0.0.1SNAPSHOT</version>

    <dependencies>
	    
	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.10</version>
        </dependency>
    </dependencies>
</project>

Compiling, testing and packaging

mvn compile

mvn test

mvn package

Generating mvn project structure with mvn own commands

IV. Other commands of mvn

Clear the files generated by the mvn compilation test package
mvn clean
 Install the jar package for the packaging project into the classpath environment
mvn install

If Item 2 is to use the method or attribute of Item 1, mvn install must be executed in Item 1 directory, and Item 1 coordinates must be added to the pom.xml file of Item 2:

<groupId>com.dvvnv.maven01</groupId>
<artifactId>maven01-modeltest</artifactId>
<version>0.1SNAPSHOT</version>

========================= End of Notes======================

Topics: Maven Java xml Apache