Maven的聚合、继承(NEW)
<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.gc.user</groupId> <artifactId>user-aggregation</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 此处,类型为pom --> <packaging>pom</packaging> <!-- 将其它模块放到一起,集中执行命令 --> <modules> <module>../user-core</module> <module>../user-dao</module> </modules> </project>
?
?
====================================================================
?
?
模块的继承
?
引入继承带来的好处:
将各个模块中相同的配置定义到一个文件中,由其它模块来继承
提高pom文件配置的重用性,而且可以单点维护---单一职责原则!
?
用法:
新建一个Maven项目,将其artifactId设置为user-parent
修改package属性为pom
?
parent模块
<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.gc.user</groupId><artifactId>user-parent</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>user-parent</name><url>http://maven.apache.org</url><!-- 常量的定义,子模块中通过${}就能使用此处定义的常量:单点维护 --><properties><project.version>0.0.1-SNAPSHOT</project.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><my.jar.source>d:/</my.jar.source><junit.version>4.10</junit.version></properties><!-- 如果不通过 dependencyManagement管理依赖,虽然子模块能够继承这些依赖, 但是会继承所有parent中的依赖!应该根据各个子模块的需求,灵活继承依赖, 所以使用dependencyManagement来管理,这样子模块声明了那些依赖,就继承哪些依赖 子类没有声明的依赖,就不会继承下去。 --><dependencyManagement><dependencies><!-- 子模块的依赖 --><dependency><groupId>com.gc.user</groupId><artifactId>user-core</artifactId><version>${project.version}</version></dependency><!-- 子模块的依赖 --><dependency><groupId>com.gc.user</groupId><artifactId>user-dao</artifactId><version>${project.version}</version></dependency><!-- 其它依赖 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>4.2.6.Final</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.mortbay.jetty</groupId><artifactId>servlet-api</artifactId><version>2.5-6.0.0</version><scope>provided</scope></dependency></dependencies></dependencyManagement></project>
??
?
子模块user-core模块继承parent模块
<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><parent><groupId>com.gc.user</groupId><artifactId>user-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>user-core</artifactId><!-- groupId和version将从parent继承,无需定义 --><packaging>jar</packaging> <!-- properties将从parent继承,无需定义 --> <name>user-core</name><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId> <!-- version无需定义,会从parent继承下来 --></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId></dependency><dependency><groupId>org.mortbay.jetty</groupId><artifactId>servlet-api</artifactId><scope>provided</scope></dependency></dependencies></project>
?
?
子模块user-dao模块继承parent模块
??
<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> <parent> <groupId>com.gc.user</groupId> <artifactId>user-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>user-dao</artifactId> <packaging>jar</packaging> <name>user-dao</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- 引入user-core,将会发生传递性依赖:user-core中的依赖是否会导入到本模块中,还要根据scope决定! --> <dependency> <groupId>com.gc.user</groupId> <artifactId>user-core</artifactId> <exclusions> <!-- 排除user-core模块中关于log4j的依赖 --> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies></project>
?
?
====================================================================
?
??
将聚合与继承集中到parent模块的POM中?
?
user-parent模块,既提供聚合又提供继承
?
<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.gc.user</groupId><artifactId>user-parent</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><!-- 将其它模块放到一起,集中执行命令 --><modules><module>../user-core</module><module>../user-dao</module></modules><name>user-parent</name><url>http://maven.apache.org</url><!-- 常量的定义,子模块中通过${}就能使用此处定义的常量:单点维护 --><properties><project.version>0.0.1-SNAPSHOT</project.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><my.jar.source>d:/</my.jar.source><junit.version>4.10</junit.version></properties><!-- 如果不通过 dependencyManagement管理依赖,虽然子模块能够继承这些依赖, 但是会继承所有parent中的依赖!应该根据各个子模块的需求,灵活继承依赖, 所以使用dependencyManagement来管理,这样子模块声明了那些依赖,就继承哪些依赖 子类没有声明的依赖,就不会继承下去。 --><dependencyManagement><dependencies><!-- 子模块的依赖 --><dependency><groupId>com.gc.user</groupId><artifactId>user-core</artifactId><version>${project.version}</version></dependency><!-- 子模块的依赖 --><dependency><groupId>com.gc.user</groupId><artifactId>user-dao</artifactId><version>${project.version}</version></dependency><!-- 其它依赖 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>4.2.6.Final</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency><dependency><groupId>org.mortbay.jetty</groupId><artifactId>servlet-api</artifactId><version>2.5-6.0.0</version><scope>provided</scope></dependency></dependencies></dependencyManagement></project>
?