`

《Maven 实战》读书笔记(七) 聚合

 
阅读更多
1.  继承

之前我们学习Maven的聚合机制遗留个问题,就是多个模块的pom.xml文件的内容出现了冗余、重复的内容,解决这个问题其实使用Maven的继承机制即可,就像Java的继承一样,父类就像一个模板,子类继承自父类,那么有些通用的方法、变量都不必在子类中再重复声明了,具体Java继承在内存中的表现形式可以参考

http://suhuanzheng7784877.iteye.com/blog/1000635

和http://suhuanzheng7784877.iteye.com/blog/1000700 中的部分内容。Maven的继承机制类似,在一个父级别的Maven的pom文件中定义了相关的常量、依赖、插件等等配置后,实际项目模块可以继承此父项目的pom文件,重复的项不必显示的再声明一遍了,相当于父Maven项目就是个模板,等着其他子模块去继承。不过父Maven项目要高度抽象,高度提取公共的部分(交集)。笔者使用了先前的聚合项目模块做的父模板pom,实际上很多机构也是这么实施的。

    <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.liuyan.account</groupId>  
        <artifactId>MavenAccount-aggregator</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
        <packaging>pom</packaging>  
      
        <properties>  
            <springversion>2.5.6</springversion>  
            <junitversion>2.5.6</junitversion>  
        </properties>  
      
        <dependencies>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId>  
                <version>${springversion}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-beans</artifactId>  
                <version>${springversion}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context</artifactId>  
                <version>${springversion}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context-support</artifactId>  
                <version>${springversion}</version>  
            </dependency>  
            <dependency>  
                <groupId>javax.mail</groupId>  
                <artifactId>mail</artifactId>  
                <version>1.4.1</version>  
            </dependency>  
            <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.7</version>  
                <scope>test</scope>  
            </dependency>  
            <dependency>  
                <groupId>com.icegreen</groupId>  
                <artifactId>greenmail</artifactId>  
                <version>1.3.1b</version>  
                <scope>test</scope>  
            </dependency>  
        </dependencies>  
      
        <build>  
            <resources>  
                <resource>  
                    <directory>src/main/resource</directory>  
                </resource>  
            </resources>  
            <plugins>  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-source-plugin</artifactId>  
                    <version>2.1.1</version>  
                    <executions>  
                        <execution>  
                            <id>buildSource</id>  
                            <goals>  
                                <goal>jar-no-fork</goal>  
                            </goals>  
                            <inherited>false</inherited>  
                            <configuration>  
                            </configuration>  
                        </execution>  
                    </executions>  
                </plugin>  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-compiler-plugin</artifactId>  
                    <configuration>  
                        <target>1.5</target>  
                    </configuration>  
                </plugin>  
            </plugins>  
        </build>  
      
        <modules>  
            <module>../MavenAccount-email</module>  
            <module>../MavenAccount-persist</module>  
        </modules>  
    </project>
 

这个pom文件即描述了通用的依赖模板,也列举出了聚合的模块,放心modules不会被继承。下面我们来改造一下之前的两个模块

邮件模块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>  
        <artifactId>MavenAccount-email</artifactId>  
        <packaging>jar</packaging>  
      
        <parent>  
            <groupId>com.liuyan.account</groupId>  
            <artifactId>MavenAccount-aggregator</artifactId>  
            <version>0.0.1-SNAPSHOT</version>  
            <relativePath>../MavenAccount-aggregator/pom.xml</relativePath>  
        </parent>  
      
    </project>  


注册模块

pom.xml
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics