(一)简单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>4.0.0</modelVersion>
<groupId>com.alib</groupId>
<artifactId>ntytest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ntytest</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.jpa.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.6</jdk.version>
<junit.version>4.12</junit.version>
<servlet.version>3.0.1</servlet.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<!-- 设置了profile必须在build中定义resources节点,表明那个目录下的资源需要被profile中的值代替 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>spring-config.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>spring-config.xml</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<properties.dir>/app/test/configs</properties.dir>
</properties>
</profile>
<profile>
<id>st</id>
<properties>
<properties.dir>/app/test2/configs</properties.dir>
</properties>
</profile>
</profiles>
上述代码是maven pom.xml文件的基本配置:
maven坐标:一个项目的确定是由groupId、artifactId和version来决定。
《packaging》表示打包的类型为war。
《dependency》引入项目依赖的jar包
《exclusion》引入的jar包又引入了其他的jar,可选择行排除
《properties》该配置可以用${}形式引入参数
《plugin Managment》管理maven的插件,这个与《pligins》之间的区别在于模块化管理,使用这个标签可以让子模块选择父项目的插件
《profiles》配置不同环境的参数
(二)模块化管理
对于一个较为复杂的项目,一般都会分开不同的模块让不同的开发人员进行独立开发,maven在这方面就做的很好。
(1)首先建立一个parent项目,只保留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>
<groupId>com.aliby</groupId>
<artifactId>nty</artifactId>
<version>2.0.0-SNAPSHOT</version> <!-- 用于工程聚合,一般情况下不要修改 -->
<packaging>pom</packaging>
<name>nty</name>
<url>http://maven.apache.org</url>
<modules>
<module>nty-manager-api</module>
<module>nty-dao</module>
<module>nty-common</module>
<module>nty-core-service</module>
<module>nty-controller</module>
<module>nty-web</module>
</modules>
<properties>
<pub-nty.version>2.0.0-SNAPSHOT</pub-nty.version> <!-- 版本,统一控制 -->
<jdk.version>1.7</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties
</project>
(2)构建子模块依赖父项目《parent》,各子模块间可相互依赖引用
<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>
<parent>
<groupId>com.mucfc</groupId>
<artifactId>nty</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<groupId>com.mucfc.pub-nty</groupId>
<artifactId>nty-manager-api</artifactId>
<version>${nty.version}</version>
<name>pub-nty-manager-api</name>
<dependencies>
<dependency>
<groupId>com.mucfc.nty</groupId>
<artifactId>nty-dao</artifactId>
<version>${pub-nty.version}</version>
</dependency>
<dependency>
<groupId>com.mucfc.nty</groupId>
<artifactId>nty-api</artifactId>
<version>${nty.version}</version>
</dependency>
</dependencies>
</project>