一个经典的Maven工程结构大致可以划分为:
1、聚合模块
2、用于配置公共属性、依赖管理和插件管理的超级模块
3、其他功能模块
工程结构:
聚合模块 | -------- 超级模块 | | | pom.xml | -------- 功能模块 | | | pom.xml | pom.xml
聚合模块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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.michael</groupId> <artifactId>account-aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>account-aggregator</name> <modules> <module>account-parent</module> <module>account-email</module> <module>account-persist</module> <module>account-view</module> </modules> </project>
超级模块pom:
<?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> <groupId>com.michael</groupId> <artifactId>account-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>account-parent</name> <properties> <account-global-version>0.0.2-SNAPSHOT</account-global-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
功能模块A pom:
<?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.michael</groupId> <artifactId>account-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 元素relativePath表示父模块POM的相对路径。 当项目构建时,Maven会首先根据该元素制定的相对路径检查父POM, 如果找不到,再从本地库查找。 该元素的默认值使../pom.xml --> <relativePath>../account-parent/pom.xml</relativePath> </parent> <groupId>com.michael</groupId> <artifactId>account-email</artifactId> <version>${account-global-version}</version> <name>account-email</name> <packaging>jar</packaging> </project>
功能模块B pom:
<?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.michael</groupId> <artifactId>account-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../account-parent/pom.xml</relativePath> </parent> <groupId>com.michael</groupId> <artifactId>account-view</artifactId> <version>${account-global-version}</version> <packaging>war</packaging> <build> <finalName>account-view</finalName> </build> </project>