Maven Project Object Model

https://maven.apache.org/pom.html

项目对象模型

采用xml描述项目的所有配置信息。

Super POM

Super POM是Maven项目的默认设置,Maven项目的POM文件继承自Super POM,项目文件中的设置将覆盖默认设置。

默认仓库
<repositories>  <!--  <pluginRepositories> 也是同一地址 -->
   <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
         <enabled>false</enabled>
      </snapshots>
   </repository>
</repositories>

Maven – Settings Reference (apache.org): certain settings such as username and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml.

变量

Any field of the model that is a single value element can be referenced as a variable.

特殊变量
变量含义

元素说明

Maven Model – Maven (apache.org)

项目信息

<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>

   <!--unique id: groupId-artifactId-version-->
   <groupId>gary.deeplearning4j</groupId>  <!--unique org ID-->
   <artifactId>dl4j-start-app</artifactId> <!--project name-->
   <version>1.0.0.0</version>

   <packaging>jar</packaging>  <!--pom for parent project-->

   <!--Information of project-->
   <name>DeepLearning4j Start</name>
   <url>http://deaplearning.gary.net</url>
   <description>
      The start application for learning the Deeplearnig4J project.
   </description >

   <properties>...</properties>
   <dependency>...</dependency>
   <build>....</build>
</project>

POM信息

标签说明
project根元素:声明POMXML命名空间。
modelVersionPOM版本,通常为4.0.0,不常变化

项目信息

标签说明
groupId创建项目的组织名称,通常基于有效的组织域名。
artifactId输出文件名称。
packaging输出文件打包格式(默认为*.jar)。
version输出文件版本。
name项目的显示名称。
url项目的网站。
description项目的描述信息。
modules多项目管理。
parent多项目的父项目信息。
licences
organization
developers
contributors

环境变量

获取环境变量:

env.PATH:返回系统环境变量;

project.X.Y:返回POM.xml中对应元素的值;

settings.xxx:获取settings.xml中元素的值;

Java环境变量,例如${java.home}

xxx.yyy.zzz:在properties标签中设置的元素。

标签说明
properties声明环境变量用于在文件其他部分进行替换(${dl4j.version})。
作为过滤器
maven.compiler.source
maven.compiler.target
为Maven的编译任务指定Java版本(例如1.811)。
<!--Define enviromnment variables for Maven-->
<properties>
  <nd4j.version>1.0.0-beta4</nd4j.version>
  <dl4j.version>1.0.0-beta4</dl4j.version>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

特殊变量:${project.basedir} 项目根目录。

依赖库

标签说明
dependencies依赖库列表,其子元素为dependency
dependency依赖库声明,其子元素包括:groupIdartifactIdversion
依赖库的信息可以在Maven软件仓库中查询。
dependencyManagement用于父项目管理依赖项配置。
<dependencies>
  <dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>${nd4j.backend}</artifactId>
    <version>${nd4j.version}</version>
  </dependency>
  <dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-nlp</artifactId>
    <version>${dl4j.version}</version>
  </dependency>
</dependencies>

构建工具配置

标签说明
build构建工具配置。
plugins位于build子节点,构建工具列表。
pluginManagement位于build子节点,其子节点plugins/plugin声明全局的插件配置信息,任何子项目要使用其中的插件还需要在build/plugins子节点中声明。
plugin构建工具声明,包括工具的groupIdartifactIdversion以及相关配置信息。
configuration构建工具配置信息,其包含的子元素取决于具体工具。
executions指定构建工具的执行方式。
resourcesspecifying where resources exist within your project.
<build>
  <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
  <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
  <outputDirectory>${basedir}/target/classes</outputDirectory>
  <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
  <defaultGoal>install</defaultGoal>  <!--default goal/phase to execute-->
  <directory>${basedir}/target</directory> <!--output dir-->
  <finalName>${artifactId}-${version}</finalName>  <!--output name-->
  ...
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.1.0</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>gary.App</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>