Maven管理项目的时候,我们通常将一个业务作为一个单独的子业务进行开发处理,这样就需要使用到了聚合打包和聚合工程的创建问题了。在现在的物联网公司,一般是这个开发模式,对于传统的对日开发中,仍然是一个项目,下面一堆数据,我觉得还是maven这种项目管理起来更加的方便快捷。父类项目的作用,可以简单来讲,就是给各个子项目提供了相同的jar包配置,以及子项目包信息。
案例地址
https://gitee.com/yellowcong/bos/tree/master/maven_demo
工程分配
工程名 | 工程类型 | 打包类型 |
---|---|---|
yellowcong-parent | 父类包 | pom |
yellowcong-cms | web界面 | war |
yellowcong-user-service | 处理用户的模块 | jar |
yellowcong-passage-service | 处理文章的模块 | jar |
项目创建
创建父类工程
1、创建一个简单的maven项目
2、设定打包方式为pom,填写坐标
3、创建成功后的项目结构
创建web项目
1、创建maven项目,记住 workspace location的地址是parent项目的目录
2、选择webapp
3、填写项目的坐标
4、创建好的项目,index.jsp报错,是由于没有servlet-api的jar包,现在先不管他
创建子类项目
1、创建maven项目,同创建web项目一样的操作
2、选择quickstart这个
3、填写项目的坐标
4、创建后的项目
注释:同理创建好yellowcong-passage-service
这个项目
所有项目,都建立好后的项目如下图
配置pom
1、yellowcong-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>
<groupId>yellowcong.com</groupId>
<artifactId>yellowcong-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../yellowcong-passage-service/pom.xml</module>
<module>../yellowcong-user-service/pom.xml</module>
<module>../yellowcong-cms/pom.xml</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、yellowcong-cms 这个web项目配置
同样的子类包中,也需要著名父类包,子类包中有单独的打包方式
<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>
<artifactId>yellowcong-cms</artifactId>
<packaging>war</packaging>
<name>yellowcong-cms Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>yellowcong.com</groupId>
<artifactId>yellowcong-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../yellowcong-parent</relativePath>
</parent>
<dependencies>
<!--引用两个子项目-->
<dependency>
<groupId>yellowcong.com</groupId>
<artifactId>yellowcong-passage-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>yellowcong.com</groupId>
<artifactId>yellowcong-user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>yellowcong-cms</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3、两个jar项目配置
<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>
<artifactId>yellowcong-user-service</artifactId>
<packaging>jar</packaging>
<name>yellowcong-user-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>yellowcong.com</groupId>
<artifactId>yellowcong-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../yellowcong-parent</relativePath>
</parent>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
编译并打包
1、选中父类项目,我的就是yellowocng-parent这个项目
2、执行后,控制台会有输出
maven-install 相当于执行 mvn clean / mvn test /mvn package
3、执行成功后,每个子类的target包下都有相应的输出
4、我们可以查看war包,看见引用的子项目
常见问题
1、创建web项目后,报错
解决办法,修改项目的依赖包,这个是eclipse 的bug,如果你没有,就不需要管了