Maven (1): Introduction and installation of Maven


Migration of Notes in 2022

Introduction

What is Maven?

It can be translated as the accumulation of knowledge, or as an "expert".

Maven is an extremely successful cross-platform project management tool and a top open source project of Apache.

What are the functions of the so-called project management tools?

One of the core uses is to help us automate construction , from cleaning, compiling, testing, generating reports, to packaging, deploying, and generating project documentation.

Maven can help us do it well, we only need to configure Maven for the project, and then complete the above process through some simple instructions. For example mvn clean install.

Handing over these tedious tasks to Maven has greatly liberated our hands.

In addition, Maven is cross-platform, no matter what platform it is on, the instructions of Maven are the same.

Based on the above characteristics, Maven can also help us standardize the construction process. Everyone uses Maven's rules to build their own projects.

Another core use is for dependency management .

Dependencies are the third-party class libraries that the project needs to use, and these class libraries will be introduced into the project through dependencies.

For large projects, it is very common to reference dozens or even hundreds of third-party open source libraries. Manually managing such a huge number of dependencies is impossible and exhausting. Version inconsistencies, version conflicts, dependency conflicts, etc. are doomed to be unrealistic to solve manually.

So Maven provides an excellent dependency management solution.

It accurately locates each component ( artifact ) through a coordinate system, that is to say, through a set of coordinates, Maven can find any Java class library (such as a jar file).

Maven provides a free central repository for Java developers all over the world, where you can find any popular class library. Through some Maven derivatives (such as Nexus ), we can quickly search and download it, which is very convenient.

In addition, Maven has established rules for project directory structure and test case naming methods. As long as these mature rules are followed, a lot of learning costs will be saved when switching between projects. It can be said that convention is better than configuration.

Similar products of Maven

The project management tools in the Java field are not only Maven, but also old-fashioned Make, Ant, etc. Even an off-the-shelf IDE.

Make

Make is the earliest build tool.

The core is a Makefile, and users write various building rules in the Makefile.

Make is too expensive to learn and use, and it is not cross-platform.

Ant

Another Neat Tool, another neat tool.

Ant was first used to build the famous Tomcat. One of the author's motivations for creating it is because he can't stand the syntax format of Makefile.

Think of Ant as Java's version of Make, so it's cross-platform.

In addition, Ant uses build.xml to define build scripts, which is more friendly.

Early Ant did not have dependency management, and later it was possible to manage dependencies with the help of lvy. But Ant has no central repository.

other

I saw a very interesting sentence: The father of C++ once said that there are only two types of computer languages, one is scolded every day, and the other is not used.

The most efficient way to use Maven is always the command line . IDEs have inherent flaws in automated builds. Although IDEs such as IntelliJ IDEA have been working hard to better integrate Maven, it takes time.

The documentation on the Maven official site is very messy, very messy.

Maven installation and configuration

Maven download address: https://maven.apache.org/download.html

The version of Maven is strongly corresponding to the version of jdk. You can go to https://maven.apache.org/docs/history.html to see the jdk version corresponding to each version of Maven.

Maven 3.3 requires JDK 1.7 or above

Maven 3.2 requires JDK 1.6 or above

Maven 3.0/3.1 requires JDK 1.5 or above

After decompression, configure the bin directory as an environment variable, and then enter it in cmd mvn -v. If it returns normally, the installation is successful.

The installation process on windows and linux is basically the same, which is the process of decompression + configuration of environment variables.

Regarding the upgrade of Maven, let me mention this a little bit.

In Windows, the upgrade is very simple, download the new version of Maven, after decompression, modify the environment variable to the bin directory of the new version of Maven. Of course, the configuration file or something needs to be modified by yourself.

In Linux, the upgrade is the same. Unzip the new version of Maven + configure environment variables. There is a little trick here, that is, when you configure environment variables, you can make a symbolic link into an environment variable, so that when you upgrade later, you can modify the pointing of the symbolic link.

Installation directory analysis

The directory that comes out after Maven decompresses is like this:

bin/
boot/
conf/
lib/
license
notice
readme.txt

bin/ : Contains the scripts run by mvn; the contents of mvn and mvnDebug are basically the same, the difference is that mvnDebug has an additional MAVEN_DEBUG_OPTS configuration, which is used to enable debug when running Maven. This directory also contains the m2.conf file, which is the configuration file for classworlds.

boot/ : Contains only the plexus-classworlds.jar file, which is Maven's own class loader framework, and Maven uses this framework to load its own class library. But for general Maven users, you don't need to care about this.

conf/ : core configuration file settings.xml. Globally control some configurations of Maven, such as central warehouse address, local warehouse address, etc.;

lib/ : Some class libraries required by Maven runtime.

Skill

The settings.xml in the Maven installation directory is a global configuration file, and all users on the entire machine will be directly affected by this configuration;

~/.m2/settings.xml in the user directory is a user-wide configuration file, and only the current user will be affected by this configuration.

Generally speaking, it is recommended to use user-level settings.xml.

Regarding the priority of taking effect: pom.xml > user settings > global settings .

When applying the configuration, different configuration files will be merged. If there are duplicate configurations, the configuration with higher priority will override the configuration with lower priority.

Try not to use the Maven that comes with the IDE. For example, idea generally installs one under plugins\maven\lib\maven3 in the installation directory.

It is more troublesome to use and upgrade, it is better to install one yourself.

Guess you like

Origin blog.csdn.net/wlh2220133699/article/details/131258486