Maven的pom文件详解

我就是我 2023-06-30 02:55 71阅读 0赞

​​​​​​POM基本项目信息

groupId:项目或者组织的唯一标志

artifactId:项目的通用名称

version:项目的版本

packaging:打包机制,如pom,jar,maven-plugin,ejb,war,ear,rar,par

name:用户描述项目的名称,无关紧要的东西,可选

url:应该是只是写明开发团队的网站,无关紧要,可选

POM依赖关系

依赖关系示例

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.0</version>
  6. <type>jar</type>
  7. <scope>test</scope>
  8. <optional>true</optional>
  9. </dependency>
  10. <dependency>
  11. <groupId>com.alibaba.china.shared</groupId>
  12. <artifactId>alibaba.apollo.webx</artifactId>
  13. <version>2.5.0</version>
  14. <exclusions>
  15. <exclusion>
  16. <artifactId>org.slf4j.slf4j-api</artifactId>
  17. <groupId>com.alibaba.external</groupId>
  18. </exclusion>
  19. ....
  20. </exclusions>
  21. ......
  22. </dependencies>

其中groupId, artifactId, version这三个组合标示依赖的具体工程,而且 这个依赖工程必需是maven中心包管理范围内的。

dependency里属性介绍

type:默认为jar类型,常用的类型有:jar,ejb-client,test-jar…,可设置plugins中的extensions值为true后在增加 新的类型,

scope:是用来指定当前包的依赖范围,maven的依赖范围

optional:设置指依赖是否可选,默认为false,即子项目默认都继承,为true,则子项目必需显示的引入,与dependencyManagement里定义的依赖类似 。

exclusions:如果X需要A,A包含B依赖,那么X可以声明不要B依赖,只要在exclusions中声明exclusion.

exclusion:是将B从依赖树中删除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external ,但是alibaba.apollo.webx是集成了com.alibaba.external,所以就需要排除掉.

parent的使用方法

如果一个工程是parent或者aggregation(即mutil-module的)的,那么必须在packing赋值为pom,child工程从parent继承的包括:dependencies,developers,contributors,plugin lists,reports lists,plugin execution with matching ids,plugin configuration

parent的使用方法如下:

  1. <parent>
  2. <groupId>org.codehaus.mojo</groupId>
  3. <artifactId>my-parent</artifactId>
  4. <version>2.0</version>
  5. <relativePath>../my-parent</relativePath>
  6. </parent>

relativePath是可选的,maven会首先搜索这个地址,在搜索本地远程repositories之前.

dependencyManagement使用方法

dependencyManagement:是用于帮助管理chidren的dependencies的。例如如果parent使用dependencyManagement定义了一个dependencyon junit:junit4.0,那么 它的children就可以只引用 groupId和artifactId,而version就可以通过parent来设置,这样的好处就是可以集中管理 依赖的详情

modules:对于多模块的project,outer-module没有必需考虑inner-module的dependencies,当列出modules的时候,modules的顺序是不重要的,因为maven会自动根据依赖关系来拓扑排序,

modules例子如下 :

my-project other-project

​​​​​​​properties信息

properties:是为pom定义一些常量,在pom中的其它地方可以直接引用。

定义方式如下:

  1. <properties>
  2. <file.encoding>UTF-8</file_encoding>
  3. <java.source.version>1.5</java_source_version>
  4. <java.target.version>1.5</java_target_version>
  5. </properties>

使用方式 如下 :

${file.encoding}

build属性设置

常用属性设置

defaultGoal:默认的目标,必须跟命令行上的参数相同,如:jar:jar,或者与时期parse相同,例如install

directory:指定build target目标的目录,默认为$(basedir}/target,即项目根目录下的target

finalName:指定去掉后缀的工程名字,例如:默认为${artifactId}-${version}

filters:用于定义指定filter属性的位置,例如filter元素赋值filters/filter1.properties,那么这个文件里面就可以定义name=value对,这个name=value对的值就可以在工程pom中通过${name}引用,默认的filter目录是${basedir}/src/main/fiters/

resources:描述工程中资源的位置 ​​​​​​​

  1. <resource>
  2. <targetPath>META-INF/plexus</targetPath>
  3. <filtering>false</filtering>
  4. <directory>${basedir}/src/main/plexus</directory>
  5. <includes>
  6. <include>configuration.xml</include>
  7. </includes>
  8. <excludes>
  9. <exclude>**/*.properties</exclude>
  10. </excludes>
  11. </resource>

targetPath:指定build资源到哪个目录,默认是base directory

filtering:指定是否将filter文件(即上面说的filters里定义的*.property文件)的变量值在这个resource文件有效,例如上面就指定那些变量值在configuration文件无效。

directory:指定属性文件的目录,build的过程需要找到它,并且将其放到targetPath下,默认的directory是${basedir}/src/main/resources

includes:指定包含文件的patterns,符合样式并且在directory目录下的文件将会包含进project的资源文件。

excludes:指定不包含在内的patterns,如果inclues与excludes有冲突,那么excludes胜利,那些符合冲突的样式的文件是不会包含进来的。

testResources:这个模块包含测试资源元素,其内容定义与resources类似,不同的一点是默认的测试资源路径是${basedir}/src/test/resources,测试资源是不部署的。​​​​​​​
plugins配置

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>2.0</version>
  5. <extensions>false</extensions>
  6. <inherited>true</inherited>
  7. <configuration>
  8. <classifier>test</classifier>
  9. </configuration>
  10. <dependencies>...</dependencies>
  11. <executions>...</executions>
  12. </plugin>

extensions:true or false, 决定是否要load这个plugin的extensions,默认为true.

inherited:是否让子pom继承,ture or false 默认为true.

configuration:通常用于私有不开源的plugin,不能够详细了解plugin的内部工作原理,但使plugin满足的properties

dependencies:与pom基础的dependencies的结构和功能都相同,只是plugin的dependencies用于plugin,而pom的denpendencies用于项目本身。在plugin的dependencies主要用于改变plugin原来的dependencies,例如排除一些用不到的dependency或者修改dependency的版本等,详细请看pom的denpendencies.

executions:plugin也有很多个目标,每个目标具有不同的配置,executions就是设定plugin的目标,

  1. <execution>
  2. <id>echodir</id>
  3. <goals>
  4. <goal>run</goal>
  5. </goals>
  6. <phase>verify</phase>
  7. <inherited>false</inherited>
  8. <configuration>
  9. <tasks>
  10. <echo>Build Dir: ${project.build.directory}</echo>
  11. </tasks>
  12. </configuration>
  13. </execution>

id:标识符

goals:里面列出一系列的goals元素,例如上面的run goal

phase:声明goals执行的时期,例如:verify

inherited:是否传递execution到子pom里。

configuration:设置execution下列表的goals的设置,而不是plugin所有的goals的设置.

仓库设置

Repositories:pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于,pom里的仓库是个性化的。

  1. <repositories>
  2. <repository>
  3. <id>public</id>
  4. <name>public</name>
  5. <url>http://172.16.0.193:8081/nexus/content/groups/public/</url>
  6. <releases>
  7. <enabled>true</enabled>
  8. </releases>
  9. <snapshots>
  10. <enabled>true</enabled>
  11. <updatePolicy>always</updatePolicy>
  12. </snapshots>
  13. </repository>
  14. </repositories>

​​​​​​​组件上传远程

  1. <!-- 配置部署的远程仓库 -->
  2. <distributionManagement>
  3. <snapshotRepository>
  4. <id>user-snapshots</id>
  5. <name>User Project SNAPSHOTS</name>
  6. <url>http://172.16.0.193:8081/nexus/content/repositories/snapshots</url>
  7. </snapshotRepository>
  8. <repository>
  9. <id>user-release</id>
  10. <name>User Project Release</name>
  11. <url>http://172.16.0.193:8081/nexus/content/repositories/releases</url>
  12. </repository>
  13. </distributionManagement>

对应setting.xml配置

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW9rZWppbjUyMQ_size_16_color_FFFFFF_t_70

下节更精彩!可以看我首页进群你学习!!

发表评论

表情:
评论列表 (有 0 条评论,71人围观)

还没有评论,来说两句吧...

相关阅读