It took so long to review the series one: Maven focuses on learning

Time:2019-12-30

Preface

I have been engaged in Java programming for some years (kekekekeke ~ take a sip of tea). Basically, I will use some integration tools, such as Maven or gradle, in every project development process. Recently, I am writing a project, but there are problems in the process of packaging. I don’t know how to solve these problems when I use Maven as a fool tool all the time. So now I want to take a fresh look at the tool and explain it in a way that’s easy to understand.

Intended for

This tutorial is for two types of people:

  1. Children’s shoes that use Maven for the first time but are not in the mood to read long tutorials on the official website
  2. Children’s shoes (such as me) that have used Maven but have no knowledge of its specific working mode
  3. Senior Maven players

Everyone is welcome to question the questions in this article. I will cherish your opinions!

Before you start

Install MAVEN

What is Maven for?

Six words are given on the official website as the purpose of Maven

  • Building
  • Documentation
  • Reporting Report
  • Dependencies dependency management
  • SCM configuration management
  • Release management
  • Distribution distribution

At first glance, these words seem to express nothing. Then I will use some specific examples to illustrate.

For the first time to contact Java students, write a class in eclipse, which contains a main method, and then click the run key to run the code.
However, projects in the real world are often very complex. First, a project will have a development process life cycle. The steps closely related to programmers include: development, testing, integration, packaging, deployment. In these processes, many programmers are often required to work together to complete a step, which may be implemented by multiple programmers in parallel. What’s more, in the iterative development process, there are often multiple versions of products, which have both commonality and difference. How to manage each version effectively? Not only that, in different development periods, often with the transfer of personnel, how can a new group of programmers to understand the project faster and put into work?

This raises a question,How to ensure that each programmer’s work is visible to each other, so that everyone can cooperate more smoothly, rather than conflict and redundancy

Maven was born here. It’s more like some kind of consensus. We follow this consensus to develop the program. Maven defines a series of development specifications, such as package naming and function, project naming and so on. Maven also implements many functions, such as automatic construction, automatic management of dependencies, automatic generation of project documents and so on. With the help of maven, developers can focus on coding as much as possible, rather than writing lengthy project description documents, or carrying out repeated project construction and complex configuration management.

Don’t talk nonsense, start a maven project!

Maven is essentially a terminal tool, but many ides (integrated development environment) are equipped with Maven plug-ins, so many people will use graphic tools to operate it when they first contact Maven. This is not conducive to learning Maven. It is recommended that you use the command line to control Maven as much as possible in the future, and use graphical tools after you are proficient.

First open the terminal (CMD or terminal) to enter the project directory, and then output the following command:

   mvn -B archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=com.mycompany.app \
  -DartifactId=my-app

This directive declares the name of the project (artifactid), the organization to which the project belongs (groupid), and the Maven template to use when the project is initialized.

Creating a project with Maven for the first time is slow because it initializes something.

It took so long to review the series one: Maven focuses on learning

After execution, the directory structure as shown below will be generated, where the root directory name is the value of artifactid (in this case, my app).
It took so long to review the series one: Maven focuses on learning
The details are as follows:
It took so long to review the series one: Maven focuses on learning

Here we will briefly introduce the purpose of each component under the root directory.
Note: ${basedir} refers to the root directory of the project, for example, / users / rale / my app on my computer

${basedir}/src/main/java: the source code of the project is placed in this directory. The package is built according to the property of groupid during initialization. Each level of domain name corresponds to a folder. This is the default build package specification of Maven.
${basedir}/src/main/java: the test code of the project is placed in this directory, and will not be loaded into the package when the project is packaged. The format of the folder under this directory is the same as that under main.
pom.xml: this file can be said to be the core file of maven, which is described in detail below.

The contents of the initialized POM file are as follows.

<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.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

POM contains all the information of current project management. POM file uses XML format to store the configuration information of the project (for children’s shoes that are not familiar with XML file, Baidu can do it by itself, this article will not describe its syntax in detail). First, I will briefly introduce the contents of the simple POM file in the figure above:

project: top level element in XML file
modelVersion: the version of the model adopted by POM. This element is rarely changed
groupId: the unique identification number of the organization developing this project, generally in reverse order of domain name
artifactId: the name of the project product
packaging : packaged format
version : packaged version
name: the display name of the project, usually used in the documents generated by maven
url: the web address of the project, usually used in the documents generated by maven
description: the basic description of the project, usually used in the documents generated by maven

At this time, there is an auto implemented Hello World instance in the app.java file generated by Maven. We can compile and run the project directly.
Execute at the root of the projectmvn test, Maven will automatically generate the corresponding. Class file located in${basedir}/target/classesDirectory and execute the test. You can see that the output of the figure below shows a total of one test case, and there are no failed test cases.

It took so long to review the series one: Maven focuses on learning

Use instructions latermvn packagePackage. The default path of the package generated file is${basedir}/target

It took so long to review the series one: Maven focuses on learning

So far, we have completed the basic operation of Maven.

Other operations of maven

Plugin

Here is an example of adding a maven compiler plug-in.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.3</version>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </plugin>
  </plugins>
</build>

Plug ins in Maven can be attached to different declaration cycles during compilation. There are two types of plug-ins, one is for compiling and the other is for generating reports. Compiled plug-ins should be declared in < build > modules, while reported plug-ins should be declared in < reporting / > modules

All plug-ins require at least three pieces of information, groupid, artifactid, and version
For each plug-in, it is defined in detail under its own configuration tag.
For more information about plugin, please refer to this website

Dependency

Here is an example of adding JUnit dependency

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

To add a dependency, you need to declare at least four properties:Groupid, artifactid, version, and scope。 Where scope refers to which stage of compilation the dependency is used, and its value can beCompile test and runtime
By default, all dependent files are located in${user.home}/.m2/repositoryUnder the directory. Maven will download the dependency that is not local to this directory and point to it in the project.

Plugin VS Dependency

There may be some confusion between the concepts of plugin and dependency. It seems that both of them are developed by referencing external tools. But these two concepts are totally different.
The plug-in is bound to the specified life cycle and triggered in a specific life cycle. We don’t use plug-ins to help us program.
But dependency means that we will use dependent files to help development, such as the well-knownJUnit。 We will use some of the functions in it for re coding development.

How to compile multiple projects at the same time?

First of all, we need to know that POM files can be inherited. That is, we can have a parent POM file and multiple child POM files. The format of the table of contents is as follows:

+- pom.xml
+- my-app
| +- pom.xml
| +- src
|   +- main
|     +- java
+- my-webapp
| +- pom.xml
| +- src
|   +- main
|     +- webapp

The following statement should be declared in the parent POM to add each module

<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.mycompany.app</groupId>
  <artifactId>app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
 
  <modules>
    <module>my-app</module>
    <module>my-webapp</module>
  </modules>
</project>

Because my webapp needs my app dependency, add the following statement to my webapp / pom.xml file

  <dependencies>
    <dependency>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    ...
  </dependencies>

Finally, add the following statements in my webapp / pom.xml and my app to inherit the parent POM file

  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

Finally, execute in the total root directory of the projectmvn verifyYou can see that the project starts to compile together

Maven Instruction Summary

mvn archetype:create -DgroupId=packageName -DartifactId=projectName: create a normal Maven project
mvn archetype:create -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp : create a maven web application
mvn compile: compile source code
mvn test: perform test
mvn test-compile: compile source and test code
mvn package: package project aspackagingThe format declared under the tag, in this case jar format. By default, the generated jar package is located in${basedir}/targetUnder the directory.
mvn compile:
mvn clean: to clear the previous compilation results, it is intuitive to delete${basedir}/targetFolder
mvn site: website for automatically generating project profiles
mvn verify: run any checks to verify that the package is valid and meets quality standards
This article will be updated continuously~~~

It took so long to review the series one: Maven focuses on learning
Want to know more about development technology, interview course and Internet Co push, welcome to my WeChat public address! The welfare will be paid irregularly~