在实际开发中一个庞大的项目往往是由多个工程组成的,为了便于管理,Maven提出工程父子结构。
如图所示一个大的工程名为Thesis_parent_LHQ,打开这个文件夹发现是由3个子工程thesis_background_lhq,thesis_book_lhq,thesis_preapply_lhq组成。当从SVN检出父工程时,三个子工程也会被检出。
1、父工程的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>thesis</groupId> <!-- 父工程的组ID-->
<artifactId>thesis-parent</artifactId> <!-- 父工程的ID-->
<version>1.1.1</version> <!-- 父工程的版本号,自己定义-->
<packaging>pom</packaging> <!-- 因为是引用这种父子结构的所以和以往大大不同,不是用war,也不是用jar而是用pom-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--定义使用UTF-8-->
</properties>
<modules><!-- 这个父工程下包含了下面3个子工程 -->
<module>thesis_background_lhq</module>
<module>thesis_book_lhq</module>
<module>thesis_preapply_lhq</module>
</modules>
<build><!-- 如果没有这段代码,程序也能运行,但是发布到Tomcat服务器上面的时候只有.class文件没有.java文件 -->
<finalName>thesis-parent</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/test/java</directory>
</resource>
</resources>
</build>
<!-- Junit测试和log4j写在父工程中,子工程继承父工程,默认加载这2个jar包 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
2、子工程thesis_background_lhq的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><!-- 父工程的组ID 和ID -->
<groupId>thesis</groupId>
<artifactId>thesis-parent</artifactId>
<version>1.1.1</version>
</parent>
<!-- 子工程不设置组ID,因为默认就在父工程的组ID -->
<artifactId>thesis_background_lhq</artifactId>
<version>1.1</version>
<packaging>war</packaging><!-- 子工程是一个可以独立运行的,格式是war -->
<name>thesis_background_lhq Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>thesis_background_lhq</finalName><!-- 工程名 -->
</build>
<dependencies>
<!-- struts包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4</version>
</dependency>
................
</dependencies>
</project>
3、thesis_book_lhq,thesis_preapply_lhq的pom.xml配置文件和thesis_background_lhq的pom.xml文件相似,以此类推。