1、在使用maven进行多模块、多工程开发时,在父project的pom.xml文件中,需要将<packaging>
标签修改为<packaging>pom</packaging>
。
主web模块打成war包,其他web子模块也需要打成war包,其他如entity、service、dao等子模块要打成jar包。
2、主web模块的pom.xml文件,需要在<build>
标签中增加,以下包含2个子模块的依赖,分别是<artifactId>customerweb</artifactId>
和<artifactId>goodsweb</artifactId>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<overlays>
<overlay>
<groupId>com.myproject</groupId>
<artifactId>customerweb</artifactId>
</overlay>
<overlay>
<groupId>com.myproject</groupId>
<artifactId>goodsweb</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopPort>9999</stopPort>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
增加jetty-maven-plugin这个plugin是为了该web模块可以使用jetty运行测试。
依赖关系也需要增加,在标签<dependencies>
下增加依赖的模块,注意依赖的web模块,一定要设置type标签为<type>war</type>
,例如:
<dependencies>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>customerweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>goodsweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
3、如果某个模块依赖其他子模块,需要在这个模块的pom.xml文件中增加依赖关系,在<dependencies>
标签里增加,例如:
<dependency>
<groupId>com.myproject</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
4、如果某个web子模块要单独运行测试,pom.xml也需要增加plugin,在标签<build>
下增加,这样可以使用jetty运行,进行测试:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopPort>9999</stopPort>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
5、项目中各个子模块依赖的公共的jar包等,可以配置在父工程的pom.xml中,这样其他子模块就可以不用配置了。
6、在eclipse中运行某个子模块,可以如下配置: