如何使 Maven 将战争中常见的所有 jar 放在同一个 EAR 中到 EAR 根?

发布于 2024-07-19 18:25:04 字数 338 浏览 6 评论 0原文

我们有解决无数战争的办法。 Wars 的相似之处在于它们都使用 hibernate 和 spring。 这意味着每次战争中我们都会有许多相同的罐子。 这正在成为一个问题,因为耳朵的大小开始变得不成比例。

我想使用 Maven 来计算依赖关系并将多个战争通用的所有 jar 放置到 EAR 的根目录中。

我尝试使用 j2ee archetype (maven-archetype-j2ee-simple) 组织我的项目,但所有战争仍然与 WEB-INF/lib 内的依赖项一起打包。 有没有办法让Maven计算常见的依赖关系并将它们放入EAR中,就像他在构造war或jar时能够计算所有过渡依赖关系一样?

We have a solution with numerous wars. Wars are similar in the sense they all use hibernate and spring. This means that we have a number of same jars inside each war. This is becoming a problem, because the size of the ear is starting to grow out of proportion.

I would like to use Maven to calculate dependencies and to place all jars common to multiple wars to the root of the EAR.

I tried organizing my project using j2ee archetype (maven-archetype-j2ee-simple), but all wars are still packaged with dependencies inside the WEB-INF/lib.
Is there a way to make Maven calculate common dependencies and place them to EAR, just as he is able to calculate all transitional dependencies when constructing a war or a jar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

未蓝澄海的烟 2024-07-26 18:25:05

正如您在评论中提到的,maven 的任务是计算每个依赖项。 当您创建具有每个常见依赖项的工件时,您还必须猜测哪些依赖项属于那里。

也有可能,您必须部署一场战争,它依赖于另一台没有耳朵的机器,当您将每个战争依赖项设置为提供时,您会再次陷入困境。

获得瘦战争的唯一正确方法是从例子中:
http://maven.apache.org/plugins/maven -war-plugin/examples/skinny-wars.html

但是,现在有趣的部分来了,有一个大的! 快捷方式(完全消除了提到的痛苦),告诉 Maven,你的 WAR 有哪些依赖项。

进入 EAR 模块内部,为每个 WAR 依赖项使用 pom 类型声明对 WAR 的第二个依赖项。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.foo</groupId>
    <artifactId>skinny</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ear</artifactId>
<packaging>ear</packaging>
<dependencies>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <skinnyWars>true</skinnyWars>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <modules>
                    <webModule>
                        <groupId>com.foo</groupId>
                        <artifactId>war</artifactId>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</build>

现在,每个 WAR 将以其自己的依赖项独立打包,而 EAR 将与瘦 WAR 和 lib 文件夹内的每个依赖项一起打包

更新:

请记住,ear/lib 文件夹不能用于严格容器(如 JBoss EAP 6)中的每个依赖项 jar。 JSF 组件库(如 tomahawk、primefaces 等)必须驻留在 WEB-INF/lib 文件夹中。

使用上述解决方案实现此目的的一个简便方法是在 EAR pom.xml 中排除组件库,如下所示:

...
<dependencies>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
            <exclusion>
        </exclusions>
    </dependency>
</dependencies>
...

现在 WAR 的每个依赖项都将放置在 Ear/lib 中,但要放置的组件库除外在 WAR 内的 WEB-INF/lib 中

As you've mentioned in a comment, it's maven's task to calculate every dependency. When you're creating an artifact, with every common dependency, then you'll also have to guess, which dependencies belong there.

It could also be possible, that you have to deploy one war, with it's dependencies on another machine without an ear, an when you set every war dependency to provided, then you're stuck again.

The only right way, to get skinny wars is from the examples:
http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html

But, and now comes the interesting part, there is one big! shortcut (which completly takes away the mentioned pain), to tell maven, which dependencies your WARs have.

Go inside your EAR-Module an declare a second dependency on the WAR with type pom for every WAR dependency.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.foo</groupId>
    <artifactId>skinny</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ear</artifactId>
<packaging>ear</packaging>
<dependencies>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <skinnyWars>true</skinnyWars>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <modules>
                    <webModule>
                        <groupId>com.foo</groupId>
                        <artifactId>war</artifactId>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</build>

Now, every WAR will be packaged independently with it's own dependencies and the EAR will be packaged with skinny WARs and every dependency inside the lib folder

Update:

Keep in mind, that the ear/lib folder can't be used for every dependency jar in a strict Container like JBoss EAP 6. JSF Component libraries like tomahawk, primefaces, etc. have to reside in WEB-INF/lib folder.

A handy way to achieve this with the above described solution is to make an exclusion for the component library in the EARs pom.xml like this:

...
<dependencies>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.foo</groupId>
        <artifactId>war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
            <exclusion>
        </exclusions>
    </dependency>
</dependencies>
...

Now every dependency of the WAR will be placed in ear/lib except the component library which will be placed in WEB-INF/lib inside the WAR

尴尬癌患者 2024-07-26 18:25:05

创建一个名为 commons-jars 的新工件并将其打包为 pom.xml。 它应该取决于您正在使用的所有常见 jar - Spring、Hibernate、Log4j 等。

然后,在您的每个 wars 中将其添加为范围“provided”的依赖项(并且不要忘记将类型设置为 pom)。 您将能够在类路径中看到它,但它们不会被打包到战争中。 这样,您还可以将特定于战争的依赖项打包到其中,该解决方案来自 瘦战争不提供。

Create a new artifact named commons-jars and package it as pom. It should depend on all the common jars you are using - Spring, Hibernate, Log4j, etc.

Then, in each on your wars add it as dependency with scope "provided" (and don't forget to set the type as pom). You will be able to see it in your classpath but they won't be packaged into the war. This way you can also have war specific dependencies packaged into it, which the solution from skinny wars does not provide.

落花浅忆 2024-07-26 18:25:05

您可以将依赖范围设置为“提供” 。 这意味着它们将由其他模块提供,并且不会包含在最终的 jar 或 war 中。

也许 程序集插件 可以帮助您打包最终的 EAR 并放置那里有常见的罐子。

You can set the dependancies scope to "provided". This means they will be provided by some other module and will not be included in the final jar or war.

Perhaps the assembly plugin can help you when packaging up the final EAR and place common jars there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文