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:
- Cleanup: Delete files generated by previous compilation in preparation for new code compilation.
- Compile: compile source code to execute code, support batch compilation;
- Testing: Verify that the function is normal by executing the test code, and it also supports batch testing;
- Reports: files that generate test results by executing tests;
- packaging: Talk about the project
class
Files and configuration files are packed into a compressed file. For general programs, the packaged zip file extension is usually.jar
For Web applications, the zip file extension is usually.war
; - Install: Install the packaged files to the local repository;
- Deploy: Get the program running.
Maven Core Concepts
- Pom: Project object Model. Maven manages JAR dependencies by treating the project as a model that controls Maven’s process of building the project.
- Directory structure: Maven’s directories and file locations are conventions;
- Coordinates: A unique identifier used to represent a resource;
- Dependency management: Use JAR files to manage projects;
- Warehouse management: resource storage path;
- Life cycle: The process by which Maven tools build projects;
- Plugins and goals: The tools for performing Maven builds are plugins
- inheritance
- 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.class file |
mvn test |
test | Project test |
mvn package |
packaging | According to the user configuration, package the project asjar Package orwar 包 |
mvn install |
The installation | Manually install one in the local warehousejar |
mvn deploy |
upload | willjar Upload 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:
- 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
- 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;
- It will then ask you to type in
groupId
,artifactId
,version
,package
Information such as;
- And then you confirm the information;
- 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.