Maven工具,以及maven项目的创建将不做科普。学者自学;
1.最外层的项目pom.xml(jar包应用不在赘述):项目名称:peit
一个<artifactId>peit</artifactId>项目中包含了一个<module>appapi</module>项目
<?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"</project>
注意:appapi项目pom.xml中包含了两个项目:
<module>garageApi</module>
<module>lgbirdapi</module>
2.第二层的POM.xml配置:项目名称:appapi
<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- parent标签说明子项目和父项目一样的属性-->
<parent>
<!--父项目的artifact标识符-->
<artifactId>peit</artifactId>
<!--父项目的group标识符-->
<groupId>com.peit</groupId>
<!--父项目的版本-->
<version>1.0-SNAPSHOT</version>
</parent>
<!--声明此POM项目描述符所遵从的版本-->
<modelVersion>4.0.0</modelVersion>
<!--一个全局唯一的项目标识符。通常使用完全限定的包名来和其它项目区隔(比如,org.apache.maven)-->
<groupId>com.peit</groupId>
<!--在给定gorup ID内唯一的产品标识符。Artifact就是由项目产生的,或者由项目所使用的东西。比如由Maven项目产生的artifacts包括:JARs,source以及二进制分发包和WARs。-->
<artifactId>appapi</artifactId>
<packaging>pom</packaging>
<!--作为本项目一部分的模块(子项目)-->
<modules>
<module>garageApi</module>
<module>lgbirdapi</module>
</modules>
</project>
3.第三层POM.xml配置:项目名称garageApi
<?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>appapi</artifactId>
<groupId>com.peit</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.peit</groupId>
<artifactId>garageApi</artifactId>
<!--打包格式-->
<packaging>war</packaging>
<!--引用properties文件-->
<properties>
<deploy.mode>new-product</deploy.mode>
</properties>
<!--打包的名称-->
<build>
<finalName>${project.artifactId}-${deploy.mode}</finalName>
<!--过滤文件:Maven提供的一种过滤机制,可以在资源文件被复制到目标目录的同时,替换其中的placeholders-->
<filters>
<filter>src/main/resources/config-${deploy.mode}.properties</filter>
</filters>
<!--应用工具-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--surefire plugin的作用 surefire 插件用来在maven构建生命周期的test phase执行一个应用的单元测试。它会产生两种不同形式的测试结果报告-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
</plugin>
<!--关联数据库-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<configurationFile>src/main/resources/generator-mysql.xml</configurationFile>
</configuration>
</plugin>
<!--引用打包工具-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<webResources>
<resouce>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.jsp</include>
<include>**/*.xml</include>
</includes>
</resouce>
</webResources>
</configuration>
</plugin>
<!--引用tomcat工具-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<!--定义项目,端口号-->
<configuration>
<port>8086</port>
<path>/garageapi</path>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 将自定义的properties在项目打包过程中拷贝到每个子项目中的classes -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>config-${deploy.mode}.properties</include>
<include>applicationContext.xml</include>
<include>applicationContext-dataSource.xml</include>
<include>applicationContext-resources.xml</include>
<include>applicationContext-timer.xml</include>
<include>applicationContext-service.xml</include>
<include>log4j.properties</include>
<include>memcached.properties</include>
<include>sqlmap/*</include>
<include>demo.vm</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
优点:
1.引用jar包方便,可以直接在父项目中应用,全体项目均可享用;同时子项目和独立
2.打war包方便,可以在第一层项目中打出所有的项目包;
3.最大的有点,其实是拓展简单方便;让一个大的项目,分为若干子项目。子项目再选中是否有必要进一步拆分。