Maven

6.4 解决项目测试乱码问题

  • 在当前项目的xml文件中增加插件配置

<build>

         <plugins>

                   <!-- 解决maven test命令时console出现中文乱码乱码 -->

                   <plugin>

                            <groupId>org.apache.maven.plugins</groupId>

                            <artifactId>maven-surefire-plugin</artifactId>

                            <version>2.7.2</version>

                            <configuration>

                                     <forkMode>once</forkMode><!--在一个进程中进行所有测试 ; 默认值:once -->

                                     <argLine>-Dfile.encoding=UTF-8</argLine>

                            </configuration>

                   </plugin>

         </plugins>

</build>

 

第7章 继承

7.1 为什么需要继承机制?

由于非compile范围的依赖信息是不能在“依赖链”中传递的,所以有需要的工程只能单独配置。例如:

Hello

<dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.0</version>

    <scope>test</scope>

</dependency>

HelloFriend

<dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.0</version>

    <scope>test</scope>

</dependency>

MakeFriend

<dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.0</version>

    <scope>test</scope>

</dependency>

此时如果项目需要将各个模块的junit版本统一为4.9,那么到各个工程中手动修改无疑是非常不可取的。使用继承机制就可以将这样的依赖信息统一提取到父工程模块中进行统一管理。

 

7.2 创建父工程

创建父工程和创建一般的Java工程操作一致,唯一需要注意的是:打包方式处要设置为pom。

 

7.3 在子工程中引用父工程

<parent>

         <!-- 父工程坐标 -->

<groupId>...</groupId>

         <artifactId>...</artifactId>

         <version>...</version>

         <relativePath>从当前目录到父项目的pom.xml文件的相对路径</relativePath>

</parent>

<parent>

    <groupId>com.atguigu.maven</groupId>

    <artifactId>Parent</artifactId>

    <version>0.0.1-SNAPSHOT</version>

 

    <!-- 指定从当前子工程的pom.xml文件出发,查找父工程的pom.xml的路径 -->

    <relativePath>../Parent/pom.xml</relativePath>

</parent>

 

 

此时如果子工程的groupId和version如果和父工程重复则可以删除。

 

7.4 在父工程中管理依赖

将Parent项目中的dependencies标签,用dependencyManagement标签括起来

<dependencyManagement>

    <dependencies>

       <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>4.9</version>

           <scope>test</scope>

       </dependency>

    </dependencies>

</dependencyManagement>

在子项目中重新指定需要的依赖,删除范围和版本号

<dependencies>

    <dependency>

       <groupId>junit</groupId>

       <artifactId>junit</artifactId>

    </dependency>

</dependencies>