maven pom.xml说明


maven pom.xml说明

                           

官网:http://maven.apache.org/pom.html

                                     

                 

********************

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
     
    <!-- The Basics -->
    <parent>...</parent>          <!-- 继承的父模块 -->
    <groupId>...</groupId>        <!-- 项目groupId -->
    <artifactId>...</artifactId>  <!-- 项目artifactId -->
    <version>...</version>        <!-- 项目version -->
    <packaging>...</packaging>    <!-- 打包方式 -->
    <dependencies>...</dependencies>                  <!-- 项目导入的依赖 -->
    <dependencyManagement>...</dependencyManagement>  <!-- 管理子项目的依赖 -->
    <modules>...</modules>        <!-- 项目包含的模块 -->
    <properties>...</properties>  <!-- 自定义项目属性 -->
     
    <!-- Build Settings -->
    <build>...</build>            <!-- 项目构建:jar包名称、如何打包等 -->
    <reporting>...</reporting>    <!-- 项目site -->
     
    <!-- More Project Information -->
    <name>...</name>                         <!-- 项目名称 -->
    <description>...</description>           <!-- 项目描述 -->
    <url>...</url>                           <!-- 项目介绍网址(如果有) -->
    <inceptionYear>...</inceptionYear>       <!-- 项目创建年份 -->
    <licenses>...</licenses>                 <!-- 项目授权信息 -->
    <organization>...</organization>         <!-- 项目组织方 -->
    <developers>...</developers>             <!-- 项目开发者信息 -->
    <contributors>...</contributors>         <!-- 项目参与者信息 -->
     
    <!-- Environment Settings -->
    <issueManagement>...</issueManagement>   <!-- 项目发布管理 -->
    <ciManagement>...</ciManagement>         <!-- 项目持续集成管理 -->
    <mailingLists>...</mailingLists>         <!-- 项目相关用户邮件列表 -->
    <scm>...</scm>                           <!-- Software Configuration Management -->
    <prerequisites>...</prerequisites>       <!-- 项目执行需要满足的先决条件 -->
                                             <!-- The POM may have certain prerequisites in order to execute correctly -->
    <repositories>...</repositories>                       <!-- 项目依赖仓库 -->
    <pluginRepositories>...</pluginRepositories>           <!-- 项目创建仓库 -->
    <distributionManagement>...</distributionManagement>   <!-- 项目发布管理,可将jar包发布到私服 -->
    <profiles>...</profiles>                               <!-- 项目profile -->
</project>

                        

*************

parent:继承的父模块

             

示例

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

                                 

*************

packaging:项目打包方式

When no packaging is declared, Maven assumes the packaging is the default: jar. 
# 如果packaging没有设置,默认为 jar

The valid types are Plexus role-hints (read more on Plexus for a explanation of roles and 
role-hints) of the component role org.apache.maven.lifecycle.mapping.LifecycleMapping. The 
current core packaging values are: pom, jar, maven-plugin, ejb, war, ear, rar
# 可选打包方式:pom、jar、maven-plugin、ejb、war、ear、rar

The packaging type required to be pom for parent and aggregation (multi-module) projects
# 多模块项目中,父类项目packaging需要设置为pom

                          

pom 打包

# 项目包括含模块user-info、user-service、user-web,打包方式需要设置为pom
    <!-- 父模块packaging:pom -->
    <packaging>pom</packaging>
    <modules>
        <module>user-info</module>
        <module>user-service</module>
        <module>user-web</module>
    </modules>

             

*************

dependencies:项目依赖的jar包

         

可选属性说明

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>

    <dependency>
      ...
    </dependency>
  </dependencies>

groupId、artifactId、version:项目坐标(组id、标识id、版本)
type:依赖类型,可选值ejb-client、test-client、jar(默认)
scope:compile、provided、runtime、test、system
systemPath:scope为system时,必须设置该值
optional:依赖可选,即使未导入,也不影响项目启动

                         

示例

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

            

*************

dependencymanagent:管理子项目依赖

               

dependencyManagement is used by a POM to help manage dependency information 
across all of its children. 
# dependencyManagent用来管理子项目的依赖信息

If the my-parent project uses dependencyManagement to define a dependency on
junit:junit:4.12, then POMs inheriting from this one can set their dependency 
giving the groupId=junit and artifactId=junit only and Maven will fill in the
version set by the parent. The benefits of this method are obvious. Dependency
details can be set in one central location, which propagates to all inheriting POMs.
# 如果父项目(my-paret)在dependencyManagement定义了依赖junit:junit:4.12
# 子项目只使用了groupId=junit、artifactId=junit,没有指定版本,则子项目会使用4.12
# 好处很明显,父项目可集中管理子项目的依赖版本

Note that the version and scope of artifacts which are incorporated from transitive 
dependencies are also controlled by version specifications in a dependency 
management section. This can lead to unexpected consequences. Consider a case in 
which your project uses two dependences, dep1 and dep2. dep2 in turn also uses 
dep1, and requires a particular minimum version to function. If you then use 
dependencyManagement to specify an older version, dep2 will be forced to use the 
older version, and fail
# 注意,父项目也会管理子项目的传递依赖
# 父项目管理依赖dep1、dep2
# 子项目依赖dep2依赖dep1,并且dep2依赖的dep1有最低版本要求
# 如果父项目定义的dep1低于最低版本,项目构建失败

                

示例

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-amqp</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-blueprint</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-broker</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      ...
  </dependencyManagement>

           

*************

peoperties:项目属性

          

  <properties>
    <activemq.version>5.16.3</activemq.version>
    ...
  </properties>

key:activemq.version
value:5.16.3

                

属性使用

# 环境变量 env.X
Prefixing a variable with "env." will return the shell's environment variable
变量使用:Path=value ==> ${env.Path}

# java系统变量:java System Properties
All properties accessible via java.lang.System.getProperties() are available as POM properties
# 如果java系统变量key可以通过System.getProperty(key)获取,则在pom中可使用${key}引用


# settings.xml 属性:setting.key
A dot (.) notated path in the settings.xml will contain the corresponding element's value

# settings.xml属性使用示例
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <localRepository>/path/to/local/repo</localRepository>
  <offline>false</offline>
  ...
</settings>
${settings.localRepository}:引用本地仓库地址
${settings.offline}:引用offline属性


# pom.xml 属性:project.key
A dot (.) notated path in the POM will contain the corresponding element's value.

# pom.xml属性使用示例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.5.5</version>
  <packaging>pom</packaging>

  ...
</project>
${project.modelVersion}:引用modelVersion
${project.groupId}:引用groupId
${project.artifactId}:引用artifactId
${project.version}:引用version
${project.packaging}:引用packaging


# pom.xml 自定义属性
  <properties>
    <activemq.version>5.16.3</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
  </properties>
${activemq.version}:引用activemq.version属性
${antlr2.version}:引用antlr2.version属性

             

*************

build:项目构建

          

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>
 
  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <!-- 根据profile不同,执行不同的build过程 -->
      <build>...</build>
    </profile>
  </profiles>
</project>

                 

基本build元素

<build>
  <defaultGoal>install</defaultGoal>
  <directory>${basedir}/target</directory>
  <finalName>${artifactId}-${version}</finalName>
  <filters>
    <filter>filters/filter.properties</filter>
  </filters>
  ...
</build>

finalName:项目输出名称
directory:项目输出目录,默认为:${basedir}/target
filters:过滤的属性文件,默认过滤目录:${basedir}/src/main/filters
defaultGoal:the default goal or phase to execute if none is given

                        

默认目录

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <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>
    ...
  </build>
</project>

sourceDirectory:源java目录
scriptSourceDirectory:源script目录
testSourceDirectory:源测试目录

outputDirectory:sourceDirectory内容build后的输出目录
testOutputDirectory:testSourceDirectory内容build厚的输出目录

                    

resources 子标签

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <build>
    ...
    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    <testResources>
      ...
    </testResources>
    ...
  </build>
</project>

targetPath:资源输出目录
directory:源目录
filtering:是否过滤源目录
includes:输出到targetPath的文件
excludes:不输出到文件

                            

plugins 子标签

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <build>
    ...
    <plugins>

      <!-- 项目build过程中使用的插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <extensions>false</extensions>
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>...</dependencies>
        <executions>...</executions>
      </plugin>
    </plugins>
  </build>
</project>

                         

pluginManagement 子标签

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <build>
    ...
    <pluginManagement>

      <!-- 管理子项目build时使用的插件 -->
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.6</version>
          <executions>
            <execution>
              <id>pre-process-classes</id>
              <phase>compile</phase>
              <goals>
                <goal>jar</goal>
              </goals>
              <configuration>
                <classifier>pre-process</classifier>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
    ...
  </build>
</project>

                        

extensions 子标签

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <build>
    ...
    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ftp</artifactId>
        <version>1.0-alpha-3</version>
      </extension>
    </extensions>
    ...
  </build>
</project>

                          

*************

reporting:项目site

          

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <reporting>
    <outputDirectory>${basedir}/target/site</outputDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.0.1</version>
        <reportSets>
          <reportSet></reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

                          

*************

repositories:项目仓库

          

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...

  <!-- 项目仓库 -->
  <!-- 加载依赖现在本地仓库查找,本地仓库不存在,则去远程仓库加载 -->
  <repositories>
    <repository>
      <releases>
        <enabled>false</enabled>
        <updatePolicy>always</updatePolicy>
        <checksumPolicy>warn</checksumPolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
        <checksumPolicy>fail</checksumPolicy>
      </snapshots>
      <name>Nexus Snapshots</name>
      <id>snapshots-repo</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <layout>default</layout>
    </repository>
  </repositories>

  <!-- 插件仓库 -->
  <pluginRepositories>
    ...
  </pluginRepositories>
  ...
</project>

                           

*************

distributionManagement:项目发布

          

    <!-- 常用于将jar包发布到私有仓库 -->
    <distributionManagement>
        <repository>
            <id>lihu</id>         <!-- 对应settings.xml中的server -->
            <url>http://192.168.57.120:8081/repository/lihu-releases/</url>
        </repository>             <!-- release jar包发布地址 -->
 
        <snapshotRepository>
            <id>lihu</id>
            <url>http://192.168.57.120:8081/repository/lihu-snapshots/</url>
        </snapshotRepository>     <!-- snapshot jar包发布地址 -->
    </distributionManagement>

                         

*************

profiles:项目环境

                  

  <profiles>

    <!-- 根据不同条件激活profile,实现同一项目执行不同构造过程 -->
    <profile>
      <id>test</id>
      <activation>...</activation>
      <build>...</build>
      <modules>...</modules>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <dependencies>...</dependencies>
      <reporting>...</reporting>
      <dependencyManagement>...</dependencyManagement>
      <distributionManagement>...</distributionManagement>
    </profile>
  </profiles>

                      

*************

其他标签

          

licenses:项目许可信息

  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>

                  

organization:项目组织方

  <organization>
    <name>Codehaus Mojo</name>
    <url>http://mojo.codehaus.org</url>
  </organization>

                             

developers:项目开发者信息

  <developers>
    <developer>
      <name>Pivotal</name>
      <email>[email protected]</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>

                        

contributors:项目参与者信息

  <contributors>
    <contributor>
      <name>Noelle</name>
      <email>[email protected]</email>
      <url>http://noellemarie.com</url>
      <organization>Noelle Marie</organization>
      <organizationUrl>http://noellemarie.com</organizationUrl>
      <roles>
        <role>tester</role>
      </roles>
      <timezone>America/Vancouver</timezone>
      <properties>
        <gtalk>[email protected]</gtalk>
      </properties>
    </contributor>
  </contributors>

                         

issueManagement:it's primarily used for generating project documentation

  <issueManagement>
    <system>Bugzilla</system>
    <url>http://127.0.0.1/bugzilla/</url>
  </issueManagement>

                         

ciManagement:项目异常时发出提醒

  <ciManagement>
    <system>continuum</system>
    <url>http://127.0.0.1:8080/continuum</url>
    <notifiers>
      <notifier>
        <type>mail</type>                    <!-- 邮件通知 -->
        <sendOnError>true</sendOnError>      <!-- 错误时触发 -->
        <sendOnFailure>true</sendOnFailure>  <!-- 失败时触发 -->
        <sendOnSuccess>false</sendOnSuccess> <!-- 成功时不触发 -->
        <sendOnWarning>false</sendOnWarning> <!-- 有警告信息时不触发-->
        <configuration><address>[email protected]</address></configuration>
      </notifier>
    </notifiers>
  </ciManagement>

                         

mailingLists:项目相关人员的邮件信息

  <mailingLists>
    <mailingList>
      <name>User List</name>
      <subscribe>[email protected]</subscribe>
      <unsubscribe>[email protected]</unsubscribe>
      <post>[email protected]</post>
      <archive>http://127.0.0.1/user/</archive>
      <otherArchives>
        <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
      </otherArchives>
    </mailingList>
  </mailingLists>

                             

scm:Software Configuration Management, also called Source Code/Control Management

  <scm>
    <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
    <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
    <tag>HEAD</tag>
    <url>http://127.0.0.1/websvn/my-project</url>
  </scm>

                             

prerequisites:项目执行需要先满足的条件

  <prerequisites>
    <maven>2.0.6</maven>      <!-- 项目中maven的最低版本是2.0.6 -->
  </prerequisites>

                    

                            

猜你喜欢

转载自blog.csdn.net/weixin_43931625/article/details/120683173
今日推荐