maven多模块大型开发构建工程简单范例 weir 2015-06-05 16:34:17.0 java,maven 2444 开始: 总工程wpro: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.weir</groupId> <artifactId>wpro</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>wpro</name> <url>http://weir.com</url> <properties> <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> 注意红色部分。 然后做一个总的web工程: wproWeb: 这你是Maven Module 不要搞错了。 其他我就不多说了吧,一个模块一个模块就这样添加就行了。 这时候wpro的pom.xml里面会多出来这些配置: <modules> <module>wproWeb</module> <module>common</module> <module>loginWeb</module> <module>userManage</module> <module>userWeb</module> </modules> 你可以对一个模块里面的功能再做模块化分,比如web一个模块,业务操作另外一个模块。怎么划分就是你们的考虑了 上面仅仅是模块分好了,还要记着合并他们,那么合并在哪里呢,就是wproWeb里面,因为这个是总的web工程 先看下wproWeb的pom.xml里面都有什么: <?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.weir</groupId> <artifactId>wpro</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.weir</groupId> <artifactId>wproWeb</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>wproWeb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>wproWeb</finalName> </build> </project> 怎么合并其他模块呢??? 第一步:加入要合并的war内容 放在<build>里面 <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <overlays> <overlay> <groupId>com.weir</groupId> <artifactId>loginWeb</artifactId> </overlay> <overlay> <groupId>com.weir</groupId> <artifactId>userWeb</artifactId> </overlay> </overlays> </configuration> </plugin> </plugins> 注意这里加入的是 web工程 千万要记住,假如你的一个模块分解为web和业务,那么你加入到总的web工程里面的只有web模块那部分。 第二步:加入要合并的war的依赖 <dependency> <groupId>com.weir</groupId> <artifactId>loginWeb</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> </dependency> <dependency> <groupId>com.weir</groupId> <artifactId>userWeb</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> </dependency> 这样之后就可以maven install 了。 到现在你应该发现问题了,这就是maven的多模块开发:那个模块需要别的模块,就在该模块里面加入依赖的模块就行了(重复第二步就行了)。 这里需要注意的是最后合并的内容:如果多个war有同路径且同名的文件,如果总的web里面有,那么总的这个 会覆盖分支的;如果总的没有,那么看合并的顺序,留下第一个的文件。 要有每个模块都可以单独运行我们需要加入jetty: <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.14.v20131031</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <stopPort>9999</stopPort> <webAppConfig> <contextPath>/user</contextPath> </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>9080</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin>