maven学习笔记二

来源:互联网 发布:java 根据汉字生成签名 编辑:程序博客网 时间:2024/06/15 15:28

1、maven的依赖特性

<dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>3.8.1</version>    <scope>test</scope></dependency>

maven在依赖特性只要是由<scope>test</scope>来定义的,其取值范围在笔记一中有说明。

2、maven的聚合特性

    对于项目所有的模块,我们可以新建一个统一的管理模块来管理所有的模块,新建的方法和建立新项目一样,但是需要注意一下几点,都在图中标明:



这样我们就可以在新建的模块的pom.xml中统一管理所有的模块了。

 <!-- 导入要管理的模块,把这些模块聚合在一起 --> <modules>   <module>../user-core</module>   <module>../user-service</module>   <module>../user-action</module>  </modules>

3、maven的继承特性

    集成也是建立一个单独的只是用于管理的模块,建立方法和上面聚合是一样的,这样在管理模块的pom.xml中我们也就可以把公共的代码放在里面进行管理了,比如定义常量的代码放在管理模块中所有继承它的模块都可以拥有这些常量了。

    继承模块的代码为:

 <parent>   <groupId>com.cll.firstmaven</groupId>   <artifactId>user-mnager</artifactId>   <version>0.0.1-SNAPSHOT</version>   <relativePath>../user-mnager/pom.xml</relativePath>  </parent>

值得一说的是包的继承:

在父类中定义的包,不是知己定义在父类中的,而是通过包管理器定义在父类中的,因此在子类中,需要用的就声明,不需要的就不写,这样避免了父类中所有的包都会被子类继承。

【父类】

    <dependencyManagement>        <dependencies>            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>3.8.1</version>                <scope>test</scope>            </dependency>        </dependencies>                  </dependencyManagement>

【子类】

    <dependencyManagement>        <dependencies>            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>3.8.1</version>                <scope>test</scope>            </dependency>        </dependencies>                  </dependencyManagement>

0 0