maven scope 的 compile/provided/runtime/test

  • 2019-09-09
  • 浏览 (1304)

maven pome 的scope有compile,provided,runtime,test。

  1. complie是默认值,表示在build,test,runtime阶段的classpath下都有依赖关系。

  2. test表示只在test阶段有依赖关系,例如junit

  3. provided表示在build,test阶段都有依赖,在runtime时并不输出依赖关系而是由容器提供,例如web war包都不包括servlet-api.jar,而是由tomcat等容器来提供

    <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>servlet-api</artifactId>
     <version>3.0.1</version>
     <scope>provided</scope>
    </dependency>
    
  4. runtime表示在构建编译阶段不需要,只在test和runtime需要。这种主要是指代码里并没有直接引用而是根据配置在运行时动态加载并实例化的情况。虽然用runtime的地方改成compile也不会出大问题,但是runtime的好处是可以避免在程序里意外地直接引用到原本应该动态加载的包。例如JDBC连接池

    <dependency>
     <groupId>commons-dbcp</groupId>
     <artifactId>commons-dbcp</artifactId>
     <version>1.4</version>
     <scope>runtime</scope>
    </dependency>
    

    spring applicationContext.xml

    <!-- mysql -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <property name="dataSource">
         <ref bean="dataSource" />
     </property>
    </bean>
    <!-- Connection Pool -->
    <bean id="dataSource" destroy-method="close"
     class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName" value="${ckm.jdbc.driver}" />
     <property name="url" value="${ckm.jdbc.url}" />
     <property name="username" value="${ckm.jdbc.username}" />
     <property name="password" value="${ckm.jdbc.password}" />
     <property name="initialSize" value="6" />
    </bean>
    

5.import
想必大家在做SpringBoot应用的时候,都会有如下代码:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>

假如说,我不想继承,或者我想继承多个,怎么做?

我们知道Maven的继承和Java的继承一样,是无法实现多重继承的,如果10个、20个甚至更多模块继承自同一个模块,那么按照我们之前的做法,这个父模块的dependencyManagement会包含大量的依赖。如果你想把这些依赖分类以更清晰的管理,那就不可能了,import scope依赖能解决这个问题。你可以把dependencyManagement放到单独的专门用来管理依赖的pom中,然后在需要使用依赖的模块中通过import scope依赖,就可以引入dependencyManagement。例如可以写这样一个用于依赖管理的pom:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test.sample</groupId>
    <artifactId>base-parent1</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactid>junit</artifactId>
                <version>4.8.2</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactid>log4j</artifactId>
                <version>1.2.16</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

然后我就可以通过非继承的方式来引入这段依赖管理配置

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.test.sample</groupId>
            <artifactid>base-parent1</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependency>
    <groupId>junit</groupId>
    <artifactid>junit</artifactId>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactid>log4j</artifactId>
</dependency>

注意:import scope只能用在dependencyManagement里面

这样,父模块的pom就会非常干净,由专门的packaging为pom来管理依赖,也契合的面向对象设计中的单一职责原则。此外,我们还能够创建多个这样的依赖管理pom,以更细化的方式管理依赖。这种做法与面向对象设计中使用组合而非继承也有点相似的味道。

那么,如何用这个方法来解决SpringBoot的那个继承问题呢?

配置如下:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.3.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

这样配置的话,自己的项目里面就不需要继承SpringBoot的module了,而可以继承自己项目的module了。

0  赞