手头上有个已上线的系统,但因老板的特殊要求,系统需要不断的修改。还有就是公司市场部不定期地在举行一些微信活动,每一个活动都是周期性的,活动完了这个功能就要在系统中移除。
系统中就有三种模块:已经在系统中正常运行不需要再变更的模块、经常性变更的模块、用完就要移除的活动模块。
简单说明一下:
eclipse版本:
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
由于涉及到maven项目在eclipse上进行tomcat的集成调试,如果调试异常的朋友可以考虑更换版本比较高的eclipse版本。
maven版本: 2.2
maven相关的内容:http://blog.csdn.net/p_3er/article/category/2505419
maven多模块项目需要一个主项目来聚合各个子模块,不过其实就是需要一个父pom.xml。
这个pom.xml主要有两个内容:
<packaging>pom</packaging>
<modules>
<module>timetable-common</module>
<module>wechat-api</module>
<module>timetable-system</module>
<module>timetable-activity</module>
<module>timetable-main</module>
</modules>
完整的timetable/pom.xml为:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<packaging>pom</packaging>
<version>2.0.0</version>
<name>timetable</name>
<url>http://maven.apache.org</url>
<properties>
<project.version>2.0.0</project.version>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>timetable-common</module>
<module>wechat-api</module>
<module>timetable-activity</module>
<module>timetable-main</module>
<module>timetable-system</module>
</modules>
</project>
通过eclipse,新建Maven Module.
新建一个模块名为timetable-common的simple project。
timetable-common/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.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-common</artifactId>
<name>timetable-common</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 设置变量:用于统一管理Library的版本 -->
<javax.version>7.0</javax.version>
<javax.servlet.version>3.1.0</javax.servlet.version>
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.16</log4j.version>
<org.aspectj.version>1.7.3</org.aspectj.version>
<net.sf.ehcache.version>2.3.2</net.sf.ehcache.version>
<org.slf4j.version>1.6.1</org.slf4j.version>
<mysql.jdbc.version>5.1.29</mysql.jdbc.version>
<org.hibernate.entitymanager.version>3.6.3.Final</org.hibernate.entitymanager.version>
<org.springframework.version>4.1.1.RELEASE</org.springframework.version>
<org.codehaus.jackson.version>1.7.4</org.codehaus.jackson.version>
<net.sf.json-lib.version>2.4</net.sf.json-lib.version>
<proxool.version>0.8.3</proxool.version>
<javamail.version>1.4.1</javamail.version>
<commons-codec.version>1.9</commons-codec.version>
<commons-io.version>1.3.2</commons-io.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<xstream.version>1.4.3</xstream.version>
<org.apache.jcs.version>1.3</org.apache.jcs.version>
<joda-time.version>2.3</joda-time.version>
<org.apache.poi.version>3.8</org.apache.poi.version>
<com.fasterxml.jackson.core.version>2.3.3</com.fasterxml.jackson.core.version>
<commons-httpclient.version>3.1</commons-httpclient.version>
<jaxen.version>1.1.6</jaxen.version>
<com.drewnoakes.version>2.8.1</com.drewnoakes.version>
<com.google.zxing.version>3.0.0</com.google.zxing.version>
<io.netty.version>5.0.0.Alpha2</io.netty.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>${javax.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${org.hibernate.entitymanager.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${net.sf.ehcache.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${javax.servlet.jstl.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj.version}</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.jdbc.version}</version>
</dependency>
<!-- proxool -->
<dependency>
<groupId>proxool</groupId>
<artifactId>proxool</artifactId>
<version>${proxool.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>${org.codehaus.jackson.version}</version>
</dependency>
<!-- spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<!-- joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${net.sf.json-lib.version}</version>
<classifier>jdk15</classifier>
</dependency>
<!-- mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javamail.version}</version>
</dependency>
<!-- Jcscache -->
<dependency>
<groupId>org.apache.jcs</groupId>
<artifactId>jcs</artifactId>
<version>${org.apache.jcs.version}</version>
</dependency>
<!-- commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!-- 微信接口使用到 -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>${xstream.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>
<!-- commons-httpclient 支付宝接口使用 -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${commons-httpclient.version}</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>${jaxen.version}</version>
</dependency>
<!-- 获取图片信息 -->
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>${com.drewnoakes.version}</version>
</dependency>
<!-- google二维码工具 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${com.google.zxing.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${com.google.zxing.version}</version>
</dependency>
<!-- netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${io.netty.version}</version>
</dependency>
</dependencies>
</project>
配置比较简单:
<parent>
<groupId>com.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-common</artifactId>
<name>timetable-common</name>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
使用a中相同的方法新建wechat-api模块。
wechat-api/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.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>wechat-api</artifactId>
<name>wechat-api</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
timetable-system模块的新建与模块与wechat-api模块的方式与配置雷同。只是timetable-system模块依赖的是wechat-api模块。当然,通过依赖wechat-api,同时也依赖了timetable-common。
与使用eclipse创建一个独立的maven web project一样,创建一个maven web module。
注意:我们不是创建简单的模块
通过Filter过滤后,选择maven-archetype-webapp。
创建完成后的模块类似一个的java web项目:
在此模块下,我们和正常的java web开发一下,在src/main/java下写java代码,src/main/resources下放配置文件,src/main/webapp下写web相关的代码及资源。
timetable-activity/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.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-activity</artifactId>
<packaging>war</packaging>
<name>timetable-activity Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-system</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>timetable-activity</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-system</artifactId>
<version>${project.version}</version>
</dependency>
<packaging>war</packaging>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
像3.4那样创建一个timetable-main的maven web module。
timetable-main/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.benben</groupId>
<artifactId>timetable</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>timetable-main</artifactId>
<packaging>war</packaging>
<name>timetable-main</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-system</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 合并多个war -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
<overlays>
<overlay>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
</project>
<dependency>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
<overlays>
<overlay>
<groupId>com.benben</groupId>
<artifactId>timetable-activity</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
多web项目在eclipse+tomcat环境下部署调试和单个maven web项目的方式是一样的:
再去tomcat目录,发现已经将合并后的web发布到对应目录下了。 依赖的打包方式为jar的maven module已经被打包成jar发布到lib下了,而依赖的打包方式为war则和之前一样,编译文件会发布到classes下。不过你可以打个断点试试,依然可以调试,修改一段代码,发现热部署也是可以的,这样就和原来的开发习惯完全一样了。
注意: