此前对maven的认识不多,最近得知maven对jar包的依赖以及源码的管理非常方便,所以也就开始使用maven了。
不过此时对maven的使用也仅限于maven对jar包的管理,其它功能使用得比不多。
近两天听说maven有个一子模块的功能,可以把一个项目分成不同的子项目来处理,并行开发,觉得这个功能非常实用,所以也想学习一下。
我一共新建了3个项目testP、testC1和testC2
---- testP
| -- pom.xml (packaging: pom)
| -- testC2
| -- pom.xml (packaging: jar)
| -- testC1
| -- pom.xml (packaging: war)
testP的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lee.test</groupId>
<artifactId>testP</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testP 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>testP</finalName>
</build>
<modules>
<module>testC2</module>
<module>testC1</module>
</modules>
<properties>
<!-- project character encoding setting -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- java source version -->
<maven.compiler.source>1.6</maven.compiler.source>
<!-- java class version -->
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>
<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.lee.test</groupId>
<artifactId>testP</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.lee.test</groupId>
<artifactId>testC1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>testC2 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>testC2</finalName>
</build>
<properties>
<!-- project character encoding setting -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- java source version -->
<maven.compiler.source>1.6</maven.compiler.source>
<!-- java class version -->
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>
testC2的pom.xml
<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.lee.test</groupId>
<artifactId>testP</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.lee.test</groupId>
<artifactId>testC2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testC2 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>testC2</finalName>
</build>
<properties>
<!-- project character encoding setting -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- java source version -->
<maven.compiler.source>1.6</maven.compiler.source>
<!-- java class version -->
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>
对于个人而言,有点代码洁癖,所以会觉得testP这个项目有点别扭。它目录下的pom.xml文件以及其packaging的值为pom也感到不解。
觉得应该可以直接在testC1目录下,即pom.xml的packaging值为war值的项目下引入其它子模块就够了。可是自己尝试修改过几次,pom.xml文件都会报错。
总而言之,maven子模块的框架算是依葫芦画瓢地弄出来了,但是对齐原理以及为何这样设计仍旧是知之甚少。还希望有懂的maven子项目的朋友多多发言,教教小弟,小弟不甚感激!