# Maven Dependency Scope 的使用說明 ### 一、pom.xml檔 使用 Dependency Scope: ```xml <!-- servlet api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet.version}</version> <scope>provided</scope> </dependency> ``` ### 二、目前 \<scope> 可以使用5個設定: #### 1.compile (編譯) > 此為預設值,表示此jar是所有階段需要的。 部署時將一起被打包到jar/war裡面。 #### 2.test (測試) > 表示此jar檔只有在測試時,才需要使用。如 junit jar 等。 部署時將不會打包到jar/war裡面。 #### 3.runtime (運行) > 表示此jar檔只有在運行時,才需要使用。如jdbc jar等。 #### 4.provided (已提供) > 當運行時,表示此jar檔期望由JDK、Tomcat及jboss等來提供。如 servlet.jar、jsp-api.jar 等。部署時將不會打包到jar/war裡面。 #### 5.system (系統) > 表示此jar檔有系統提供。跟provided 相似,這是以外部JAR檔的形式提供。而在maven repository 是無法找到它的。需指定位置,配合 systemPath 來使用。 #### 6.import **dependencyManagement 兩種依賴繼承** a. import 用法 父pom.xml(配置版本管理) ```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.test</groupId> <artifactId>test-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 集中定義依賴版本號 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.1.3.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.32</mysql.version> <slf4j.version>1.6.4</slf4j.version> <jackson.version>2.4.2</jackson.version> <druid.version>1.0.9</druid.version> </properties> <dependencyManagement> <dependencies> <!-- 連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 資原始檔拷貝外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java編譯外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat外掛 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> </project> ``` 子pom.xml實現版本 ```xml <dependencyManagement><!--如果是多模組的,可以配置在parent級pom.xml中--> <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> <!--配置依賴時不需要配置版本號,由dependencyManagement統一管理,便於維護--> <!--如果程式碼中沒有配置依賴,則不會拉取jar--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <!--<version>1.0.8</version>--> <!--如果配置了版本號,就不會使用dependencyManagement定義的版本號--> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> ``` b. parent 用法  ::: info > 1、需要maven2.9以上版本。 > 2、將dependency分類,每一類建立單獨的pom檔案 > 3、在需要使用到這些依賴的子model中,使用dependencyManagement管理依賴,並import scope依賴 > 4、scope=import只能用在dependencyManagement裡面,且僅用於type=pom的dependency ::: ### 三、Maven標籤說明  資料來源: [Maven Dependency Scope 的使用說明 ](http://pclevin.blogspot.com/2015/02/maven-dependency-scope.html) [maven中dependencyManagement標籤的簡單使用](https://www.itread01.com/content/1545295874.html) [maven pom类型](https://blog.csdn.net/wangjun5159/article/details/104298131) [Maven多项目继承](https://my.oschina.net/xuzimian/blog/3000296) [Overriding managed version 问题解决详解](https://blog.csdn.net/qq_41247433/article/details/86774155) ###### tags: `Maven` `pom.xml` `import`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up