Project management tools: Maven Usage Summary

mb61b711907d5f4 2022-01-26 13:45:43 阅读数:839

project management tools maven usage

One 、 Concept

Maven Is a project management and build automation tool .Maven Provides developers with a complete lifecycle framework , The development team can automatically complete the infrastructure construction of the project . One Maven The results of the project are shown in the figure below :

 Project management tools :Maven Summary of usage _maven

Two 、Maven install

In the installation maven Before , Make sure it's installed first JDK, And then in Apache On our website http://maven.apache.org/download.cgi Download the latest version of Binary zip archive package .

After downloading, unzip it and put it in a local path , Then configure maven environment variable :

New variable name M2_HOME:C:\Program Files\apache-maven-3.3.9

A variable's value :C:\Program Files\apache-maven-3.3.9

When the configuration is complete , stay windows At the command prompt , Input mvn -version, If the following figure is displayed, the installation is successful :

 Project management tools :Maven Summary of usage _maven_02

stay Eclipse 4.0 Above version , Inherited Maven Plug in for , Can be in Window -> Preference -> Maven Do some configuration :

 Project management tools :Maven Summary of usage _maven_03

 Project management tools :Maven Summary of usage _apache_04

3、 ... and 、 Common commands


mvn archetype:create establish Maven project
mvn compile Compiled source code
mvn deploy Publish the project
mvn test-compile Compile test source code
mvn test Running unit tests in an application
mvn site A website that generates project related information
mvn clean Clear the build results in the project directory
mvn package According to the project jar
mvn install In the local Repository Install in jar
mvn eclipse:eclipse Generate eclipse Project documents
mvn jetty:run start-up jetty service
mvn tomcat:run start-up tomcat service
mvn clean package -Dmaven.test.skip=true: Clear the old bag and repack it , Skip test class
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

Four 、 Life cycle

Maven The life cycle of is to abstract all the construction processes , Easy to unify .

1、clean Clean up the project

pre-clean Carry out pre clean up work

clean Clean up all files generated by the last build

post-clean Execute the cleaned file

2、Default Build the project

compile Compile the source code in the project

test Use the appropriate unit test framework to test , The test code will not be packaged or deployed

test-compile Compile the source code to be tested to the path , It's usually compilation src/test/java In the catalog java File to target output test classpath Directory

package Accept compiled code , Packaged in a releasable format , Such as JAR

install Install the package to the local repository , Provide dependencies for other local references

3、site Generate project site

pre-site Work to be done before building the project site

site Generate site documentation for the project

post-site What to do after building the project site

site-deploy Publish the generated site to the server

5、 ... and 、 first Maven project

stay Eclipse Create a new one in Maven project :

 Project management tools :Maven Summary of usage _apache_05

 Project management tools :Maven Summary of usage _xml_06

Finish The result of the post project is :

 Project management tools :Maven Summary of usage _apache_07

Eclipse structure web Project time , Same as above , It should be noted that the choice ArcheType When to choose maven-archetype-webapp.

pom File for :

 Project management tools :Maven Summary of usage _maven_08 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.xj.webdemo</groupId>

  <artifactId>webdemo</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>webdemo Maven Webapp</name>

  <url>http://maven.apache.org</url>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.10</version>

      <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>javax.servlet</groupId>

        <artifactId>javax.servlet-api</artifactId>

        <version>3.0.1</version>

        <scope>provided</scope>

    </dependency>

  </dependencies>

  <build>

    <finalName>webdemo</finalName>

    <plugins>

        <plugin>

            <groupId>org.apache.tomcat.maven</groupId>

            <artifactId>tomcat7-maven-plugin</artifactId>

            <version>2.2</version>

            <executions>

                <execution>

                    <phase>

                        package

                    </phase>

                    <goals>

                        <goal>run</goal>

                    </goals>

                </execution>

            </executions>

        </plugin>

    </plugins>

  </build>

</project>


And then run , You can enter... In the browser localhost:8080, Then you can access it .

6、 ... and 、POM file

All configurations of a project are placed in pom In file : Define the type of project 、 name 、 Managing dependencies , Custom plug-in behavior, etc .


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xj.maven</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.


stay pom In file


<groupId></groupId>
<artifactId></artifactId>
<version></version>
  • 1.
  • 2.
  • 3.


It's called coordinates , It uniquely identifies a project , With maven coordinate , We can use it to specify other items that our project depends on

7、 ... and 、Maven library

When the first run maven On command , You need Internet Connect , Because it has to download some files from the Internet ,maven The default remote library is http://repo1.maven.org/maven2, This remote library has maven Core plug-ins and downloadable jar file .

But not all jar Files can be downloaded from remote Libraries , For example, we develop our own projects , At this time, you can customize the library internally , You can also manually download and install the required jar File to local library .

Local library means maven Downloaded the plug-in or jar A copy of the file stored on the local machine after the file . stay Linux On , Its position is ~/.m2/repository, stay Windows XP On , stay C:\Documents and Settings\username\.m2\repository , stay Windows 7 On , stay C:\Users\username\.m2\repository. When maven Find what you need jar When you file , It will first look in the local library for , Only if it can't be found , Will go to the remote library to find .

Run the following command to get our helloworld Install the project to the local library

copyright:author[mb61b711907d5f4],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261345411307.html