To be honest, how many of these Maven commands do you know?

Time:2021-1-22

Focus on “Java back end technology stack”

Reply to “interview” for full interview information

Today, a wechat friend asked me privately: why does MVN install seem to have done the work of MVN compile, so I usually use MVN install. The strange thing is, why do you want to build an MVN compile?To be honest, how many of these Maven commands do you know?

After listening to this friend, I remember that when I just started using MVN, it seemed to be the same.

That’s what my boss taught me. I use it like this. It’s very coolTo be honest, how many of these Maven commands do you know?

Sometimes when you see big guys using MVN… A long list of commands, you will be blinded. What is this command? What – dxxx – pxxx.

After thinking about it, it is necessary to write an article about Maven command. I hope you can master more MVN commands through this sharing.To be honest, how many of these Maven commands do you know?

To be honest, how many of these Maven commands do you know?

Creating a project with commands

Here is the command template:

mvn  archetype:generate Fixed format
-Dgroupid organization ID (package name)
-Dartifactid project name
-Create a java project by specifying archetypeid and Maven archetype QuickStart;
Maven archetype webapp to create a web project
-Does dinteractivemode use interactive mode
-Darchetypecatalog = local offline mode

Now let’s demonstrate how to create a project. We demonstrate two kinds of projects: standard project and web type project.

Create Java standard project

Let’s create a my Maven project.

Use the command:

mvn archetype:generate -DgroupId=com.tian.maven -DartifactId=my-maven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=local

Enter your own workspace.

To be honest, how many of these Maven commands do you know?

This is the Maven project that we created manually before. Today we use the command to create a my Maven project.

Open the run window.

To be honest, how many of these Maven commands do you know?

Enter the above command:

To be honest, how many of these Maven commands do you know?

Press enter.

To be honest, how many of these Maven commands do you know?

Build success indicates that we have built successfully. Back to our workspace.

To be honest, how many of these Maven commands do you know?

I found that my Maven project had been created.

My Maven project structure

my-maven
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- tian
    |               `-- maven
    |                   `-- App.java
    `-- test
         `-- java
            `-- com
                 `-- tian
                    `-- maven
                        `-- AppTest.java

Let’s open this pom.xml Documents.

To be honest, how many of these Maven commands do you know?

POM file header label

Here is a brief description of these labels:

<modelVersion>: model version. Maven 2.0 must be written like this. It is now the only version that Maven 2 supports.

<groupId>: the unique flag of a company or organization, and the path generated during configuration is also generated from it, such as: com.tian.trade , Maven will put the jar package of the project in the local path:/com/tian/trade

<artifactId>: the unique ID of this project. There may be more than one project under a groupid, which is distinguished by artifactid.

<version>: the current version number of this project.

<packaging>: packaging mechanism, such as POM, jar, Maven plugin, EJB, war, ear, rar, par. The default is jar.

<name>: project name.

<url>: address of central warehouse.

<dependencies>: define the dependencies of this project.

<dependency>: each dependency corresponds to this jar package. In general, Maven retrieves the component through the values of groupid, artifactid and Version (commonly known as coordinates), and then introduces it into your project. If someone wants to refer to the project you are developing (provided that it has been developed and published to the remote warehouse), he needs to create a new dependency node in his POM file, write the groupid, artifact ID and version of the project, and Maven will download the jar package you uploaded to his local.

<scope>: dependency scope. Scope includes the following values: compile, provided, runtime, test and system.

The content of apptest class is to test the app class in the main directory. The JUnit test used here. If you are more careful, you will find that the test related classes are in the JUnit package directory, and they are in the pom.xml We also see in the file that due to the default dependency on JUnit.

To be honest, how many of these Maven commands do you know?

OK, the above is a Java standard project created with the Maven command. Do you feel great. There is no need to manually create folders and classes one by one.

In fact, it can be done with relatively simple commands.

mvn archetype:generate -DgroupId=com.tian.maven -DartifactId=my-maven -Dversion=1.0-SNAPSHOT

The effect is the same as above.

Create a web project

Also create in our workspace:

To be honest, how many of these Maven commands do you know?

Press enter:

To be honest, how many of these Maven commands do you know?

So our web project is created successfully. Let’s take a look at what was created:

The SRC directory and POM file are also created. Look first pom.xml Content:

pom.xmln There is no difference between the content and the previous one.

My web Maven project structure

 my-web-maven
    |-- pom.xml
    `-- src
        |-- main
            `-- resource
            `-- webapp
                `-- index.jsp
                `-- WEB-INF
                    `-- web.xml

There is nothing in the resource directory, because we need to add configuration files ourselves. Maven doesn’t know whether we need XML, properties or configuration files.

Enter the webapp directory, there is a index.jsp And WEB-INF, which has a web.xml .

To be honest, how many of these Maven commands do you know?

The above directory is our simple version of spring MVC. But if we want to store java source files, we have to create our own Java directory.

OK, so far, let’s create a web project with Maven.

But many people may find it troublesome, so there is another lazy way: you can also create a new Java project or a new web project in the idea.

-D and – P parameters

The – D in the Maven command parameter represents the properties property and – P represents the profiles configuration file.

-Beginning with D

D means to set properties. Use the command line to set properties – D template:

mvn -DpropertyName=propertyValue clean package

If propertyname does not exist in pom.xml File, it will be set. If propertyname already exists pom.xml File, its value will be overridden by the value passed as a parameter.

For example, we used – dgroupid, dartifactid and – dversion in creating Java standard projects. -They’re all behind D pom.xml There is a corresponding label in the file.

-DgroupId=com.tian.maven

That is to assign a value to groupid, and others are similar. In the end, we came to the conclusion.

To be honest, how many of these Maven commands do you know?

We didn’t specify version. We use the default version. You can also assign a value to version:

-Dversion=1.0-SNAPSHOT

To set multiple variables, use multiple space separators plus – D:

mvn -DpropA=valueA -DpropB=valueB -DpropC=valueC

-Beginning with P

-P represents the properties of the profiles configuration file, that is to say, it can be passed or assigned by – P in the specified configuration file. It usually means that we want to use the jar package of that environment. In the actual project, it will be accompanied by the configuration file of the corresponding environment.

For example, we usually use: MVN test – ptest. Represents the specified test environment.

Common commands

We have created two projects, the Java standard project and the web project. Let’s take the Java standard project as an example to demonstrate several commands.

mvn compile

Compile the project.

To be honest, how many of these Maven commands do you know?

After successful compilation, a target directory (compiled file) will be added.

To be honest, how many of these Maven commands do you know?

The target directory has been mentioned in the previous article, so I won’t repeat it here.

mvn clean

Delete the entire target directory.

mvn test

Test, the premise is that we integrate JUnit, and have a test directory, and the corresponding test class.

To be honest, how many of these Maven commands do you know?

To be honest, how many of these Maven commands do you know?

From the log, we can see the failure, error and time-consuming information. In addition, we found that there are several test classes in the target directory

In fact, the test class is compiled and placed in this directory.

mvn package

Pack and continue to demonstrate with the above project:

To be honest, how many of these Maven commands do you know?

MVN test is also executed when MVN package is executed. Because of the order of life cycle, package comes after test. And it’s a jar package.

In the target directory, you can see that:

To be honest, how many of these Maven commands do you know?

mvn install

Installation is to install our large jar into our local warehouse.

Now I don’t have my-maven-1.0 in my local warehouse- SNAPSHOT.jar It’s packed.

To be honest, how many of these Maven commands do you know?

We execute the MVN install command:

It can be seen from the log that MVN test is also executed. In addition, our my-maven-1.0 is added to the install command- SNAPSHOT.jar A copy of the package went to the local warehouse.

Command combination

In front of us, the demonstration is a single command demonstration.

MVN clean compile, MVN clean install, etc.

Visualization in idea

In idea, we can use visualization and double-click the mvane commands above. The situation is as follows:

To be honest, how many of these Maven commands do you know?

For example, double-click clean:

To be honest, how many of these Maven commands do you know?

You can also enter the command mode in idea:

To be honest, how many of these Maven commands do you know?

The following are commonly used:

  • Skip test:- Dmaven.test.skip (=true)
  • Specified port:- Dmaven.tomcat.port=9090
  • Ignore test failed:- Dmaven.test.failure .ignore=true 。

Of course, if your other related projects have been updated, be sure to run MVN clean install in the root directory of the project to perform the update, and then run MVN tomcat:run Make the change effective.

Other orders

  • mvn eclipse:eclipse : build eclipse project.
  • mvn idea:idea : generate idea project.
  • MVN test compile: compile the content of the test.
  • MVN test – skipping compile – skipping test compile: only tests without compiling, and does not test compilation.
  • MVN – Version / – V: display version information.
  • mvn – Dmaven.test.skip=true : does not execute test cases or compile test case classes.
  • MVN – E: display detailed error information
  • MVN – U: forced update of dependent package
  • MVN – B: this parameter means let Maven build the project in batch mode
  • MVN clean install – PL user Dao: install only user Dao.
  • s pringboot:run : to run the spring boot project, there are also start and stop.
  • mvn jetty:run : run the project on jetty.

Publish third party jars to local repository

In the actual development, some jar packages are not in the central warehouse or our private server, but our project uses Maven. At this time, a better way is to put this jar package into our private server, so that we can better manage it.

For example: jar needed for XX payment, jar needed for XX SMS sending, etc.

mvn install:install-file -DgroupId=com -DartifactId=client -Dversion=0.1.0 -Dpackaging=jar 
-Dfile=d:client-0.1.0.jar -DdownloadSources=true -DdownloadJavadocs=true

Note: version, current storage directory of package, groupid, artifactid.

Little helper

  • mvn help:system Displays a list of platform details, such as system properties and environment variables.
  • mvn help:active-profiles Lists the current configuration files that are valid for building.
  • mvn help:all-profiles Lists the available profiles under the current project.
  • mvn help:describe : describes the properties of plug-ins and / or mojo (Maven common old Java object).
  • mvn help:evaluate : evaluate the Maven expression given by the user interactively.
  • mvn help:effective-pom : displays a valid POM as the current version of XML, taking into account the active configuration file. If it is verb, a comment is added to each XML element to describe the starting point of the line.
  • MVN help: effective settings: the calculated settings are displayed as the XML of the project given the profile enhancements and the inheritance of global settings to user level settings.

Have you ever suffered from not remembering the goals of a plug-in? Have you ever suffered from not remembering the parameters of a goal? Try this command. It will tell you everything. Parameters:

-Dplugin=pluginName   
-Dgoal (or - dmojo) = goalname:

-Dgoal is used with – dplugin, which lists the goal information of a plug-in.

If it is not detailed enough, you can also add – ddetail. (Note: a plug-in goal is also considered to be a “mojo”, Maven plain old Java object, which will be described in the plug-ins later.).

mvn help:describe -Dplugin=help -Dmojo=describe

Interview questions: talk about the differences among MVN compile, package, install and deploy

  1. MVN compile, compile class file
  2. MVN package, including MVN compile, type it into jar or war, and store it in the target directory
  3. MVN install, including MVN compile and MVN package, and then upload to the local warehouse
  4. MVN deploy, including MVN install, and then upload to private server

summary

This article describes how to use Maven command to create Java standard project and web project. Describes the meaning of – D and – P parameters. Describes the use of commands and instructions, interview questions and two ways to use in idea.

「PS」: the Maven command seems simple and easy to learn. Mvane is the leader, plug-ins are our developers, and the real workers are actually our developers. So Maven is essentially a plug-in framework, its core does not perform any specific build tasks, all these tasks are left to plug-ins to complete. Later we will have a special introduction to Maven plug-in, please look forward to it~

“The key to life is: as long as you work hard, you will win and lose brilliantly. 」

Recommended reading

Interviewer: what are the design patterns in mybatis? I answered eight questions at a time

Four mistakes of self taught programming, have you been recruited?

6000 words | second kill system design considerations