大家在学习pom.xml文件之前先了解一些概念:
1、POM是Project Object Module 项目对象模型,通过xml文件来描述我们的项目
2、GVA(groupId,version,artifactId)我们的项目通过这个元素来标识它的唯一性
了解完这两个概念之后,我将会通过一个项目来个大家具体讲述pom.xml文件里面的一些配置
1、首先我建立一个maven工程名为:spring_maven,其pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>4.0.0</modelVersion>
<!-- 通过GVA定义唯一的项目 -->
<groupId>com.wq</groupId>
<artifactId>spring_maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>spring-maven</name>
<description>spring maven project</description>
<!-- 该项目中不同的模块,不同模块之间本身无任何关系 -->
<modules>
<module>spring_mave_dao</module>
<module>spring_maven_service</module>
<module>spring_maven_web</module>
</modules>
<properties>
<spring.version>4.0.5.RELEASE</spring.version>
<slf4j.version>1.7.6</slf4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 定义一些公共的依赖,子pom会继承全部,且会下载这些依赖,如果子类需要不同的版本的依赖,需要在子类中覆盖对应的依赖 -->
<dependencies>
<!--junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<!-- log4j 三者的关系:slf4j->slf4j-log4j12->log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>
<!-- spring相关的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<!-- 一些基础包的依赖 -->
<!-- apache 包含一些格式的编码方式如:md5,base64,hex等 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
<scope>compile</scope>
</dependency>
<!-- commons-lang3包中包含了一些处理基本信息的类,如对字符串,时间等的处理 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<scope>compile</scope>
</dependency>
<!-- commons-collections是对java collections的扩展 -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<!-- 父pom中定义依赖管理,子pom会继承,但是不会去下载这些依赖,如果子pom中需要依赖,可以通过定义依赖的groupid和artifactid定义,这样的优势在于
项目中的所有的子pom的对应依赖的版本一致 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、在该项目中分别建立其他的三个模块为spring_maven_dao,spring_maven_service,spring_maven_web,它们的pom.xml分别为:
spring_maven_dao:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- 继承父pom -->
<parent>
<groupId>com.wq</groupId>
<artifactId>spring_maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- 继承父pom中的groupId和vesion -->
<artifactId>spring_mave_dao</artifactId>
<packaging>jar</packaging>
<name>spring_mave_dao Maven Mojo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
<!-- 使用父pom中管理的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<scope>compile</scope>
</dependency>
<!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId>
<scope>compile</scope> </dependency> -->
<!-- mysql连接 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
<scope>compile</scope>
</dependency>
<!-- c3p0数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5-pre7</version>
<scope>compile</scope>
<!-- 去除一些你不需要的依赖 -->
<!-- <exclusions> <exclusion> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId>
</exclusion> </exclusions> -->
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>compile</scope>
</dependency>
<!-- <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version>
<scope>compile</scope> </dependency> -->
</dependencies>
</project>
spring_maven_service:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wq</groupId>
<artifactId>spring_maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring_maven_service</artifactId>
<packaging>jar</packaging>
<name>spring_maven_service Maven Mojo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<!-- 依赖spring_maven_dao -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring_mave_dao</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
spring_maven_web:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wq</groupId>
<artifactId>spring_maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring_maven_web</artifactId>
<packaging>war</packaging>
<name>spring_maven_web</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 依赖spring_maven_service -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring_maven_service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<!-- 设置jdk的版本 -->
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF8</encoding>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- resin plugin -->
<plugin>
<groupId>com.caucho</groupId>
<artifactId>resin-maven-plugin</artifactId>
<version>4.0-SNAPSHOT</version>
<configuration>
<server>127.0.0.1</server>
<port>8080</port>
<user>admin</user>
<password>admin</password>
</configuration>
</plugin>
</plugins>
</build>
</project>
各个项目之间的依赖关系是spring_maven->spring_maven_dao->spring_maven_service->spring_maven_web,下面我们来详细的分析一下这些pom.xml文件的内容:
1、spring_maven相当于父pom,我们在定义它的packaging方式定义为pom,这样我们的子pom才能继承它的pom
2、我们把子模块中需要用到的jar包可以在spring_maven中中dependencies添加这些依赖
3、把子模块中会出现但是不是所有的模块中出现的依赖定义在dependencyManagement中
4、可以通过设置properties设置我们在pom中需要用到的一些变量,比如依赖的版本号
5、在子模块中会继承所有父pom的元素。
6、我们可以在pom文件中定义一些插件
7、注意一些模块化的定义
分享到:
相关推荐
主要介绍了Maven pom.xml与settings.xml详解的相关资料,这里对pom.xml与setting.xml详细的研究说明,需要的朋友可以参考下
记录一下自己常用的maven工程的pom.xml配置详解,各个标签详解
一、Idea关联的maven本地仓库配置...三、3个可单独使用的,maven项目文件pom.xml自定义配置 pom-maven-springboot-CusConfigV1.xml pom-maven-springboot-CusConfigV2.xml pom-maven-spring-CusConfigV1不推荐使用.xml
maven 中 pom.xml 文件详解
maven pom.xml详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=...
maven pom.xml详解
Maven之pom.xml配置文件详解.pdf
本文主要阐述了maven build是用来干什么的,以及对build标签中各子元素配置的作用
pom.xml android maven 工程 如何拿Maven 构建 一个 Android 项目的pom配置
该jar包功能,可以在一个properties文件里面定义jdbc.url=${url},在另一个properties文件定义具体的值,通过该jar可以获取到哪个具体的值。下载之后,自行安装到本地...具体pom.xml配置使用可以参考网络其他博文,谢谢
maven项目pom.xml中parent标签的使用
maven的pom.xml的最详细配置,内含pom的依赖、jdk配置等
-打包文件前置.xml- -全局属性配置- -NJCC开发环境(MySQL)- -部署环境(对应配置文件版本)- -设置默认环境- -NJCC开发环境(Oracle)- -部署环境(对应配置文件版本)- -配置maven地址- -外网- -版本增加- -表示test...
SSH框架Maven项目pom.xml
构建文件pom.xml详解,可具体了解Maven构建、编译过程的原理
搭建springboot项目的 pom.xml配置,只需要创建maven项目把pom替换即可。 指定启动类,搭配上springboot相应的注解
Maven_pom.xml常用配置解析
selenium2.53+maven环境pom.xmlselenium2.53+maven环境pom.xmlselenium2.53+maven环境pom.xmlselenium2.53+maven环境pom.xml