chapter
POM is an acronym for Project Object Model / project object model. pom.xml file contains the dependencies, build directory, source directory, test source directory, plugin, targets, and information and configuration information. Maven pom.xml read the file, and then execute the build target.
The basic elements of pom.xml file
A pom.xml file needs to contain at least the following elements:
element | description |
---|---|
project | pom document root element representing a project |
modelVersion | Child elements of project elements, designated modelVersion version number should be set to 4.0.0 |
groupId | group element sub-element project, specify the project belongs |
artifactId | Child elements of project elements, project deliverables ID, project deliverables means the final delivery of the project file, such as a jar, zip, war, etc. |
version | Child elements of project elements, project (deliverables) version number |
pom.xml example:
<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.qikegu.demo</groupId>
<artifactId>mybatis-demo</artifactId>
<version>0.0.1</version>
</project>
Common elements pom.xml file
In addition to the basic elements, here are some common elements pom file:
element | description |
---|---|
packaging | Definition of the type of packaging, such as jar, war |
name | Specify the project name |
url | Url specified item |
dependencies | It represents the project dependencies list |
dependency | Child elements that represent individual dependencies, this element is the element of dependency list |
scope | Individual dependencies scope, a scope can compile, test, runtime, provided, one of the System, is intended to define the scope of the dependency scope, such as test, it indicates only work when dependency test |
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>com.qikegu.demo</groupId>
<artifactId>mybatis-demo</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>mybatis demo </name>
<url>http://www.qikegu.com</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
pom inheritance
All Maven POM files are inherited from a parent POM. If not specified, the default file POM inherits from basic POM (base POM). As shown below:
POM file can explicitly inherits from another POM file, 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>
<parent>
<groupId>com.qikegu.demo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
...
</project>
Child POM file can override the settings parent POM file.
Pom take effect
When using pom inheritance, difficult to clearly know the final entry into force is kind of how pom, pom can show the contents of the final entry into force of the following commands:
mvn help:effective-pom
Maven project directory structure
Maven projects have a standard directory structure. If you follow the directory structure, you need to specify the source code, test code, etc. POM file in the directory.
For more information on, refer to the official documentation .
Here are a few of the most important directories:
- src
- main
- java
- resources
- test
- java
- resources
- target
- src directory is the root directory of the source code and the test code, main program source code directory (without test directory) of the root directory, test directory contains the test source code.
- resources directory is a resource directory.
- If the project is a web application, webapp directory that contains the Java web applications. webapp directory will be the root directory of the web application, including the WEB-INF directory and so on.
- Target directory created by Maven, Maven contains all compiled classes generated, JAR files, and so on. In the implementation of clean build phase, the target directory will be cleared.