1.什么是pom?
pom(Project Object Model,项目对象模型)定义了项目的基本信息,用于描述项目是如何构建,声明项目依赖,插件配置,仓库配置等等。
2.pom配置
- <strong><project xmlns="http://maven.apache.org/POM/4.0.0"
- 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
- 4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- 5 <modelVersion>4.0.0</modelVersion>
- 6
- 7 <!-- 坐标 -->
- <parent> ... </parent>
- 8 <groupId>...</groupId>
- 9 <artifactId>...</artifactId>
- 10 <version>...</version>
- 11 <packaging>...</packaging>
- <!-- 仓库依赖 -->
- 12 <dependencies>...</dependencies>
- 14 <dependencyManagement>...</dependencyManagement>
- <!-- 项目模块配置 -->
- 15 <modules>...</modules>
- <!-- 全局配置文件 -->
- 16 <properties>...</properties>
- 17
- 18 <!-- 构建过程的设置 -->
- 19 <build>...</build>
- 20 <reporting>...</reporting>
- 21
- 22 <!-- 项目信息设置 -->
- 23 <name>...</name>
- 24 <description>...</description>
- 25 <url>...</url>
- 26 <inceptionYear>...</inceptionYear>
- 27 <licenses>...</licenses>
- 28 <organization>...</organization>
- 29 <developers>...</developers>
- 30 <contributors>...</contributors>
- 31
- 32 <!-- 环境设置 -->
- 33 <issueManagement>...</issueManagement>
- 34 <ciManagement>...</ciManagement>
- 35 <mailingLists>...</mailingLists>
- 36 <scm>...</scm>
- 37 <prerequisites>...</prerequisites>
- 38 <repositories>...</repositories>
- 39 <pluginRepositories>...</pluginRepositories>
- 40 <distributionManagement>...</distributionManagement>
- 41 <profiles>...</profiles>
- 42 </project></strong>
3.pom标签详解
3.1 项目坐标标签:
- <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>org.codehaus.mojo</groupId>
- <artifactId>my-project</artifactId>
- <version>1.0</version>
- <packaging>war</packaging>
- </project>
- groupId : 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
- artifactId : 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
- version : 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
- packaging : 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par
- modelVersion:定义pom版本号,版本号有一系列的规则
(依赖关系列表(dependency list)是POM的重要部分,也就是我们项目对jar包的管理)
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.0</version>
- <scope>test</scope>
- </dependency>
- …
- </dependencies>
- groupId , artifactId , version :引用的坐标
- scope : compile(default),provided,runtime,test,system 依赖的范围
- exclusions 需要排除的依赖的jar包
3.3 继承和聚合(子pom对父pom依赖 和 父项目对模块的依赖)
- <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>org.maven.my</groupId>
- <artifactId>${projectName}-parent</artifactId>
- <version>2.0</version>
- <!-- 定义项目有哪些子模块 -->
- <modules>
- <module>my-spring-web<module>
- <module>my-spring-service<module>
- <module>my-spring-common<module>
- <module>my-spring-dao<module>
- </modules>
- </project>
(可以帮我们指定 需要的maven插件,主要标签:Resources和Plugins
Resources:用于排除或包含某些资源文件
可以用于解决 我们部署测试和线上 服务时,资源文件配置的隔离依赖:-Ponline | -Plocal
- <build>
- <!-- 开启资源文件过滤 -->
- <resources>
- <resource>
- <directory>${project.basedir}/src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
- <!-- 指定资源文件路径 -->
- <profiles>
- <!--测试配置 -->
- <profile>
- <id>local</id>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- <build>
- <filters>
- <filter>${project.basedir}/src/main/swap/local.properties</filter>
- </filters>
- </build>
- </profile>
- <!-- 线上配置 -->
- <profile>
- <id>online</id>
- <activation>
- <activeByDefault>false</activeByDefault>
- </activation>
- <build>
- <filters>
- <filter>${project.basedir}/src/main/swap/online.properties</filter>
- </filters>
- </build>
- </profile>
Plugins:设置构建的插件
- <build>
- …
- <!-- 配置maven在运行时 需要依赖的插件,我们平常可以配jetty插件或者assemebly插件等-->
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <version>2.0</version>
- <extensions>false</extensions>
- <inherited>true</inherited>
- <configuration>
- <classifier>test</classifier>
- </configuration>
- <dependencies>…</dependencies>
- <executions>…</executions>
- </plugin>