compile:默認,編譯范圍,會被打包;
provide:已經提供范圍,比如文件A.java在打包的時候需要xxx.jar,這個jar在目標環境中已經存在了,我們不需要把他也一起打包,就用provided
runtime:編譯的時候不需要,運行和測試的時候需要;
test:在編譯和運行的時候不需要,只有在測試的時候才需要;
system:類似provided,但是必須提供依賴的路徑,比如所以來的jar文件的位置;這樣maven不會在本地倉庫中尋找它了;不被推薦使用;
2 dependency中的version
provide:已經提供范圍,比如文件A.java在打包的時候需要xxx.jar,這個jar在目標環境中已經存在了,我們不需要把他也一起打包,就用provided
runtime:編譯的時候不需要,運行和測試的時候需要;
test:在編譯和運行的時候不需要,只有在測試的時候才需要;
system:類似provided,但是必須提供依賴的路徑,比如所以來的jar文件的位置;這樣maven不會在本地倉庫中尋找它了;不被推薦使用;
(, )不包含,[, ]包含,例如:
<version>1.4.1</version> 版本是1.4.1
<version>[3.8,4.0)</version> 版本是>=3.8并且<4.0
<version>[,4.0)</version> 版本是任何<4.0
<version>[3.8,)</version> 版本是任何>=3.8
3 dependency是傳遞的<version>1.4.1</version> 版本是1.4.1
<version>[3.8,4.0)</version> 版本是>=3.8并且<4.0
<version>[,4.0)</version> 版本是任何<4.0
<version>[3.8,)</version> 版本是任何>=3.8
比如spring依賴common-xxx依賴,我們只需要引入spring dependency即可,maven會自動去找spring的那些依賴;
4 dependency依賴排除有時候A依賴B,B依賴C,但是C在中央倉庫或者你的倉庫中沒有,就會有問題。比如hibernate依賴Sun JTA API,但是他在中央Maven 倉庫中沒有,而在倉庫中有另一個JTA實現,并不是Sun的,就可以這么用:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.5.ga</version>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
這兩個依賴是沒有關系的,只不過是在geronimo-jta_1.1_spec提供的內容正好是hibernate所需要的;
5 dependencyManagement的作用<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.5.ga</version>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
這兩個依賴是沒有關系的,只不過是在geronimo-jta_1.1_spec提供的內容正好是hibernate所需要的;
擋在parent pom.xml中定義了dependencyManagement之后,子項目可以引用它而不用聲明版本號,就是說他會默認parent pom.xml中的版本號,比如:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.2</version>
</dependency>

<dependencies>
</dependencyManagement>
那么子項目就可以引用它:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>a-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>project-a</artifactId>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
可以在子pom.xml中寫版本,那樣會覆蓋parent pom.xml中的依賴的版本;
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.2</version>
</dependency>

<dependencies>
</dependencyManagement>
那么子項目就可以引用它:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>a-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>project-a</artifactId>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
可以在子pom.xml中寫版本,那樣會覆蓋parent pom.xml中的依賴的版本;