(5)Maven快速入门_5maven聚合与继承_scope依赖范围

 

多个maven项目实现统一管理, maven 插件jar继承自父的maven项目。对maven中jar的版本进行管理。

1、创建一个项目来管理多个maven项目

new ----maven Project 

勾选 Create  a simple  project(skip archetype selection)  

next  :填写项目信息, packaging  选 pom 。

创建完成后项目目录结构如下:

编辑 pom.xml配置文件

1、maven  聚合: 统一管理多个maven模块项目,统一执行编译 ,清理,安装

 需要配置:  <modules>  </modules> 来管理多个maven项目, (配置完成后,直接点击 此项目的pom.xml 文件右键运行,即可使管理的多个maven项目同时maven install、maven clean 简化了操作)

 <!-- (maven的聚合:) 统一管理多个maven模块项目,统一执行编译 ,清理,安装 -->
  <modules>
  <!-- 管理处在同一级目录的项目,先回到父目录再找需要管理的目录 -->
  <module>../dao</module>
  <module>../service</module>
  </modules>

 

2、dao 、service 、controller 项目中 引入的maven jar 统一配置到 extendsagg项目中, 再 dao 、servcie 、controller 中 配置 <parent></parent> 可简化自身项目的pom.xml配置,引入jar的版本也可以统一继承自父项目。

 <!--    maven继承自父项目 -->
  <parent>
  <groupId>com.inspur.smvc</groupId>
  <artifactId>extendsagg</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <relativePath>../extendsagg/pom.xml</relativePath>
  </parent>
  <!--   本项目信息 继承父项目 可简化-->
  <artifactId>controller</artifactId>
  <packaging>war</packaging>

 <!-- 添加Spring支持  继承父项目 可简化版-->
    <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
      </dependency>

3、maven 父项目  extendsagg  项目中pom.xml   中  jar 信息 的版本号 进行统一设置。

<!--  各个 maven插件  引入的jar的版本统一管理 -->
  <properties>
  <spring.version>4.1.7.RELEASE</spring.version>
  <log4j.version>1.2.17</log4j.version>
  <junit.version>4.12</junit.version>
  </properties>

 <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j.version}</version>
</dependency>

<!--   spring start-->
<dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>${spring.version}</version>
        </dependency>

 

controller maven模块的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>
  <!--    maven继承自父项目 -->
  <parent>
  <groupId>com.inspur.smvc</groupId>
  <artifactId>extendsagg</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <relativePath>../extendsagg/pom.xml</relativePath>
  </parent>
  <!--   本项目信息 -->
  <artifactId>controller</artifactId>
  <packaging>war</packaging>
  
  
<!--   <name>controller Maven Webapp</name> -->
<!--   <url>http://maven.apache.org</url> -->
  
  
  <dependencies>
  
<!--   引入service -->
  <dependency>
   <groupId>com.inspur.smvc</groupId>
   <artifactId>service</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl</artifactId>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <scope>provided</scope>
</dependency>
  
  
  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
  
  <!-- 添加Spring支持 -->
    <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
        </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
      </dependency>
      
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>controller</finalName>
  </build>
</project>

dao maven模块的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

<!--    maven继承自父项目 -->
  <parent>
  <groupId>com.inspur.smvc</groupId>
  <artifactId>extendsagg</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <relativePath>../extendsagg/pom.xml</relativePath>
  </parent>
<!-- 本项目信息 -->
  <artifactId>dao</artifactId>
  <packaging>jar</packaging>
    <!--  注释
  <name>dao</name>
  <url>http://maven.apache.org</url>
     -->
     
<!--      <build>      -->
<!--         <resources> -->
<!--             编译之后包含xml -->
<!--             <resource> -->
<!--                 <directory>src/main/java</directory> -->
<!--                 <includes> -->
<!--                     <include>**/*.xml</include> -->
<!--                 </includes> -->
<!--                 <filtering>true</filtering> -->
<!--             </resource> -->
<!--             编译之后包含xml -->
<!--             <resource> -->
<!--                 <directory>src/main/resources</directory> -->
<!--                 <includes> -->
<!--                     <include>*.xml</include> -->
<!--                 </includes> -->
<!--                 <filtering>true</filtering> -->
<!--             </resource> -->
<!--         </resources> -->
<!--     </build> -->
     
     

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  
  <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
</dependency>
  
  
<!-- https://mvnrepository.com/artifact/oracle/ojdbc -->
<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc6</artifactId>
  <version>11.2.0</version>
 
</dependency>

<!--         <dependency>   -->
<!--             <groupId>commons-dbcp</groupId>   -->
<!--             <artifactId>commons-dbcp</artifactId>   -->
<!--             <version>1.2.2</version>   -->
<!--         </dependency>   -->

<!-- mybatis -->
      <dependency>
        <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
<!--  junit4.12 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

service maven 模块的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <!--    maven继承自父项目 -->
  <parent>
  <groupId>com.inspur.smvc</groupId>
  <artifactId>extendsagg</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <relativePath>../extendsagg/pom.xml</relativePath>
  </parent>
  
  <!--   本项目的maven插件信息 -->
  <artifactId>service</artifactId>
  <packaging>jar</packaging>
  
  
  

<!--   <name>service</name> -->
<!--   <url>http://maven.apache.org</url> -->

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  
  <!-- 依赖 dao -->
  <dependency>
  <groupId>com.inspur.smvc</groupId>
  <artifactId>dao</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  </dependency>
  
  
  <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
</dependency>
  
  
<!--   spring start-->
    <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
        </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
      </dependency>
      
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
      
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
    </dependency>
    
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
    </dependency>

    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

<!-- spring end   -->
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

extendsagg maven 父项目统一管理 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>com.inspur.smvc</groupId>
  <artifactId>extendsagg</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  <!-- (maven的聚合:) 统一管理多个maven模块项目,统一执行编译 ,清理,安装 -->
  <modules>
  <!-- 管理处在同一级目录的项目,先回到父目录再找需要管理的目录 -->
  <module>../dao</module>
  <module>../service</module>
  </modules>
  
  <!--  各个 maven插件  引入的jar的版本统一管理 -->
  <properties>
  <spring.version>4.1.7.RELEASE</spring.version>
  <log4j.version>1.2.17</log4j.version>
  <junit.version>4.12</junit.version>
  </properties>
  
  
  
 <dependencyManagement>
 <dependencies>
<!--     dao start -->
     <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j.version}</version>
</dependency>
  
  
<!-- https://mvnrepository.com/artifact/oracle/ojdbc -->
<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc6</artifactId>
  <version>11.2.0</version>
 
</dependency>

<!--         <dependency>   -->
<!--             <groupId>commons-dbcp</groupId>   -->
<!--             <artifactId>commons-dbcp</artifactId>   -->
<!--             <version>1.2.2</version>   -->
<!--         </dependency>   -->

<!-- mybatis -->
      <dependency>
        <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
<!--  junit4.12 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
<!--     dao end -->

<!-- service start -->
 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.2</version>
</dependency>
  
  
<!--   spring start-->
    <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>${spring.version}</version>
        </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${spring.version}</version>
      </dependency>
      
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
      
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>

    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>

<!-- service end -->

<!-- controller start -->
 <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>
  
  
  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>
  
<!-- controller end -->

    
 </dependencies>
 </dependencyManagement>
  
  
  
  
</project>

 pom.xml中

scope 选项 :

Compile(默认) 编译 依赖范围 :编译、测试、运行都有效;

Test 测试 依赖范围 :仅测试有效;例如 JUnit;

Provided 已提供 依赖范围 : 编译,测试有效,运行时无效。例如 servlet-api。

System 系统 依赖范围 : 使用system范围的依赖必须通过systemPath 指定依赖文件的路径。
Import 导入 依赖范围 : 使用dependencyManagement时候,可以导入依赖配置。

posted @ 2018-07-21 17:08  prefectjava  阅读(267)  评论(0编辑  收藏  举报