Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the entry-level knowledge points of Maven

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the entry knowledge points of Maven". In the daily operation, I believe that many people have doubts about the entry knowledge points of Maven. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the entry knowledge points of Maven?" Next, please follow the editor to study!

1. The basic concept of Maven

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

1.1. Project construction

The project construction process includes [clean up project] → [compile project] → [test project] → [generate test report] → [packaged project] → [deployment project]. These six steps are the complete construction process of a project.

The ideal project build is highly automated, cross-platform, reusable components, standardized, using maven can help us complete the project build process mentioned above.

1.2. Dependency management

Dependency refers to the interdependence between jar packages. For example, when we build a Struts2 development framework, we can't just have struts2-core-2.3.16.3.jar as a jar package. Struts2-core-2.3.16.3.jar also depends on other jar packages. Dependency management refers to the use of Maven to manage the jar packages used in the project. The way of Maven management is to "automatically download the jar packages needed for the project." Uniformly manage the dependencies between jar packages.

1.3.The benefits of using Maven

Maven uses conventions to specify the directory in which java source code must be placed and the directory in which compiled java code must be placed.

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

We just need to define a pom.xml, then put the source code in the default directory, and Maven helps us with other things.

With Maven, you can do highly automated project builds, dependency management (which is the biggest benefit of using Maven), and warehouse management.

II. Maven download

Download address: http://maven.apache.org/download.cgi

After the download is complete, you will get a compressed package, decompress it, and you can see the composition directory of maven.

Maven catalog analysis

Bin: contains scripts run by mvn

Boot: contains the plexus-classworlds class loader framework

Conf: contains settings.xml configuration files

Lib: contains the java class libraries needed by the Maven runtime

Brief introduction of LICENSE.txt, NOTICE.txt, README.txt for Maven version, third party software, etc.

III. Maven installation

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

1 Echo% JAVA_HOME%2 Java-version

2. Decompress apache-maven-3.2.3-bin.zip

Extract the apache-maven-3.2.3-bin.zip, for example, to the following directory (preferably without Chinese)

3. Set the system environment variable: MAVEN_HOME

4. Set the environment variable Path and add% MAVEN_HOME%\ bin to the Path. Be sure to use a semicolon to separate it from other values, as shown in the following figure:

% MAVEN_HOME%\ bin represents the "E:\ apache-maven-3.2.3\ bin" directory

5. Verify whether the Maven installation is successful

Open the cmd window

Enter the "mvn-v" command to view information about Maven, as shown in the following figure:

The presence of such a message indicates that the installation of Maven has been successful.

6. Set the MAVEN_OPTS environment variable (optional configuration)

Because the Maven command actually executes the Java command, you can set the MAVEN run parameters in the form of JAVA command parameters. The MAVEN_OPTS environment variable is used for this purpose.

MAVEN_OPTS

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

Fourth, the simple use of Maven 4.1, Maven project directory convention

MavenProjectRoot (project root)

| |-src |

| |-main |

| | |-java-- the .java file where the project is stored |

| | |-resources-- stores project resource files, such as spring and hibernate configuration files |

| |-test |

| | |-java-- stores all test .java files, such as JUnit test class |

| | |-resources-- stores project resource files, such as spring and hibernate configuration files |

| |-target-- Project output location |

|-pom.xml-used to identify the project as a Maven project

Create the Maven project manually and compile it using Maven

1. Create a project root folder, such as Maven01

2. Create a "pom.xml" file in the Maven01 folder, as shown below:

The pom.xml file is as follows:

4.0.0 cn.gacl.maven.hello hello-first SNAPSHOT-0.0.1

3. 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, then create a main directory in the src directory, and then create a java folder in the main directory, as shown in the following figure:

Create a Hello.java file under the java folder, as shown in the following figure

Write the following code in the Hello.java file:

Public class Hello {public static void main (String [] args) {System.out.println ("Hello Maven");}}

4. To compile Hello.java using Maven, first go to the project root directory, and then use the command "mvn compile" to compile, as shown in the following figure:

After compiling with Maven, a target folder is generated under the root of the project, as shown in the following figure:

5. Use the "mvn clean" command to clear the compilation result, that is, to delete the compiled target folder, as shown in the following figure:

After executing the "mvn clean" command, the target folder will be deleted.

Modify the default storage location of the jar package downloaded from the Maven central warehouse to the local jar

The jar package downloaded from the Maven central warehouse is stored in "${user.home} / .m2 / repository" by default, and ${user.home} indicates the user directory currently logged into the system (such as "C:\ Users\ gacl"), as shown in the following figure.

It is not good for Jar packages to be stored in this location, we want to be able to define the location of downloaded jar packages, so we can set up the storage directory of jar packages when we download them locally.

Create a "repository" folder under the "E:\" directory

Locate the settings.xml file in the apache-maven-3.2.3\ conf directory, as shown in the following figure:

Edit the setting.xml file, as shown in the following figure:

Add the following code

1 E:/repository

In this way, you can download the jar package to our specified E:/repository directory, as shown in the following figure:

The advantage of downloading the jar package locally is that when compiling, it will first look for it from the local jar package, if it exists locally, use it directly, and if it doesn't exist, download it from the central warehouse of Maven. As shown in the following figure:

The first time to execute the "mvn compile" and "mvn clean" commands, Maven will go to the central warehouse to download the required jar packages, but the second time to execute these two commands, because the required jar packages have been stored in the local warehouse, so they can be directly used, thus saving the time to download jar packages from the central warehouse.

At this point, the study of "what are the basic knowledge points of Maven" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report