带有包含不同版本的依赖项的WAR的EAR [英] EAR with WARs containing different versions of dependencies

查看:63
本文介绍了带有包含不同版本的依赖项的WAR的EAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的要求

EAR
WAR(代码)
---- Dependency A v1
WAR(与上面的WAR相同的代码)
---- Dependency A v2

由于WAR项目的代码相同,因此我不想为WAR项目创建多个代码库.因此,我正在寻找EAR pom.xml,以使maven生成具有依赖项A v1的WAR,该依赖项可以在构建WAR时作为某些属性传递.

EAR pom.xml
...
(模块)
(webmodule)
(artifact)WAR(/artifact)
依赖版本v1
(content-path)/warwithv1(/content-path)
(/webmodule)
(webmodule)
(artifact)WAR(/artifact)
依赖版本v2
(content-path)/warwithv2(/content-path)
(/webmodule)
...


谢谢

EAR
WAR (Code)
----Dependency A v1
WAR (Code same as WAR above)
----Dependency A v2

AS the code for WAR project is same I don't want to create multiple code base for WAR project. So I'm looking for EAR pom.xml in such a way that maven builds the WAR with dependency A v1 which can be passed as some property while building WAR.

EAR pom.xml
...
(modules)
(webmodule)
(artifact)WAR(/artifact)
dependency version v1
(content-path)/warwithv1(/content-path)
(/webmodule)
(webmodule)
(artifact)WAR(/artifact)
dependency version v2
(content-path)/warwithv2(/content-path)
(/webmodule)
...


Thanks

对不起,XML标签

推荐答案

编辑:答案在澄清后已更改.

EDIT: Answer changed after clarification in comments.

我现在所理解的问题是:一个人如何使用单个POM来创建2个WAR文件,它们都具有相同的源代码但依赖性不同.

The question as I now understand it is: How does one use a single POM to create 2 WAR files that both have the same source code but different dependencies.

我建议的解决方案是创建一个父POM,该父POM将为每个WAR指定一个模块.其中一个模块将包含war的源代码,并指定一种版本的依赖关系.第二个模块将参考第一个模块的源代码,并指定依赖关系的第二个版本.

The solution I propose is to create a parent POM that will specify one module per WAR. One of the modules will contain the source code for the war and specify one version of dependencies. The second module will refer to the first module for its source code and will specify a second version of dependencies.

这是我的高级项目结构:

Here is my high level project structure:

这是顶层(父)POM:

Here is the top level (parent) POM:

<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>
    <groupId>maven-war-diff-depend</groupId>
    <artifactId>maven-war-diff-depend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>module1</module>
        <module>module2</module>
    </modules>
</project>

Module1将指定v4的log4j2,这是它的POM:

Module1 will specify v2.3 of log4j2, here is its POM:

<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>maven-war-diff-depend</groupId>
        <artifactId>maven-war-diff-depend</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>module1</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>
</project>

Module2将指定log4j2的v2.8.1并指向module1以获得其源代码和web.xml文件.请注意,您可能需要做其他工作才能引用module1中的任何其他资源,例如添加更多的maven插件和配置.

Module2 will specify v2.8.1 of log4j2 and point to module1 for its source code and web.xml file. Note that you may need to do additional work to refer to any other resources in module1 such as adding more maven plugins and configuration.

这是module2 POM:

Here is the module2 POM:

<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>maven-war-diff-depend</groupId>
        <artifactId>maven-war-diff-depend</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>module2</artifactId>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>../module1/src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                <webXml>../module1/src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.1</version>
        </dependency>
    </dependencies>
</project>

请注意,我在module1中只有一个Java类,只是为了表明实际上该类已编译并包含在module1和module2中:

Note that I only have a single Java class in module1 just to show that in fact this class is compiled and included in both module1 and module2:

这是一个简单的全新安装生成的结果:

Here are the results of a simple clean install build:

一旦构建了两个WAR文件,添加另一个包含两个WAR文件的模块(这就是您的EAR)就相当简单了-我相信这是您的最终目标.

Once you have the two WAR files built it's fairly simple to add another module (this would be your EAR) which will include both of the WAR files - I believe that is your final goal.

因此,创建第三个模块:

So, create a third module:

将新模块添加到您的根级别(父级)POM:

Add the new module to your root level (parent) POM:

< module> module3</module>

在新模块的POM中创建必要的配置.这涉及添加对您使用module1和module2创建的其他两个工件的依赖关系,并根据需要配置ear插件.

Create the necessary configuration in your new module's POM. This involves adding dependencies on the other two artifacts you create with module1 and module2 and configuring the ear plugin as needed.

<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>maven-war-diff-depend</groupId>
        <artifactId>maven-war-diff-depend</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>module3</artifactId>
    <packaging>ear</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <applicationXml>${basedir}/target/application.xml</applicationXml>
                    <modules>
                        <webModule>
                            <groupId>maven-war-diff-depend</groupId>
                            <artifactId>module1</artifactId>
                        </webModule>
                        <webModule>
                            <groupId>maven-war-diff-depend</groupId>
                            <artifactId>module2</artifactId>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>maven-war-diff-depend</groupId>
            <artifactId>module1</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>maven-war-diff-depend</groupId>
            <artifactId>module2</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>war</type>
        </dependency>
    </dependencies>
</project>

现在,在构建时,它将创建一个包含模块1和模块2生成的2个WAR的EAR.在此示例中,耳朵简称为module3:

Now when you build it will create an EAR that includes the 2 WARs generated by module1 and module2. The ear in this example is simply called module3:

按要求在注释中重新添加原始答案.

这是原始投标中的POM-使用两个配置文件指定不同的依赖版本.如前所述,请注意,将servlet api包含在WAR文件中不是一个好主意,它只是在这里用作直观示例说明如何指定公共依赖项.

Here is the POM from the original proposal - using two profiles to specify different dependency versions. As before please note that it's not a good idea to include servlet api inside of a WAR file, it's just being used here as a visual example to show how common dependencies are specified.

<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>
    <groupId>maven-war-diff-depend</groupId>
    <artifactId>maven-war-diff-depend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>v2.3</id>
            <dependencies>
                <dependency>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-core</artifactId>
                    <version>2.3</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>v2.8.1</id>
            <dependencies>
                <dependency>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-core</artifactId>
                    <version>2.8.1</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

这篇关于带有包含不同版本的依赖项的WAR的EAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆