java - 多模块项目的 Maven 依赖范围

标签 java maven

我正处于java项目的初始阶段(带有maven的jsp/servlet项目)。这是一个多模块项目。我正在使用maven。 现在假设有 3 个模块。

模型、 Controller 和 Web 模块。
model 需要 mysql lib
controller使用model,扩展并使用model的类
web 需要 controllermodel 两个模块。

我已将 mysql 添加到模型的依赖项中,将模型添加到 controller 模块的依赖项中,并将 controller 添加到 web 模块的依赖项中。 (我需要将 model 模块添加到 web 模块的依赖项吗?)

我可以/应该如何配置这些模块的依赖范围?

编辑:结构

Root
 |
 |---Model  *dependencies:mysqllib/compile*
 |
 |---Controller *dependencies:Model/compile*
 |
 |---Web  *dependencies:Controller/compile*

所有内容都设置为complie范围。
我在 web 模块中有一个 .jsp 文件(这是默认的 jsp),
该jsp调用 Controller 模块内部类的静态方法Account.getAccount()

JSP代码

<%=xy.controller.Account.getAccount()%>

在浏览器中打开上面的jsp文件时,我在浏览器中遇到以下异常
错误堆栈

    org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 8 in the jsp file: /index.jsp
xy.controller.AccountH cannot be resolved to a type
5:     <title></title>
6:   </head>
7:   <body>
8:                        <%=xy.controller.Account.getAccount()%>
9:   </body>
10: </html>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

pom 文件中的依赖关系
- Controller - pom

<dependency>
            <groupId>xy</groupId>
            <artifactId>model</artifactId>
            <version>0.0.1</version>
            <scope>compile</scope>
        </dependency>
  • 网络 - pom
<dependency>
            <groupId>xy</groupId>
            <artifactId>controller</artifactId>
            <version>0.0.1</version>
            <scope>compile</scope>
        </dependency>

最佳答案

当前项目及其祖先中的所有依赖项均可用于项目。

Root  *dependencies:libA*
 |
 |---Model  *dependencies:mysqllib*
 |
 |---Controller *dependencies:Model*
 |
 |---Web  *dependencies:Model,Controller*

要回答您的问题:,如果您有上述结构,您将需要显式地将模型添加到 Web 模块的依赖项列表中。请注意,libA 可用于所有 3 个模块,但 mysqllib 仅可用于模型。

您可以尝试处理亲子关系来决定如何构建项目。

关于java - 多模块项目的 Maven 依赖范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18681692/

相关文章:

java - Maven 无法使用嵌套架构生成 JAXB 类

Java 静态嵌套类型语义

java - 在哪里可以找到 combo-box-table-cell css 样式的规则?

java - Neo4j Spring Data 示例缺少注释 @Indexed

java - JBoss 中的 ModuleNotFoundException 错误

java - Dropwizard 健康检查超时?

java - 使用格式(字符串、字符串、枚举)的 JUnit 测试

java - @org.testng.annotations.Test 和 @org.junit.Test 问题

java - Maven-如何找到负责我当前项目正在使用的 jar 文件的组 ID?

java - 如何将警告更改为容易出错的错误?