- compile (編譯)
compile是默認的範圍;若是沒有提供一個範圍,那該依賴的範圍就是編譯範圍。編譯範圍依賴在全部的classpath中可用,同時它們也會被打包。java
- provided (已提供)
provided 依賴只有在當JDK 或者一個容器已提供該依賴以後才使用。例如, 若是你開發了一個web 應用,你可能在編譯 classpath 中須要可用的Servlet API 來編譯一個servlet,可是你不會想要在打包好的WAR 中包含這個Servlet API;這個Servlet API JAR 由你的應用服務器或者servlet 容器提供。已提供範圍的依賴在編譯classpath (不是運行時)可用。它們不是傳遞性的,也不會被打包。web
- runtime (運行時)
runtime 依賴在運行和測試系統的時候須要,但在編譯的時候不須要。好比,你可能在編譯的時候只須要JDBC API JAR,而只有在運行的時候才須要JDBC
驅動實現。spring
- test (測試)
test範圍依賴 在通常的編譯和運行時都不須要,它們只有在測試編譯和測試運行階段可用。apache
- system (系統)
system範圍依賴與provided相似,可是你必須顯式的提供一個對於本地系統中JAR文件的路徑。這麼作是爲了容許基於本地對象編譯,而這些對象是系統類庫的一部分。這樣的構建應該是一直可用的,Maven 也不會在倉庫中去尋找它。若是你將一個依賴範圍設置成系統範圍,你必須同時提供一個systemPath元素。注意該範圍是不推薦使用的(建議儘可能去從公共或定製的 Maven 倉庫中引用依賴)。示例以下:springboot
<project> <dependencies> <dependency> <groupId>sun.jdk</groupId> <artifactId>tools</artifactId> <version>1.5.0</version> <scope>system</scope> <systemPath>${java.home}/../lib/tools.jar</systemPath> </dependency> ... </dependencies> </project>
- import(導入)
import僅支持在<dependencyManagement>
中的類型依賴項上。它表示要在指定的POM <dependencyManagement>
部分中用有效的依賴關係列表替換的依賴關係。該scope類型的依賴項實際上不會參與限制依賴項的可傳遞性。
服務器
2、scope的依賴傳遞
A–>B–>C。當前項目爲A,A依賴於B,B依賴於C,A與C的依賴關係?maven
- 當B對C的依賴的scope是test或者provided,則A不依賴C。
- 當B對C的依賴是scope是runtime或者compile,則A依賴C。且傳遞依賴的scope的規則:若是A對B的依賴是compile,那麼A對C的依賴和B對C的依賴相同,不然和A對B的依賴保持一致。
3、scope爲import的使用
前面說過該類型做用於只在dependencyManagement內使用生效,它能夠用來管理模塊依賴,說白了就是針對包含了一系列子依賴進的模塊導入到當前項目中進行管理使用,而不是把須要用到的依賴一個一個的加入到項目中進行管理,能夠理解爲多繼承模式。好比在一些場景中:咱們只是想單純加入springboot模塊的依賴,而不想將springboot做爲父模塊引入項目中,此時就可使用import來處理。ide
通常咱們會將springboot做爲父模塊引入到項目中,以下:spring-boot
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
一個項目通常只能有一個父依賴模塊,真實開發中,咱們都會自定義本身的父模塊,這樣就會衝突了。因此咱們可使用import來將springboot作爲依賴模塊導入本身項目中:
<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.example.demo</groupId> <artifactId>MyService</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <description>demo springboot</description> <inceptionYear>2019</inceptionYear> <organization> <name>若聲藝美</name> <url>http://baidu.com</url> </organization> <licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> </license> </licenses> <modules> <module>service</module> <module>common</module> <module>util</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 注入組件定義的第三方依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> ...... </dependencies> </dependencyManagement> <!-- 遠程倉庫配置 --> <distributionManagement> <repository> <id>releases</id> <url>http://ali/nexus/content/repositories/releases</url> </repository> <snapshotRepository> <id>snapshots</id> <url>http://ali/nexus/content/repositories/snapshots/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--使用-Dloader.path須要在打包的時候增長<layout>ZIP</layout>,不指定的話-Dloader.path不生效--> <layout>ZIP</layout> <!-- 指定該jar包啓動時的主類[建議] --> <mainClass>com.common.util.CommonUtilsApplication</mainClass> <!--<includes>--> <!--<!–依賴jar不打進項目jar包中–>--> <!--<include>--> <!--<groupId>nothing</groupId>--> <!--<artifactId>nothing</artifactId>--> <!--</include>--> <!--</includes>--> <!--配置的 classifier 表示可執行 jar 的名字,配置了這個以後,在插件執行 repackage 命令時, 就不會給 mvn package 所打成的 jar 重命名了,這樣就能夠被其餘項目引用了,classifier命名的爲可執行jar--> <!--<classifier>myexec</classifier>--> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 剔除spring-boot打包的org和BOOT-INF文件夾(用於子模塊打包) --> <!--<skip>true</skip>--> <source>1.8</source> <target>1.8</target> <!--<encoding>UTF-8</encoding>--> </configuration> </plugin> <!--拷貝依賴到jar外面的lib目錄--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>${plugin.assembly.version}</version> <configuration> <finalName>myservice</finalName> <descriptor>deploy/assembly.xml</descriptor> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </project>
上述就能夠將springboot模塊做爲依賴導入到項目中,而後就能夠繼承本身的父模塊了,若是要加入其它相似springboot這樣的模塊的話就和加入springboot同樣,這樣就可使模塊管理看起來更簡潔了,也實現了多繼承的效果。
附註:
在maven中常常會使用<optional>true</optional>參數,以下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
此處的<optional>true</optional>的做用是讓依賴只被當前項目使用,而不會在模塊間進行傳遞依賴。