• Maven--->学习心得--->maven的配置文件pom.xml


    1.概述:

    maven就是通过其配置文件pom.xml来管理jar包的,所以了解pom.xml文件结构是十分必要的,本文讲述pom.xml相关知识。

    2.参考资料:

        1)maven官网:documentation,POM(英文文档)

                该网址上documentation讲述了如下内容

              • What is a POM?
              • Super POM父pom.xml,你创建的maven工程的pom.xml实际上会自动继承Super POM.xml,也就是自动继承了SuperPOM.xml中定义的默认配置信息
              • Minimal POM
              • Project Inheritance工程继承
                • Example 1
                • Example 2
                Project Aggregation工程聚合
                • Example 3
                • Example 4
              • Project Inheritance vs Project Aggregation
                • Example 5
              • Project Interpolation and Variables工程插值以及变量
                • Available Variables   

        2)maven官网:pom.xml的各个标签的含义

        3)maven官网:POM Reference

               

    3.学习心得:

        3.1)基础知识点

            • POM:project object model,A Project Object Model or POM is the fundamental unit of work in Maven,it is a xml file 
            • Super POM:The Super POM is Maven's default POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects.  关于SuperPOM.xml 文件的内容,参见maven官网相关documentation: 不同版本的maven的SuperPOM.xml的具体内容(参见本文档中Super POM部分) 
            • pom.xml内容:
              • pom.xml中可以定义你的project的基本信息(such as the project version, description, developers, mailing lists)
              • 可以配置相关插件plugins,(maven插件,如打包插件、javadoc插件...
              • 可以配置the project dependencies(即你的project所依赖的三方jar包、库文件等) 
              • 还可以定义你的整个工程的目录结构(如配置src/main/java的编译结果存放至target/classes目录下,而src/test/java下文件的编译结果存放在target/test-classes文件夹下),这些东西在Super POM.xml中都有default value 
              • 小结:你可以在你的project的pom.xml中编辑如下几种内容,

                  • dependencies
                  • developers and contributors
                  • plugin lists (including reports)
                  • plugin executions with matching ids
                  • plugin configuration
                  • resources
            •   

         

        3.2)如何编写你的工程的pom.xml

            • step1,了解其实你的pom.xml是会自动继承SuperPOM.xml的,也就是说你的pom.xml实际上会自动继承maven的一些默认配置,如默认情况下你的project会被打包成jar包,默认情况下已经集成了maven的相关插件如javadoc这个plugin...(更多默认配置请参看SuperPOM.xml文件的具体内容) 。
                •  注意:If the configuration details are not specified, Maven will use their defaults. (即SuperPOM.xml中的相关配置) 
                • 你的project除了可以继承SuperPOM.xml之外,还可以继承其他的pom.xml(如某个自定义的pom.xml),具体配置方法参见如下网址documentation的"Minimal POM--->Project Inheritance工程继承--->Example 1以及Example2"

                •  
            • step2,编写你的pom.xml,先使其具备最基本的配置信息
                • The minimum requirement for a POM are the following:

                  • project root
                  • modelVersion - should be set to 4.0.0
                  • groupId - the id of the project's group.
                  • artifactId - the id of the artifact (project)
                  • version - the version of the artifact under the specified group

                  Here's an example: 

                  <project>
                  <modelVersion>4.0.0</modelVersion>
                  <groupId>com.mycompany.app</groupId>
                  <artifactId>my-app</artifactId>
                  <version>1</version>
                  </project>
                  
            • step3,经过step2之后,你的工程的基本信息都已经定义好了,下面可以针对项目的进一步进展在pom.xml中配置其他可以配置的信息
                  • 例子一,修改你的pom.xml使得其父POM不再是SuperPOM.xml而是其他的pom.xml(如使新工程的pom.xml继承你的另一个工程的pom.xml) 
                  • 例子二,将几个maven projects集合在同一个parent project中(通过配置parent project的pom.xml来实现)    
                  • 例子三,pom.xml文件支持“变量”,也就是说pom.xml文件中可以使用变量
                      • 涉及到的标签: <properties>在pom.xml中定义变量,定义的这些变量可以被pom.xml的其他标签使用,可以将pom.xml的其它标签的值赋值成这里所定义的变量的值
                          •     <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
                            •     <project.version>**</project.version>
                            •     <project.groupId>**</project.groupId>  
                            •     <project.build.sourceDirectory>
                            •   <project.basedir>The directory that the current project resides in.
                            •     <project.baseUri>The directory that the current project resides in, represented as an URI. Since Maven 2.1.0
                            •     <maven.build.timestamp>The timestamp that denotes the start of the build. Since Maven 2.1.0-M1
                          •        </properties> 
                      • 具体使用:
                          • step1,在pom.xml的<properties> 标签中定义想要使用的变量,
                          • step2,使用step1中所定义的变量
                          • <project>
                              ...
                              <properties>
                                <mavenVersion>2.1</mavenVersion>
                              </properties>
                              <dependencies>
                                <dependency>
                                  <groupId>org.apache.maven</groupId>
                                  <artifactId>maven-artifact</artifactId>
                                  <version>${mavenVersion}</version>
                                </dependency>
                                <dependency>
                                  <groupId>org.apache.maven</groupId>
                                  <artifactId>maven-project</artifactId>
                                  <version>${mavenVersion}</version>
                                </dependency>
                              </dependencies>
                              ...
                            </project>

        

     

      3.3)pom.xml文件中各个标签详解

            相关资料:官网documentation

            常用标签总结如下: 

    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/>
    
      <parent>
        <groupId/>
        <artifactId/>
        <version/>
        <relativePath/>
      </parent>
    
      <groupId/>
      <artifactId/>
      <version/>
      <packaging/>
    
      <name/>
      <description/>
      <url/>
      <inceptionYear/>
      <organization>
        <name/>
        <url/>
      </organization>
      <licenses>
        <license>
          <name/>
          <url/>
          <distribution/>
          <comments/>
        </license>
      </licenses>
    
      <developers>
        <developer>
          <id/>
          <name/>
          <email/>
          <url/>
          <organization/>
          <organizationUrl/>
          <roles/>
          <timezone/>
          <properties>
            <key>value</key>
          </properties>
        </developer>
      </developers>
      <contributors>
        <contributor>
          <name/>
          <email/>
          <url/>
          <organization/>
          <organizationUrl/>
          <roles/>
          <timezone/>
          <properties>
            <key>value</key>
          </properties>
        </contributor>
      </contributors>
    
      <mailingLists>
        <mailingList>
          <name/>
          <subscribe/>
          <unsubscribe/>
          <post/>
          <archive/>
          <otherArchives/>
        </mailingList>
      </mailingLists>
    
      <prerequisites>
        <maven/>
      </prerequisites>
    
      <modules/>
    
      <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
      </scm>
      <issueManagement>
        <system/>
        <url/>
      </issueManagement>
      <ciManagement>
        <system/>
        <url/>
        <notifiers>
          <notifier>
            <type/>
            <sendOnError/>
            <sendOnFailure/>
            <sendOnSuccess/>
            <sendOnWarning/>
            <address/>
            <configuration>
              <key>value</key>
            </configuration>
          </notifier>
        </notifiers>
      </ciManagement>
    
      <distributionManagement>
        <repository>
          <uniqueVersion/>
          <releases>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </releases>
          <snapshots>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </snapshots>
          <id/>
          <name/>
          <url/>
          <layout/>
        </repository>
        <snapshotRepository>
          <uniqueVersion/>
          <releases>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </releases>
          <snapshots>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </snapshots>
          <id/>
          <name/>
          <url/>
          <layout/>
        </snapshotRepository>
        <site>
          <id/>
          <name/>
          <url/>
        </site>
        <downloadUrl/>
        <relocation>
          <groupId/>
          <artifactId/>
          <version/>
          <message/>
        </relocation>
        <status/>
      </distributionManagement>
    
      <properties>
        <key>value</key>
      </properties>
    
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId/>
            <artifactId/>
            <version/>
            <type/>
            <classifier/>
            <scope/>
            <systemPath/>
            <exclusions>
              <exclusion>
                <artifactId/>
                <groupId/>
              </exclusion>
            </exclusions>
            <optional/>
          </dependency>
        </dependencies>
      </dependencyManagement>
      <dependencies>
        <dependency>
          <groupId/>
          <artifactId/>
          <version/>
          <type/>
          <classifier/>
          <scope/>
          <systemPath/>
          <exclusions>
            <exclusion>
              <artifactId/>
              <groupId/>
            </exclusion>
          </exclusions>
          <optional/>
        </dependency>
      </dependencies>
    
      <repositories>
        <repository>
          <releases>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </releases>
          <snapshots>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </snapshots>
          <id/>
          <name/>
          <url/>
          <layout/>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <releases>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </releases>
          <snapshots>
            <enabled/>
            <updatePolicy/>
            <checksumPolicy/>
          </snapshots>
          <id/>
          <name/>
          <url/>
          <layout/>
        </pluginRepository>
      </pluginRepositories>
    
      <build>
        <sourceDirectory/>
        <scriptSourceDirectory/>
        <testSourceDirectory/>
        <outputDirectory/>
        <testOutputDirectory/>
        <extensions>
          <extension>
            <groupId/>
            <artifactId/>
            <version/>
          </extension>
        </extensions>
        <defaultGoal/>
        <resources>
          <resource>
            <targetPath/>
            <filtering/>
            <directory/>
            <includes/>
            <excludes/>
          </resource>
        </resources>
        <testResources>
          <testResource>
            <targetPath/>
            <filtering/>
            <directory/>
            <includes/>
            <excludes/>
          </testResource>
        </testResources>
        <directory/>
        <finalName/>
        <filters/>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId/>
              <artifactId/>
              <version/>
              <extensions/>
              <executions>
                <execution>
                  <id/>
                  <phase/>
                  <goals/>
                  <inherited/>
                  <configuration/>
                </execution>
              </executions>
              <dependencies>
                <dependency>
                  <groupId/>
                  <artifactId/>
                  <version/>
                  <type/>
                  <classifier/>
                  <scope/>
                  <systemPath/>
                  <exclusions>
                    <exclusion>
                      <artifactId/>
                      <groupId/>
                    </exclusion>
                  </exclusions>
                  <optional/>
                </dependency>
              </dependencies>
              <goals/>
              <inherited/>
              <configuration/>
            </plugin>
          </plugins>
        </pluginManagement>
        <plugins>
          <plugin>
            <groupId/>
            <artifactId/>
            <version/>
            <extensions/>
            <executions>
              <execution>
                <id/>
                <phase/>
                <goals/>
                <inherited/>
                <configuration/>
              </execution>
            </executions>
            <dependencies>
              <dependency>
                <groupId/>
                <artifactId/>
                <version/>
                <type/>
                <classifier/>
                <scope/>
                <systemPath/>
                <exclusions>
                  <exclusion>
                    <artifactId/>
                    <groupId/>
                  </exclusion>
                </exclusions>
                <optional/>
              </dependency>
            </dependencies>
            <goals/>
            <inherited/>
            <configuration/>
          </plugin>
        </plugins>
      </build>
    
      <reports/>
      <reporting>
        <excludeDefaults/>
        <outputDirectory/>
        <plugins>
          <plugin>
            <groupId/>
            <artifactId/>
            <version/>
            <reportSets>
              <reportSet>
                <id/>
                <reports/>
                <inherited/>
                <configuration/>
              </reportSet>
            </reportSets>
            <inherited/>
            <configuration/>
          </plugin>
        </plugins>
      </reporting>
    
      <profiles>
        <profile>
          <id/>
          <activation>
            <activeByDefault/>
            <jdk/>
            <os>
              <name/>
              <family/>
              <arch/>
              <version/>
            </os>
            <property>
              <name/>
              <value/>
            </property>
            <file>
              <missing/>
              <exists/>
            </file>
          </activation>
          <build>
            <defaultGoal/>
            <resources>
              <resource>
                <targetPath/>
                <filtering/>
                <directory/>
                <includes/>
                <excludes/>
              </resource>
            </resources>
            <testResources>
              <testResource>
                <targetPath/>
                <filtering/>
                <directory/>
                <includes/>
                <excludes/>
              </testResource>
            </testResources>
            <directory/>
            <finalName/>
            <filters/>
            <pluginManagement>
              <plugins>
                <plugin>
                  <groupId/>
                  <artifactId/>
                  <version/>
                  <extensions/>
                  <executions>
                    <execution>
                      <id/>
                      <phase/>
                      <goals/>
                      <inherited/>
                      <configuration/>
                    </execution>
                  </executions>
                  <dependencies>
                    <dependency>
                      <groupId/>
                      <artifactId/>
                      <version/>
                      <type/>
                      <classifier/>
                      <scope/>
                      <systemPath/>
                      <exclusions>
                        <exclusion>
                          <artifactId/>
                          <groupId/>
                        </exclusion>
                      </exclusions>
                      <optional/>
                    </dependency>
                  </dependencies>
                  <goals/>
                  <inherited/>
                  <configuration/>
                </plugin>
              </plugins>
            </pluginManagement>
            <plugins>
              <plugin>
                <groupId/>
                <artifactId/>
                <version/>
                <extensions/>
                <executions>
                  <execution>
                    <id/>
                    <phase/>
                    <goals/>
                    <inherited/>
                    <configuration/>
                  </execution>
                </executions>
                <dependencies>
                  <dependency>
                    <groupId/>
                    <artifactId/>
                    <version/>
                    <type/>
                    <classifier/>
                    <scope/>
                    <systemPath/>
                    <exclusions>
                      <exclusion>
                        <artifactId/>
                        <groupId/>
                      </exclusion>
                    </exclusions>
                    <optional/>
                  </dependency>
                </dependencies>
                <goals/>
                <inherited/>
                <configuration/>
              </plugin>
            </plugins>
          </build>
    
          <modules/>
    
          <distributionManagement>
            <repository>
              <uniqueVersion/>
              <releases>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </releases>
              <snapshots>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </snapshots>
              <id/>
              <name/>
              <url/>
              <layout/>
            </repository>
            <snapshotRepository>
              <uniqueVersion/>
              <releases>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </releases>
              <snapshots>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </snapshots>
              <id/>
              <name/>
              <url/>
              <layout/>
            </snapshotRepository>
            <site>
              <id/>
              <name/>
              <url/>
            </site>
            <downloadUrl/>
            <relocation>
              <groupId/>
              <artifactId/>
              <version/>
              <message/>
            </relocation>
            <status/>
          </distributionManagement>
    
          <properties>
            <key>value</key>
          </properties>
    
          <dependencyManagement>
            <dependencies>
              <dependency>
                <groupId/>
                <artifactId/>
                <version/>
                <type/>
                <classifier/>
                <scope/>
                <systemPath/>
                <exclusions>
                  <exclusion>
                    <artifactId/>
                    <groupId/>
                  </exclusion>
                </exclusions>
                <optional/>
              </dependency>
            </dependencies>
          </dependencyManagement>
          <dependencies>
            <dependency>
              <groupId/>
              <artifactId/>
              <version/>
              <type/>
              <classifier/>
              <scope/>
              <systemPath/>
              <exclusions>
                <exclusion>
                  <artifactId/>
                  <groupId/>
                </exclusion>
              </exclusions>
              <optional/>
            </dependency>
          </dependencies>
    
          <repositories>
            <repository>
              <releases>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </releases>
              <snapshots>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </snapshots>
              <id/>
              <name/>
              <url/>
              <layout/>
            </repository>
          </repositories>
          <pluginRepositories>
            <pluginRepository>
              <releases>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </releases>
              <snapshots>
                <enabled/>
                <updatePolicy/>
                <checksumPolicy/>
              </snapshots>
              <id/>
              <name/>
              <url/>
              <layout/>
            </pluginRepository>
          </pluginRepositories>
    
          <reports/>
          <reporting>
            <excludeDefaults/>
            <outputDirectory/>
            <plugins>
              <plugin>
                <groupId/>
                <artifactId/>
                <version/>
                <reportSets>
                  <reportSet>
                    <id/>
                    <reports/>
                    <inherited/>
                    <configuration/>
                  </reportSet>
                </reportSets>
                <inherited/>
                <configuration/>
              </plugin>
            </plugins>
          </reporting>
        </profile>
      </profiles>
    </project>

    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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>bupt.jinmensuyin.webapp</groupId>
      <artifactId>leapmotion-web</artifactId>
      <packaging>war</packaging>
      <version>0.0.1</version>
      <name>leapmotion-web Maven Webapp</name>
      <url>http://maven.apache.org</url>
      
      
      
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>leapmotion-web</finalName>
      </build>
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>4.2.8.RELEASE</version>
              </dependency>
          </dependencies>
      </dependencyManagement>
    </project>
    pom.xml中各个常用标签的意义:
    • project This is the top-level element in all Maven pom.xml files.
    • modelVersion This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
    • groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plugins.
    • artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
    • packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.
    • version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
    • name This element indicates the display name used for the project. This is often used in Maven's generated documentation.
    • url This element indicates where the project's site can be found. This is often used in Maven's generated documentation.
    • description This element provides a basic description of your project. This is often used in Maven's generated documentation.
    
    
     

                                          

    学习的过程中总会得到一些心得体会,认真地将它们记录下来并分享给每一个愿意花费时间去阅读它们的人,然后意外地收获某个读者的评论,从而激发出新的感想,是一件十分令人欢快的事。如果你也在研习这方面的知识,欢迎加入到我们的队伍中来,和我们一起进步吧(^_^)
  • 相关阅读:
    lintcode491 回文数
    lintcode514 栅栏染色
    lintcode433 岛屿的个数
    lintcode142 O(1)时间检测2的幂次
    lintcode166 链表倒数第n个节点
    lintcode539 移动零
    lintcode: Check Sum of Square Numbers
    lintcode: Missing String
    lintcode 二叉树的锯齿形层次遍历
    Mysql 游标的定义与使用方式
  • 原文地址:https://www.cnblogs.com/lxrm/p/6182422.html
Copyright © 2020-2023  润新知