Introduction to Maven structure and configuration method

1. What is Maven

Maven is an open source project under Apache, a tool for managing and building java projects.

What is it for?

For example, if I wanted the IOUtils package before, I had to go to the website to download it and then import it.

When there are many jar packages, is it too troublesome to export them one by one? And to upgrade a package, the related dependent packages also need to be upgraded, which is even more troublesome.

So there is Maven, which can manage package dependencies.

You don’t need to create a lib package step by step, copy it into the package and click add as library...

Just add a piece of code like this to pom.xml:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

This will import the logback package version 1.2.3.

In the words that Daniel likes to say: Maven has a role in dependency management .

Also, is there a way to import the same project package for idea, eclipse or even Myeclipse? some. Maven has it, and this feature is called unified project structure .

The unified structure is as follows:

In addition, Maven can also help the project build.

We know that after the general project is compiled, it is tested, packaged, and finally released.

Use Maven to manage projects, and use standardized processes to manage project processes.

Including the cleanup operation, that is, to clean up the dependencies of other Maven imported projects, and to ensure the adaptation of the version of the package.

In short, based on Maven's instructions, operations such as project compilation and testing can be performed quickly.

process:

Clean-->Compile-->Test-->Package-->Release

Double-click compile to automatically compile, double-click package to package. 

Also cross-platform, including Linux, Windows, MacOS, etc.

2. Introduction to Maven structure

The Maven structure is as follows:

Among them, the plug-in is reflected in the functions that double-clicking complie can automatically compile and package can be automatically packaged. From files one by one, to jar packages, and finally released to form other projects...

Then there's the POM, the Project Object Model, which is what describes the Maven project itself. It includes information describing itself, such as the affiliation organization, module name, and version in the figure below.

<groupId>com.haha</groupId>
<artifactId>maven-project01</artifactId>
<version>1.0-SNAPSHOT</version>

In addition, other dependent package information can be described. For example, the 1.2.3 logback package imported above:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

This is written in the pom.xml file. 

Finally there is the dependency management model.

The dependency management model is used to find packages, import dependencies, etc.

It will first look in the local warehouse, and then in the central warehouse built by Maven. Of course, the speed of the central warehouse whose server address is located abroad is not very high, so the remote warehouse built by the company or the like is generally used.

3. Install Maven

1. First unzip the Maven package

***As a programmer, it is best not to include Chinese, spaces, special characters * and the like in this article, including the path to place files in the future. ***

I extracted it to the D drive.

Briefly introduce:

bin - store executable files

conf - store configuration files

lib- stores the jar package resources that Maven depends on

2. Configure Maven local warehouse

Create a new folder under the Maven path as a local warehouse.

As a friendly reminder, it is best to put a disk with more space, because the Maven local warehouse may store more and larger jar packages.

Here is a build called mvn_pkg:

Hey, the maven folder secretly changed its name and found that there is no

Then edit the settings.xml file in conf. You can right-click-open with-text document to open, or you can use notepad++ and the like.

Find <localRepository>/path/to/local/repo</localRepository>, and then copy out the comment, for example, you can copy it directly to the following, and then change the path to the folder you just created. The specific modified code is as follows:

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  
  <localRepository>D:\maven\mvn_pkg</localRepository>

3. Configure Alibaba Cloud Private Server

Also in the settings.xml file, find the mirrors folder and copy the following code in the past:

<id>alimaven</id>  
<name>aliyun maven</name>  
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf> 

 The copy result is shown in the figure:

Remember to save it.

4. Configure Maven environment variables

Then, if you want to use maven commands in cmd in any directory, you need to configure Maven environment variables.

Search for "Edit System Environment Variables" in the search bar, click "Environment Variables", and create a new MAVEN_HOME environment variable, as shown in the figure below, click "New", enter the variable name and variable value, and then click "OK".

 Then add the bin directory under maven to the path. Find the path, click "Edit", then click "New", enter

%MAVEN_HOME%\bin

 Then click OK.

Finally, the Maven environment is configured.

verify

Open the command line (how to open it can refer to my article on how to open Windows command line and Powershell window )

Enter mvn -v test.

Successful display results are as follows:

 If unsuccessful, it will display like this:

Then carefully check the fourth step and configure the system environment. (It is very likely that the Java path is not configured properly)

Guess you like

Origin blog.csdn.net/m0_46948660/article/details/131604194