Introduction to common maven commands

 

3.1.1. Introduction to Maven

  • We use maven to build the application environment, so let's briefly introduce maven first. What is Maven? How you answer this question depends on how you view it. Most Maven users refer to Maven as a "build tool": a tool for building source code into releasable artifacts. Build engineers and project managers will say that Maven is something more complex: a project management tool. So what is the difference? A build tool like Ant only focuses on preprocessing, compilation, packaging, testing and distribution, while a project management tool like Maven provides a superset of what a build tool has to offer. In addition to providing build functionality, Maven can also generate reports , generate Web sites, and help facilitate communication among work team members.

  • The latest version of Maven is 2.1.0. Because the version of maven2.0 is completely redesigned compared with maven1.0, and the speed is faster than before, it is called maven2. At present, many open source projects have been built with maven2. It shows how popular it is. Note: maven mentioned in this guide refers to maven2.

3.1.2. Maven common commands

  • mvn archetype:create : create a Maven project

  • mvn compile : compile source code

  • mvn test-compile : compile test code

  • mvn test : run unit tests in the application

  • mvn site : a website that generates project-related information

  • mvn clean : clean the build results in the target directory

  • mvn package : Generate jar files according to the project

  • mvn install : install the jar in the local Repository

  • mvn eclipse:eclipse : Generate Eclipse project files

  • mvn -Dmaven.test.skip=true : ignore test documentation compilation

3.2. POM

The full name of POM is Project Object Model, which is the project object model. pom.xml is the project description file of maven, which is similar to the project.xml file of antx. The pom.xml file describes the information of the project in the form of xml, including project name, version, project id, project dependencies, compilation environment, continuous integration, project team, contribution management, report generation, etc. In short, it contains all the project information.

3.2.1. Basic configuration of pom.xml

<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>org.codehaus.mojo</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
</project>

modelVersion Describes which version of the project descriptor this POM file conforms to.

groupId A universally unique identifier for an item. Usually a fully correct package name is used to distinguish it from similar names in other projects (eg: org.apache.maven).

artifactId The identifier specified for the artifact is unique within the group given the groupID, and the artifact represents the component (product) that is made or applied by a project.

version The version of the artifact generated by the current project

The above four elements are indispensable, among which groupId, artifactId, version describe the unique identifier of the dependent project.

3.2.2. pom.xml file structure

<project>
  <modelVersion>4.0.0</modelVersion>
  <!- ​​Basic information about The Basics project->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>
  <!- ​​Build Settings project's build settings->
  <build>...</build>
  <reporting>...</reporting>
  <!- ​​More Project Information Other project information->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>
  <!-- Environment Settings  ->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

project is the root node of pom.xml. For other elements, please refer to POM Reference

3.2.3. 3 important relationships of POM

POM has three very important relationships: dependency, inheritance, and composition.

3.2.3.1. Dependencies

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
    ...
</dependencies>

If you want to depend on a jar package that is not in the maven library, the method is very simple, that is, first install the jar package into the local maven library using the following command:

mvn install:install-file -Dfile=my.jar -DgroupId=mygroup -DartifactId=myartifactId -Dversion=1

Then add the dependencies to it.

3.2.3.2. Inheritance

Another powerful change that maven brings is project inheritance.

3.2.3.2.1. Defining the parent project
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mygroup </groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <packaging>pom</packaging>
</project>

The packaging type, defined as pom is used to define as parent and to synthesize multiple projects. Of course, the pom of the maven project we created all inherit the super pom of maven. If you want to see the complete pom structure of the project (parent or child), you can run:

mvn help:effective-pom

That's it.

3.2.3.2.2. Subproject Configuration
<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.mygroup </groupId>
   <artifactId>my-child-project</artifactId>
   <parent>
        <groupId>com.mygroup </groupId>
        <artifactId>my-parent</artifactId>
        <version>2.0</version>
        <relativePath>../my-parent</relativePath>
    </parent>
</project>

relativePath may not be needed, but it is used to specify the parent's directory for quick query.

3.2.3.3. Composition Relations

A project has multiple modules, also called multiple modules, or composite projects. Definition as follows:

<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.mygroup </groupId>
   <artifactId>my-parent</artifactId>
   <version>2.0</version>
   <modules>
       <module>my-child-project1<module>
       <module>my-child-project2<module>
   </modules>
</project>

Where module describes the relative path of the subproject.

3.2.4. dependencyManagement和Profile

        Maven also provides a dependencyManagement element that provides a way to unify dependency version numbers. The dependencyManagement element is generally used in the top-level parent POM. Using the dependencyManagement element in pom.xml allows you to reference a dependency in a subproject without explicitly listing the version number. Maven will go up the parent-child hierarchy until it finds a project with a dependencyManagement element, and then it will use the version number specified in the dependencyManagement element, which solves the problem of incompletely modifying the dependency version number.

        Maven's Profile element can customize a particular build for a particular environment, making build portability between different environments possible. For example, to run mvn install using the production profile, you need to pass the -Pproduction parameter on the command line, where production is the id of the profile. To verify that the production profile overrides the default Compiler plugin configuration, run Maven with debug input (-X) turned on like this.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327043487&siteId=291194637