Maven resolves from installation to project creation

1. Install Maven
Maven is the top-level project of the Apache Foundation. Under normal circumstances, Apache's fancy will not be bad.

We can download the Maven development package from http://maven.apache.org/ , which is actually a compressed package. After downloading, unzip it and configure the environment variables to use it. It's super easy!

Suppose we just downloaded an apache-maven-3.1.1-bin.zip file and now unzip it to the D:/tool ​​directory. We might as well rename the decompressed directory to Maven, so that the root directory of Maven is D:/tool/maven.
There are two environment variables that can be configured:

  • M2_HOME = D:/tool/maven
  • MAVEN_OPTS = -Xms128m -Xmx512m

  • The above M2_HOME must be configured. If you want Maven to run faster, you can set MAVEN_OPTS according to your own situation.

Now we can open cmd and enter:

mvn -v

I think you will see some information, congratulations, Maven installed successfully!

2. Understand the Maven repository
The most direct help that Maven brings to us is that the jar packages are managed uniformly, so where are these jar packages stored? They are in your local repository , located in the C:\Users\username.m2 directory (of course, this default address can also be modified).
In fact, the local warehouse can be understood as "cache", because the project will first obtain the jar package from the local warehouse. When the specified jar package cannot be obtained, the local warehouse will download the jar package from the remote warehouse (or central warehouse) and put it in the into the local repository for future use. This remote repository is officially provided by Maven and can be accessed through http://search.maven.org/ . In this way, the local repository will grow larger and larger as the project accumulates. The following picture can clearly express the relationship between the project, the local warehouse, and the remote warehouse.

The relationship between the project and the local repository and the remote repository.
Is this structure similar to Git's local and remote repositories?

Now that Maven is installed, the local repository is also available. Let's create a Maven project together.

3. Create a Maven project
Then Maven will download the Archetype plugin and all its dependent plugins. These plugins are actually jar packages, which are stored in your Maven local repository.

In cmd, you will see hundreds of Archetypes, which can be understood as project templates, from which you have to choose one.

Our goal is to create a Java web project, so you can choose maven-archetype-webapp (you can do a fuzzy search in cmd), then Maven will have some conversations with you, and Maven wants to know the following:

What is the project Archetype Version? - Optional 1.0 version

What is the project groupId (organization name)? - Enter com.smart

What is the project artifactId (artifact name)? —— You can enter smart-demo

What is the project version (version number)? - 1.0 can be entered

What is the project package (package name)? —— Enter com.smart.demo

The above method is called Interactive Mode.

If you're a productive person, you might find this interaction too cumbersome, you can also try to accomplish the same thing with just one name:

  • mvn archetype:generate -
  • DinteractiveMode = false -
  • DarchetypeArtifactId=maven-archetype-
  • webapp -DgroupId=com.smart -
  • DartifactId=smart-demo -Dversion=1.0

The above method is called Batch Mode.

Of course, there is a third option, use IDE to create Maven project, you can use Eclipse, NetBeans, IDEA to create Maven project, the operation process should be very simple.

You can also use IDEA to open a Maven project directly, just need to File -> Open -> select pom.xml, then you can develop a Maven project in IDEA, paste a picture:
The directory structure of a maven project.

It is worth explaining this Maven directory specification a little:

  • The main directory is the main code of the project, and the test directory is the test-related code.
  • The compiled output code will be placed in the target directory (this directory is at the same level as the src directory, which is not shown here).
  • Java code is stored in the java directory, and configuration files are stored in the resources directory.
  • Web application related code is stored in the webapp directory.
  • pom.xml is the configuration file for the Maven project.

The pom.xml is called the Project Object Model, which is used to describe the entire Maven project, so it is also called the Maven description file.
It can be seen that pom.xml is the key to understanding Maven, and it is necessary to see what it looks like.

4. Understand pom.xml
When you open the automatically generated pom.xml, you may feel that the readability is not very good. It is necessary to format it. After finishing, it looks like this:
pom.xml details

Briefly explain:

  • modelVersion: This is the version number of the POM, which is now 4.0.0, must have, but does not need to be modified.
  • groupId, artifactId, version: represent the organization name, component name, and version number of the Maven project respectively. The three of them together are the Maven
    coordinates. According to this coordinate, the only Maven component in the Maven repository can be corresponded.
  • packaging: indicates the packaging method of the project, war indicates that it is packaged as a war file, and the default is jar, which indicates that it is packaged as a jar file.
  • name, url: Indicates the name and URL address of the project, which are of little significance and can be omitted.
  • dependencies: Define the dependencies of the project, each of which corresponds to a Maven project. It can be seen that the Maven
    coordinates appear again, and there is an additional scope to indicate the scope (described below).
  • build: Represents the configuration related to the build, where finalName represents the name smart-demo.war after the final build, and the
    finalName here can also be defined in another way (described below).
    If the pom.xml is expressed in a tree diagram, it will be more clear:
    Screenshot of maven tool

It can be seen that in addition to the basic information of the project (Maven coordinates, packaging method, etc.), each pom.xml should include:

  • Lifecycle
  • Plugins
  • Dependencies

Lifecycle is the life cycle of project construction, which includes 9 Phases.

As we all know, Maven is a core plus multiple plugin architecture, and these plugins provide a series of very important functions, these plugins will play an important role in many stages.
Life cycle:
maven lifecycle
The plug-in name that appears in the above table is actually the alias (or prefix) of the plug-in. For example, the compiler is actually org.apache.maven.plugins:maven-compiler-plugin:2.3.2. is the full name of the Maven plugin.

Each plugin also includes a list of goals. Taking the compiler plugin as an example, it includes the following goals:

  • compiler:help: Used to display the help of the compiler plugin.
  • compiler:compile: used to compile the Java code in the main directory.
  • compiler:testCompile: used to compile the Java code in the test directory.

It can be seen that the plug-in target is the person who does the specific work. A plug-in includes one or more goals, and a stage can be supported by zero or more plug-ins.

We can define a series of project dependencies (component packages) in pom.xml, each component package will have a Scope (scope), which indicates when the component package works, including the following five:

compile: The default scope, valid at compile, test, and runtime

  • test: valid for testing
  • runtime: valid for test, runtime
  • provided: Valid at compile and test time, but not at runtime
  • system: similar to provided, but depends on system resources

It can be represented by a matrix table:
stage effect demonstration
pom.xml roughly explain:

  • We can use properties
    to define some configuration properties, such as: project.build.sourceEncoding (project build source encoding method), which can be set to
    UTF-8 to prevent Chinese garbled characters. You can also define the version number of the relevant component package, for example: smart.version, which is convenient for unified upgrade in the future.
  • It is recommended to use the latest version of JUnit. The JUnit automatically generated by Archetype is too old (3.8.1) and can be changed to the latest version (4.11).
  • Because there is no need to use the MySQL client API, it is only available at runtime, so we set the scope of the MySQL artifacts to runtime.
  • Because we only want to use the Servlet API in the code, and don't want to put its corresponding jar package in the lib directory of WEB-INF, we can set
    the scope of the Servlet component package to provided.
  • In order to ensure that it runs on JDK 1.6, we can configure the maven-compiler-plugin plugin, set the input source code to
    1.6, and the compiled output bytecode to 1.6.
  • If you want to skip tests, you can configure the maven-surefire-plugin plugin and set skipTests to true.
  • If you want to configure the generated war package as artifactId, you can modify the maven-war-plugin plugin and change the warName to
    ${project.artifactId}, so that you don't need to configure finalName.
  • If you want to deploy the application to Tomcat through Maven, you can use the tomcat7-maven-plugin plugin, you can use the mvn
    tomcat7:run-war command to run the war package.

5. Use Maven commands
We have used several Maven commands before, such as: mvn archetype:generate, mvn tomcat7:run-war and so on. Actually, Maven commands can be executed in two different ways:

Method 1: mvn <plugin>:<target> [parameters]

Method 2: mvn <stage>

Now we are exposed to the first way, and the second way is the most frequently used in our daily life, for example:

  • mvn clean : empty the output directory (ie the target directory)
  • mvn compile : compile source code
  • mvn package : Generate component packages (usually jar packages or war packages)
  • mvn install : install the artifact package to the local repository
  • mvn deploy : deploy the artifact package to the remote repository

Note that when executing Maven commands, it must be executed at the root directory of the Maven project, that is, there must be a file named pom.xml in the current directory.

I believe that after reading the analysis of maven, you must have a very good understanding of maven. I hope you can use maven to manage projects more proficiently in your future studies.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860050&siteId=291194637