This is the fourth day of my participation in the August Text Challenge.More challenges in August

preface

In the previous article, we covered what Maven is and how to install it, and we also covered simple configuration after installing Maven. Today we will focus on the core concepts in Maven and the commands Maven uses in daily use.

Maven core

build

The so-called build refers to the construction of the project, which is process-oriented, the sum of a series of steps, mainly including the compilation, running, testing, packaging and deployment of the project code and so on. Maven supports the following builds:

  1. Cleanup: Delete files generated by previous compilation in preparation for new code compilation.
  2. Compile: compile source code to execute code, support batch compilation;
  3. Testing: Verify that the function is normal by executing the test code, and it also supports batch testing;
  4. Reports: files that generate test results by executing tests;
  5. packaging: Talk about the projectclassFiles and configuration files are packed into a compressed file. For general programs, the packaged zip file extension is usually.jarFor Web applications, the zip file extension is usually.war;
  6. Install: Install the packaged files to the local repository;
  7. Deploy: Get the program running.

Maven Core Concepts

  1. Pom: Project object Model. Maven manages JAR dependencies by treating the project as a model that controls Maven’s process of building the project.
  2. Directory structure: Maven’s directories and file locations are conventions;
  3. Coordinates: A unique identifier used to represent a resource;
  4. Dependency management: Use JAR files to manage projects;
  5. Warehouse management: resource storage path;
  6. Life cycle: The process by which Maven tools build projects;
  7. Plugins and goals: The tools for performing Maven builds are plugins
  8. inheritance
  9. The aggregation

Common commands

Some common commands in Maven are as follows:

The command meaning function
mvn clean Clean up the Used to clean up compiled files
mvn compile compile Compile Java source code into bytecode.classfile
mvn test test Project test
mvn package packaging According to the user configuration, package the project asjarPackage orwar
mvn install The installation Manually install one in the local warehousejar
mvn deploy upload willjarUpload to private server

Use Archetype to generate the project skeleton

In fact, in order to quickly create a Maven skeleton, we can use Maven Archetype as follows:

  1. First go to the directory where you want to create the skeleton of the project and execute the following command:
mvn archetype:generate
Copy the code
  1. There will be a long output, there are a variety of archetypes available for you to choose, choose what you need, and then input the corresponding number can be;

  1. It will then ask you to type ingroupId,artifactId,version,packageInformation such as;

  1. And then you confirm the information;

  1. After the final confirmation, press Enter to generate.

The project structure

The generated directory contains the following files:

The SRC directory contains the project’s main code and resources, as well as test-related code and resources. Pom.xml defines all the configuration for the project.

Suppose we have a HelloWorld project. When we use Maven to manage it, the project structure should look something like this:

HelloWorld/
    |----src/
    |----|----main/
    |----|----|----java/
    |----|----|----resources/
    |----|----test/
    |----|----|----java/
    |----|----|----resources/
    |----pom.xml
Copy the code

HelloWorld is the root directory, which is the name of our project, and SRC is the main source directory, which is divided into Java and resources. The Java directory is mainly used to store the package and the Java files in the package. The Resources directory holds the configuration files to be used in the program.

The test directory has the same structure as SRC, except that, as the name suggests, it is used to store the code and configuration files for testing, and this is not a mandatory option, you can choose according to your own needs.

Finally, there is pom.xml, the core file for every project managed by Maven, which is a must for subsequent dependency management and other operations.

conclusion

Ok, so that’s some common Maven commands and how to use Archetype to build the skeleton of a project, as well as the overall structure of a project created using Maven. If you think it will help you, please click like to follow a wave. That’s it for today, stay tuned for the next installment of our series on dependency management using Maven.