首页 > 代码库 > Maven--->学习心得--->maven的配置文件pom.xml

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       

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) 
          • 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中配置其他可以配置的信息
              •     

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