1. 程式人生 > >maven 中標籤中的 scope屬性的作用

maven 中標籤中的 scope屬性的作用

maven scope屬性值設定含義

1、列舉各個屬性值的含義

  • compile,預設值,適用於所有階段,會打包進專案。
  • provided,類似compile,期望JDK、容器或使用者會提供這個依賴。
  • runtime,只在執行時使用,如JDBC驅動,適用執行和測試階段。
  • test,只在測試時使用,用於編譯和執行測試程式碼。不會隨專案釋出。
  • system,類似provided,需要顯式提供包含依賴的jar,Maven不會在Repository中查詢它。

2、其它型別的屬性值都比較容易理解,這裡重點比較一下compile和runtime之間的區別:

(1)先描述一個簡單的例子:模組A依賴X,此時X的scope設定的值為runtime;

(2)另一模組B依賴A,則B在編譯時不會依賴X(編譯時不會有任何問題);

如果原先X的scope設定為compile,則說明在編譯的時B需要顯示的呼叫X的相關類,在maven依賴中最常見的設定為runtime的依賴是JDBC,主要原因是由於jdbc中對驅動類的配置是採用反射的機制在配置檔案中配置了class-name;

解決了當

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

時候

出現問題

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
[main 2018-11-24 14:56:56 ERROR]-org.springframework.boot.SpringApplication Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.yonyou.yuncai.angang.project.service.ProjectServiceApplication.main(ProjectServiceApplication.java:18)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:189)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)
    ... 8 common frames omitted

 

 

 

 

換成這個解決上述問題