Today, a wechat friend asked me privately: MVN install how seems to compile MVN work also dry, so I usually use MVN install ok. Strange, why compile MVN?

Listening to this friend, I was reminded of how I felt when I first started using MVN. That’s what my boss taught me. I just use it. It feels good…

Sometimes I see big guys using MVN…. When you have a long list of commands, you get confused. What is this command? What? -Dxxx. -Pxxx.

I thought it would be worthwhile to write an article about the Maven command. I hope you can learn more about the MVN command by sharing this article.

Use the command to create the project

Here is the command template:

MVN archetype:generate archetype:generate DartifactId Project name DarchetypeArtifactId Maven-archetype-quickstart, create a Java Project; Maven-archetype-webapp, create a Web Project -DinteractiveMode whether to use interactive mode -DarchetypeCatalog=local offline modeCopy the code

Let’s demonstrate how to create projects. We will demonstrate two types of projects: standard projects and Web type projects.

Create a Java standards project

Let’s create a my-Maven project.

Use the following command:

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

Enter your own workspace.

This is the Maven project we created manually. Today we will use the command to create a My-Maven project.

Open the run window.

Enter the command above:

Press Enter.

BUILD SUCCESS: indicates that the BUILD is successful. Back to our workspace.

Found that the My-Maven project has 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
Copy the code

Let’s open the pom.xml file.

Pom file header label

Here is a brief description of these labels:


: modelVersion. Maven2.0 must be written like this and is now the only version supported by maven2.


: unique identifier of the company or organization, and the path generated during configuration is also generated by this path, such as com.tian. Trade. Maven will place the jar package of the project in the local path: /com/tian/trade.


: unique ID of this item. There may be multiple items under a groupId, which is distinguished by artifactId.


: indicates the current version of the project.

< Packaging > : Packaging mechanisms such as POM, JAR, Maven-plugin, EJB, WAR, EAR, RAR, par, jar by default.


: Project name.

< URL > : Central warehouse address.


: Defines the dependencies of this project.


: Each dependency corresponds to this jar package. Typically, Maven retrieves the component by groupId, artifactId, and Version values (commonly known as coordinates) and then imports them into your project. If someone wants to reference the project you are developing (if it has been developed and published to a remote repository), they need to create a dependency node in their POM file and write groupId, artifactId, version of the project. Maven will download the jar packages you upload to its local location.


: dependency scope. Scope contains the following values: compile, provided, Runtime, test, and system.

The content of the AppTest class tests the App class in the main directory. The junit test used here. If you are careful, you can also see that the Test related classes are in the junit package directory, and we can also see in the POM.xml file that because of the default dependency on junit.

Ok, so that’s a Java standard project created using the Maven command. Does it feel good? There is no need to manually create folders and classes one by one.

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

Archetype: generate-dgroupid =com.tian. Maven-dartifactid = my-maven-dversion = 1.0-snapshot MVN archetype: generate-dgroupid =com.tianCopy the code

The effect is the same as above.

Creating a Web project

Also create in our workspace:

Press Enter:

So our Web project is created successfully. Let’s see what was created:

SRC directory and POM file are also created. Start with the pom.xml content:

The POM.xmln content is no different from the previous one.

My-web-maven project structure

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

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

JSP and web-INF. Web-inf has web.xml.

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

Ok, here we are using Maven to create a Web project.

However, many people may find it troublesome, so there is another lazy solution: you can also create a Java project in IDEA, create a New Web project.

-d and -p parameters

The -d in the Maven command parameter represents the Properties property, while -p represents the Profiles profile.

– D beginning

-d indicates to set the Properties property. Use the command line to set the Properties -d template:

mvn -DpropertyName=propertyValue clean package
Copy the code

If propertyName does not exist in the pom.xml file, it will be set. If the propertyName already exists in the POM.xml file, its value will be overwritten by the value passed as a parameter.

For example, we used -dgroupid, DartifactId, and -dversion to create the Java standards project. Everything after -d has corresponding tags in the POM.xml file.

-DgroupId=com.tian.maven
Copy the code

That’s assigning to groupId, and so on. And then finally we have.

Version is not specified; the default is used. It is also possible to assign version:

- Dversion = 1.0 - the SNAPSHOTCopy the code

To set multiple variables, use multiple space delimiters plus -d:

mvn -DpropA=valueA -DpropB=valueB -DpropC=valueC
Copy the code

– ps

-p represents an attribute of the Profiles profile, meaning that in the specified, it can be passed or assigned by -p. This usually means that we are going to open the jar package for that environment, and in the actual project will be accompanied by the configuration file for that environment.

For example, MVN test -Ptest is usually used. Indicates that the test environment is specified.

Common commands

We have created two projects above: the Java Standards project and the Web project. Let’s use the Java standard project as an example to demonstrate a few commands.

mvn compile

Compile the project.

A target directory (compiled file) is added upon successful compilation.

This target directory was covered in the previous article and will not be covered here.

mvn clean

Delete the target entire directory.

mvn test

Testing, provided we integrate junit and have the Test directory and corresponding test classes.

Logs provide information about failures, errors, and time consumption. Test_classes = target; test_classes = target;

Basically, you compile your test classes and put them in this directory.

mvn package

Wrap it up and continue with the demonstration using the above project:

The MVN test is also executed when the MVN package is executed. Because of the lifecycle order, package comes after test. And also into a JAR package.

In the target directory you can see:

mvn install

Install is to install our large JAR into our local repository.

The my-Maven-1.0-snapshot. jar package is not currently available in my repository.

Let’s execute the MVN install command:

As you can see from the log, the MVN test was also executed. In addition, the install command copied our my-maven-1.0-snapshot. jar package to the local repository.

Command combination

The previous demonstrations were all single-command demonstrations.

MVN clean compile and MVN clean install.Copy the code

Visualization in IDEA

We can use visualizations in IDEA, double-clicking the mvane commands above. The picture below:

For example: Double-click clean:

You can also enter the command mode in IDEA:

The following are commonly used:

  • Skip tests: -dmaven.test.skip (=true)
  • -dmaven.tomcat. port=9090
  • Ignore the test failure: – Dmaven. Test. Failure. Ignore = true.

Of course, if any of your other associated projects have been updated, be sure to run MVN Clean Install in the project root directory to perform the update, and then run MVN tomcat:run for the changes to take effect.

Other commands

  • MVN Eclipse: Eclipse: Generates an Eclipse project.
  • MVN idea:idea: generates an IDEA project.
  • MVN test-compile: compiles the test contents.
  • MVN test-railcraft compile -skipping test-compile: tests instead of compiling and tests compilation.
  • MVN -version/-v: displays the version information.
  • Mvn-dmaven.test. skip=true: No test case is executed and no test case class is compiled.
  • Mvn-e: displays detailed error information.
  • Mvn-u: forcibly updates the dependency package
  • Mvn-b: This parameter tells Maven to build the project in batch mode
  • MVN Clean install-pl user-DAO: Only the user-DAO is installed.
  • Springboot :run: is to run the Spring Boot project, corresponding to the start and stop.
  • MVN Jetty: Run: Runs the project on Jetty.

Publish third-party jars to a local repository

In practice, some jar packages are not in the central repository or our private server, but our project is using Maven. It is better to put the jar package in our private server, so that we can better manage it.

For example: the payment needs jar, the need to send a message jar, 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=trueCopy the code

Note: version, package current directory, 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 valid configuration files for the build.
  • MVN help: All-profiles lists the available profiles under the current project.
  • MVN help:describe :describe the attributes of the plug-in and/or Mojo (Maven’s common Old Java Object).
  • MVN help:evaluate: Interactively evaluate a user-given Maven expression.
  • MVN help:effective -POM: Displays the valid POM as the current version of XML, taking into account the active configuration file. If verbose, a comment is added to each XML element to describe the start of the line.
  • MVN help: effective- Settings: displays the calculated Settings as XML for the project, given configuration file enhancements and inheritance of global Settings to user-level Settings.

If you’ve ever had trouble remembering goals for a plugin, if you’ve ever had trouble remembering parameters for a goal, try this command. It’ll tell you everything. Parameters:

- Dplugin = pluginName - Dgoal (or - Dmojo) = goalName:Copy the code

-dgoal is used with -dplugin, which lists the goal information for a plugin.

If you don’t have enough detail, you can also add -ddetail. (Note: a plugin goal is also considered a “Mojo”. Maven plain Old Java Object will be explained later in the plugin.)

mvn help:describe -Dplugin=help -Dmojo=describe
Copy the code

What are the differences between compile, package, install and deploy

  1. MVN compile, compile class files
  2. MVN package, including MVN compile, into jar or war, and save in the target directory
  3. MVN install, including MVN compile, MVN package, and then upload to the local repository
  4. MVN deploy contains MVN install and then uploads to the private server

conclusion

This article describes how to create Java standard projects and Web projects using maven commands. Describes the meanings of parameters starting with -d and -p. It describes the use and explanation of commands, interview questions and two ways of using them in IDEA.

“PS” : The Maven command looks simple and easy to learn. Mvane was the leader, the plugin was our developer, and we were the developers who did the real work. So Maven is essentially a plug-in framework, and its core doesn’t perform any specific build tasks, leaving all of those tasks to plug-ins. Stay tuned for an article on maven plugins

“The key to life is: as long as you put your heart into it, losing and winning are wonderful.”