简体   繁体   English

如何在Maven中继承Web应用程序的Java-EE API?

[英]How to inherit Java-EE API for Webapps in Maven?

My project has the following structure with multiple jars and wars: 我的项目具有多个jar和wars的以下结构:

root (pom)
+--core (jar)
+--webapp (jar)
+--childgroup (pom)
   +--actual-web-application (war)
   +--some-library (jar)
+--othergroup (pom)
   +--another-web-application (war)       

Where actual-web-application depends on webapp which depends on core. 实际的Web应用程序取决于Webapp,而后者取决于核心。 Ideally, I would like to specify the Java EE dependency only in the webapp module and let the actual-web-application inherit it. 理想情况下,我只想在webapp模块中指定Java EE依赖关系,然后让real-web-application继承它。 But because it's a provided dependency, this doesn't work and I have to manually add dependencies for provided stuff like Java EE, jax-rs etc. in every web application. 但是因为它是提供的依赖项,所以它不起作用,我必须在每个Web应用程序中手动为Java EE,jax-rs等提供的东西添加依赖项。

Is there any way, with dependencyManagement for example, to let actual-web-application inherit the javaee-api from the webapp? 是否有任何方法(例如,dependencyManagement)可以让实际的Web应用程序从Web应用程序继承javaee-api?

What I've tried, in webapp's pom.xml: 在Webapp的pom.xml中,我尝试了以下操作:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

And in actual-web-application's pom.xml: 在实际的网络应用程序的pom.xml中:

   <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <scope>provided</scope>
    </dependency>

However, it complies about the version so it seems like it's still not carrying over the dependency from webapp's pom.xml. 但是,它遵循该版本,因此似乎仍然没有继承webapp的pom.xml的依赖关系。

也将<scope>provided</scope>放入dependencyManagement

Provided dependencies are inherited just as any other dependency would be. 所提供的依赖项将像其他依赖项一样被继承。

parent pom.xml: 父pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

child pom.xml: 子pom.xml:

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
    </dependency>
</dependencies>

And the child's dependency tree: 和孩子的依赖树:

--- maven-dependency-plugin:2.8:tree (default-cli) @ child ---
com.iei.web:child:war:1.0-SNAPSHOT
\- javax:javaee-api:jar:7.0:provided
   \- com.sun.mail:javax.mail:jar:1.5.0:provided
       \- javax.activation:activation:jar:1.1:provided

If you are trying to avoid explicitly defining dependencies in the child at all, then you would of course have to move it out of dependencyManagement in the parent and make it a direct dependency, assuming you want every child module to inherit it. 如果您要完全避免在子级中显式定义依赖项,那么假设您希望每个子级模块都继承它,那么您当然必须将其移出父级中的dependencyManagement并使其成为直接依赖项。

Dependency Management 依赖管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM