目录
前言
Maven父子项目结构:一个父项目中包含多个 子 模块项目。搭建的重点在于pom.xml文件的配置。好处在于依赖的传递、方便项目的打包。在父项目下即可完成所有子项目的打包。
一、创建父项目pro
父项目基本就是一个空的项目文件夹,包含有pom文件。
idea中创建一个空的Maven项目pro,如下步骤:
新建的父项目中删除src目录:
二、创建子项目
1.创建子项目sbservice和sbwebsite
如下步骤:
2.修改父项目及两个子项目的pom文件
pro父项目的pom文件:
<?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.wqbr</groupId>
<artifactId>pro</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>pro</name>
<!--添加两个子项目模块-->
<modules>
<module>sbservice</module>
<module>sbwebsite</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
<configuration>
<attach>true</attach>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
子项目sbservice的pom文件:
<?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">
<!--添加父类项目-->
<parent>
<artifactId>pro</artifactId>
<groupId>com.wqbr</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sbservice</artifactId>
<packaging>war</packaging>
<name>sbservice</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--添加XMl文件打包配置-->
<build>
<resources>
<resource>
<!--对应的项目目录-->
<directory>src/main/java</directory>
<includes>
<!--包含的文件-->
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
子项目sbwebsite的pom文件:
<?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">
<!--添加父类项目-->
<parent>
<artifactId>pro</artifactId>
<groupId>com.wqbr</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sbwebsite</artifactId>
<packaging>war</packaging>
<name>sbwebsite</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
3.创建子项目基础目录
标记新创建的目录:
到处Maven父子项目构建完成可以在Maven中编译、打包试试: