preface
In our previous development, we often encountered the following problems:
- A project is a project, when the project is too big, difficult to manage;
- The jar packages in the project (that is, our dependency files) need to be moved manually to the Web project
WEB-INF/lib
Directory; - Jars are typically downloaded or packaged by someone else;
- Cumbersome dependencies between individual JAR packages need to be resolved by ourselves;
To solve these problems, we proposed to use project management tools to manage our projects, and this is what we are going to talk about today, Maven.
Using Maven, we can split a large project into multiple projects. In addition, Maven makes it easy to centrally manage individual JAR packages, and it also resolves dependencies between packages.
What is Maven
Maven is a Project management tool that contains a Project Object Model, reflected in the configuration as the POM.xml file. This includes a project lifecycle, a dependency management system, and plugins and goals defined at the project lifecycle stages.
Two of Maven’s core concepts are dependency management and project building.
- Dependency management: to provide the
jar
Unified management of. Maven provides a central repository where we automatically download dependencies after we have added them to the project. - Project building: Maven provides compilation, testing, packaging, deployment, uploading to private servers, and more.
Maven installation
Maven is a Java project, so using Maven must rely on the JDK.
To make sure you have JDK installed on your computer, use the following command.
java -version
Copy the code
If the following message is displayed, our JDK installation is successful (of course, you may not have the same version as me, but if something like the following message is displayed, you are successful too).
After installing the JDK, then install Maven as follows:
- Download Maven, download address: maven.apache.org/download.cg… On Windows, select the second option. On macOS or Linux, select the first option.
- Decompress the downloaded package
- Configuring environment Variables
You need to configure environment variables. One is to create a new system variable to point to the decompressed Path of Maven, and the other is to modify the Path variable environment to add a new Path to the bin directory of Maven.
- MAVEN_HOME: where Maven is stored after the decompression
- Path:
%MAVEN_HOME%\bin
- Verify that the installation is successful
After we configure the environment variables, we can open a new CMD window and use the following verification command to test whether our Maven is successfully installed. If the installation is successful, the following prompt will appear.
mvn -v
Copy the code
Maven directory structure
After installing Maven, the directories and contents are as follows:
- bin
Contains MVN run steps to configure the Java command, prepare the classpath and associated Java system properties, and then execute the Java command.
- boot
Contains only one file and is a class loader framework that provides a richer syntax for easy configuration than the default Java class loader.
- conf
Contains settings. XML, which can be modified to globally customize Maven’s behavior on the machine.
- lib
Contains all of the Java libraries required by the Maven runtime. Maven itself is developed in modules, so there are libraries for different modules and so on. It also includes some third-party dependencies that Maven uses.
Maven configuration
Warehouse Image Configuration
Usually, it is ready to use after installing Maven. However, because the central warehouse server of Maven is located abroad and the Internet speed is slow in China, we had better change the central warehouse to alibaba Cloud image in China.
Open the settings. XML file in the apache-maven-xxx/conf/ directory and add the following configurations under the mirrors node:
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>Ali Cloud public warehouse</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
Copy the code
Local Warehouse configuration
After installing Maven, the local repository is stored under the current user name /.m2/repository by default, but this location is somewhat hidden, so you are advised to customize it to another path:
Open settings. XML in apache-maven-xxx/conf/ and change the following path to your local repository:
<localRepository>/path/to/local/repo</localRepository>
Copy the code
conclusion
Ok, so that’s about Maven, which we use a lot in our daily projects. This article describes what Maven does, how you should install Maven and some common Settings after installing it, and finally explains the directory structure of Maven in detail. In my next blog post, I’ll cover the most common commands in Maven and see what commands people use to get things done.