What is Maven and what does it do?

Maven is a project management tool with two main functions: project building and dependency management. Project construction is compile, test, integration, automation, it’s very convenient to rely on management function, as long as the current project relies on components (jar, war, etc.) to pom configuration file, you can import the corresponding component automatically from the warehouse and component depends on other components, different maven project share a component warehouse, Projects reference builds in the repository to avoid repeated downloads.

How to install Maven?

The first step is to install the JDK. The installation of JDK and the configuration of environment variables need to be compiled and run during the construction of maven project, which requires javac.exe and java.exe.

Step 2 Download the maven. Zip package (.tar.gz for Linux) from maven.apache.org, decompress the package to the desired directory, and configure environment variables MAVEN_HOME and PATH. MAVEN_HOME is the root directory of Maven. Add %MAVEN_HOME%\bin to PATH. After configuration, run CMD to test whether MVN -version is valid. (If the MVN command does not work after restart, replace the PATH in PATH with the PATH of MAVEN_HOME and delete MAVEN_HOME.)

How is Maven used?

Maven integration will not be covered in the IDE environment, explaining the manual way to create the project to execute the MVN command to complete the build.

Step 1 Create a project create a folder in the specified location command is the project name, create a file within the folder pom. XML, add the following content to pom. XML:

The project p = = 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

4.0.0

com.helloworld

helloworld-first

0.0.1 – the SNAPSHOT

Maven Hello World Project

GroupId, artifactId, version are the basic coordinates of the project. The project is built as a dependency of other projects, which is located by coordinates. Therefore, Maven requires that all projects should have coordinates. GroupId is the inverted domain name and ends with the total project name. ArtifactId is recommended as the total project name-module name to facilitate the classification of components of different modules in the same project in the file system. Version indicates the version number of the current project, version suffix snapshot indicates the unstable version, and name indicates the project name. The component name composition is artifactiD-version.package

Maven will find the main code in the SRC /main/ Java/directory. Create the SRC /main/ Java folder and com.helloworld.first package in the Java folder. Create first.java under first. Write the main code as follows:

package com.helloworld.first; public class First{ public String sayHello(){ return Hello Maven! ;

} public static void main(String[] args){

System.out.println( new First().sayHello());

}

}

Step 3 Compile and run:

MVN clean compile Clears /target and compiles

MVN exec: Java – Dexec mainClass = com. The helloworld. First. The first run no arguments

MVN exec: Java – Dexec mainClass. = com vineetmanohar. The module. The Main – Dexec. Args = arg0 arg1 arg2 have and run (not required)

The fourth step is to write test code package will not package the test code, only test whether the program is correct.

Start by adding JUnit’s standard unit test dependencies to pom.xml:

The project p = = 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

4.0.0

com.helloworld

helloworld-first

0.0.1 – the SNAPSHOT

Maven Hello World Project

  

  

junit

junit

4.7

test

  

  

< span style = “max-width: 100%; clear: both; min-height: 1px;

Maven: SRC /test/ Java: com.helloworld. First: firsttest.java: com.helloworld. First: firsttest.java: com.helloworld.

package com.helloworld.first; import static org.junit.Assert.assertEquals; import org.junit.Test; public class FirstTest{ @Test

public void testSayHello(){

String result = new First().sayHello();

assertEquals(Hello Maven! ,result);

}

}

Step 5 unit test

mvn test

“Archetype :generate” allows you to create a project from MVN archetype:generate. You can write your own archetype if necessary.

What are the Maven project build commands?

MVN clean Clean /target(that is, the compiled or packaged folder)

MVN compile compile

MVN test Unit test (perform compilation, test)

MVN package (perform compilation, test, package)

MVN install publishes to the repository (performs compilation, testing, packaging, and publishing)

How do I find component versions?

Find center warehouse building https://search.maven.org/, artifactId enter name can search to the related project and version of the company.

What is a Maven repository?

Maven depots are divided into remote depots, private depots, and local depots. The central depots are Maven’s default remote depots, while the private depots are built on the LOCAL area network to reduce bandwidth usage. The images are consistent with the corresponding remote repository build, but some can speed up the download. By default, local repositories are under ~/.m2/repo. Settings for repositories and mirrors can be found in settings.xml, or you can change the location of local repositories in settings.xml.

What do Maven’s POM.xml and settings.xml do?

Pom. XML is the configuration of the current project. In the root directory of the project, it mainly configures the coordinates (groupId, artifactId, Version, etc.) and dependencies, etc.

Settings. XML is Maven configuration, including global and user Settings. The global Settings are located in Maven /conf/, and the user Settings are copied to ~/.m2/. You are not advised to modify the global Settings. In this way, you do not need to modify the Settings for project upgrade.

Settings allows you to configure the proxy, remote repository, mirror, and local repository location.