pom.xml configuration file

Use Maven to manage third-party libraries Java project, the most important is through the pom.xml file, this section will explain the use of the file.

pom.xml file


To (四)通过 mvn 命令创建 Maven 项目project creation as an example. Open the pom.xml file in the root directory of the project.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.myapp.pro</groupId>
  <artifactId>myapp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>myapp</name>
  <url>http://maven.apache.org</url>

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

  </dependencies>

</project>
  • project: top-level elements pom.xml file;

  • modelVersion: POM specified version of the object model used. This value rarely changes.

  • groupId: uniquely identifies the specified organization or group to create a project. GroupId is to identify key projects, typically, this identification with the fully qualified name of the organization to define. For example, org.apache.maven.plugins all Maven plugins project-specific groupId.

  • artifactId: specify the base name of the main products produced by the project. The main products of the project is typically a JAR file. A second, generally used as source code package artifactId as the last part of the name. A typical product names used in this format: - (for example: myapp-1.0.jar).

  • version: version number of the product of the project. Maven helps you manage versions, you can often see SNAPSHOT this version, indicating that the project is in the development stage.

  • name: Displays the name of the project, usually for document maven generated.

  • url: Specifies the project site, typically used to document maven generated.

  • description: description of the project, usually for document maven generated.

Management of third-party libraries


To add a third-party library, the following are a few parameters we need to focus.

<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>

It is mainly dependent on the management of deployment. Currently you can use five values:

  • compile, the default values ​​for all stages, will be released along with the project.
  • provided, similar to compile, desired JDK, container, or the user may provide this dependency. As servlet.jar.
  • runtime, only at runtime, such as JDBC driver for running and testing phase.
  • test, the test is only used when, for compiling and running the test code. It will not be published with the project.
  • system, similar to the provided, required to provide explicit dependence comprising jar, Maven does not find it in the Repository.

By looking at the pom.xml configuration Junit, apparently some version of the old, we went to download the latest Junit4, but we do not know Maven specific version of how to do it? We can go to the central Maven repository query.

Maven repository: http://mavenrepository.com/

Through the website "Junit", Junit4 find the latest version of the configuration.
Here Insert Picture Description
Modify the configuration of the pom.xml Junit4.

<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>

Maven command or by third-party libraries (plugins) updated the project.

myapp > mvn clean

myapp > mvn idea:idea

About Maven plugin for reference: http://maven.apache.org/plugins/index.html

Guess you like

Origin blog.csdn.net/weixin_44474742/article/details/90574391