1. 程式人生 > >pom.xml檔案中dependency中optional屬性的作用

pom.xml檔案中dependency中optional屬性的作用

官方文件的說法是

當project-A 依賴project-B,  project-B 依賴project-D時

What if we dont want project D and its dependencies to be added to Project A's classpath because we know some of Project-D's dependencies (maybe Project-E for example) was missing from the repository, and you don't need/want the functionality in Project-B that depends on Project-D anyway. In this case, Project-B's developers could provide a dependency on Project-D that is <optional>true</optional>, like this:

<dependency>
  <groupId>sample.ProjectD</groupId>
  <artifactId>ProjectD</artifactId>
  <version>1.0-SNAPSHOT</version>
  <optional>true</optional>
</dependency>

所以當project-B的<optional>true</optional>時, project-A中如果沒有顯式的引入project-D, 則project-A不依賴project-D, 即project-A可以自己選擇是否依賴project-D

預設<optional>的值為false, 及子專案必須依賴