【问题标题】:Why Maven compiles successfully without Servlet dependency?为什么 Maven 在没有 Servlet 依赖的情况下编译成功?
【发布时间】:2013-03-05 22:55:57
【问题描述】:

我正在编写一个 spring-mvc 应用程序(并学习 spring-mvc)。我没有向 pom.xml 添加 servlet 依赖项。当我运行mvn compile 或mvn package 时,它会成功完成。是不是应该抛出像“找不到 javax.servlet.http.HttpServlet”这样的异常?它是如何编译的?

mvn 依赖:列表:

已解决以下文件:[INFO]
aopalliance:aopalliance:jar:1.0:compile [INFO]
commons-dbcp:commons-dbcp:jar:1.4:compile [INFO]
commons-pool:commons-pool:jar:1.5.4:compile [INFO]
log4j:log4j:jar:1.2.17:compile [INFO]
org.codehaus.jackson:jackson-core-asl:jar:1.9.12:compile [INFO]
org.codehaus.jackson:jackson-mapper-asl:jar:1.9.12:compile [INFO]
org.slf4j:jcl-over-slf4j:jar:1.7.2:compile [INFO]
org.slf4j:slf4j-api:jar:1.7.2:compile [INFO]
org.slf4j:slf4j-log4j12:jar:1.7.2:compile [INFO]
org.springframework:spring-aop:jar:3.2.1.RELEASE:compile [INFO]
org.springframework:spring-beans:jar:3.2.1.RELEASE:compile [INFO]
org.springframework:spring-context:jar:3.2.1.RELEASE:compile [INFO]
org.springframework:spring-core:jar:3.2.1.RELEASE:compile [INFO]
org.springframework:spring-expression:jar:3.2.1.RELEASE:compile [INFO] org.springframework:spring-jdbc:jar:3.2.1.RELEASE:compile [INFO]
org.springframework:spring-tx:jar:3.2.1.RELEASE:compile [INFO]
org.springframework:spring-web:jar:3.2.1.RELEASE:compile [INFO]
org.springframework:spring-webmvc:jar:3.2.1.RELEASE:compile [INFO]
postgresql:postgresql:jar:9.2-1002.jdbc4:compile

【问题讨论】:

    标签: maven servlets spring-mvc


    【解决方案1】:

    因为 servlet API 是 org.springframework:spring-webmvc 的依赖项,因此是项目的传递依赖项。

    【讨论】:

      【解决方案2】:

      因为,Servlet Api 是 spring-webmvc 的传递依赖。 默认情况下,Maven 包含所有传递依赖项。

      使用以下命令列出依赖项及其传递依赖项

      mvn dependency:tree
      

      【讨论】:

        【解决方案3】:

        未完全实现: 仍然在您学习 spring-mvc 时,我建议仅在 pom 中保留所需的依赖项,这将告诉您哪个 jar 具有哪组类。

        还为 spring 特定包配置日志记录和调试,这极大地支持理解流程或可能出现的问题。当我从春天开始时,它对我帮助很大。学习愉快:)

        【讨论】:

          猜你喜欢
          • 2023-03-14
          • 2022-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-24
          • 2023-04-09
          • 1970-01-01
          • 2020-01-05
          相关资源
          最近更新 更多