Maven ear 插件多个 Artifact 内容

标签 maven war environment ear

让我们假设我有一个 Web 项目和几个可以部署它的环境。我希望 Maven 一次构建多个 Artifact (例如,用于开发产品)。我有一个 A-war 模块和一个 A-ear 模块(其中包含 A-war)。每个 war 文物都可能包含仅与其环境相关的信息。

首先我为 A-war 模块配置了一个 pom.xml 文件:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <executions>
                    <execution>
                        <id>package-prod</id>
                        <phase>package</phase>
                        <configuration>
                            <classifier>prod</classifier>
                            <webappDirectory>${project.build.directory}/${project.build.finalName}-prod</webappDirectory>
                            <webResources>
                                <resource>
                                    <directory>src/env/prod</directory>
                                </resource>
                            </webResources>
                        </configuration>
                        <goals>
                            <goal>war</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>package-dev</id>
                        <phase>package</phase>
                        <configuration>
                            <classifier>dev</classifier>
                            <webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory>
                            <webResources>
                                <resource>
                                    <directory>src/env/dev</directory>
                                </resource>
                            </webResources>
                        </configuration>
                        <goals>
                            <goal>war</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
    <finalName>A-war</finalName>
</build>

这工作正常 - 在目标文件夹中创建了 2 *war*s:A-war-prod 和 A-war-dev。

现在我想为这些 war 中的每一个构建ear神器。

A-ear模块中pom.xml的主要内容如下:

<dependencies>
    <dependency>
        <groupId>gr.id</groupId>
        <artifactId>A-war</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <classifier>prod</classifier>
        <scope>provided</scope>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>gr.id</groupId>
        <artifactId>A-war</artifactId>
        <classifier>dev</classifier>
        <version>0.0.1-SNAPSHOT</version>
        <scope>provided</scope>
        <type>war</type>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>package-dev</id>
                    <phase>package</phase>
                    <configuration>
                        <classifier>dev</classifier>
                        <version>5</version>
                        <modules>
                            <webModule>
                                <groupId>gr.id</groupId>
                                <artifactId>A-war</artifactId>
                                <classifier>dev</classifier>
                                <contextRoot>/A-war</contextRoot>
                                <bundleFileName>/A-war.war</bundleFileName>
                            </webModule>
                        </modules>
                    </configuration>
                    <goals>
                        <goal>ear</goal>
                    </goals>
                </execution>
                <execution>
                    <id>package-prod</id>
                    <phase>package</phase>
                    <configuration>
                        <classifier>prod</classifier>
                        <version>5</version>
                        <modules>
                            <webModule>
                                <groupId>gr.id</groupId>
                                <artifactId>A-war</artifactId>
                                <classifier>prod</classifier>
                                <contextRoot>/A-war</contextRoot>
                                <bundleFileName>/A-war.war</bundleFileName>
                            </webModule>
                        </modules>
                    </configuration>
                    <goals>
                        <goal>ear</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>A-ear</finalName>
</build>

它还构建了 2 个 ear**:A-ear-dev.ear 和 A-ear-prod.ear。这些 *ear* 中的每一个都包含一个 A-war.war 神器。除了一个小细节外,一切都会很好:这些 **war 文件是相同的。我的意思是 A-ear-dev.ear 包含 A-war-dev.war(重命名为 A-war.war)但 A-ear-prod.ear 包含相同的 A-war-dev.war 而不是它自己的A-war-prod.war。 此外,当我更改执行顺序(将 prod 的创建移动到高于 dev 的位置)时,这些 *ear* 都包含 A-war-prod.war。

正如我从 maven 输出中看到的那样,当开始构建 ear**s 时,它将第一次 war 复制到第一个 **ear 的文件夹中,但对于第二个它没有:

[INFO] --- maven-ear-plugin:2.8:ear (package-dev) @ A-ear
---
[INFO] Copying artifact [war:gr.id:A-war:dev:0.0.1-SNAPSHOT] to [/A-war.war]
[INFO] Including custom manifest ....
[INFO] Copy ear sources to ...

[INFO] Building jar: <location>\A-ear\target\A-ear-dev.ear

....
[INFO] --- maven-ear-plugin:2.8:ear (package-prod) @ A-ear ---
[INFO] Copy ear sources to ...
[INFO] Including custom manifest file ...

[INFO] Building jar: <location>\A-ear\target\A-ear-prod.ear

所以也许有人知道如何强制 maven 每次都复制 war 文件?

最佳答案

正如我所发现的,当 Maven 将 war 文件复制到临时目录中时,它不会重写它 - 如果存在同名文件,则不会替换它。所以在第一个 Artifact 复制之后,它总是复制第一个指向执行模块的 Artifact 。 所有其他 Artifact 均未复制。所以这个神器被放置在所有结果耳朵

所以这个问题的解决方案是为每个执行标签指定工作目录,比如:

            <execution>
                <id>package-prod</id>
                <phase>package</phase>
                <configuration>
                    <workDirectory>target/prod</workDirectory>
                    <classifier>prod</classifier>
                    <version>5</version>
                    <modules>
                        <webModule>
                            <groupId>gr.id</groupId>
                            <artifactId>A-war</artifactId>
                            <classifier>prod</classifier>
                            <contextRoot>/A-war</contextRoot>
                            <bundleFileName>/A-war.war</bundleFileName>
                        </webModule>
                    </modules>
                </configuration>
                <goals>
                    <goal>ear</goal>
                </goals>
            </execution>

关于Maven ear 插件多个 Artifact 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15655371/

相关文章:

user-interface - 开发多显示器

java - org.jivesoftware :smack:jar:3. 4.1-0cec571 的 POM 丢失

jakarta-ee - 在同一个 Gradle 项目中生成 WAR 和 EAR 工件

java - 使用maven和spring boot创建jar的问题

maven-2 - Maven - 从 war 中排除类文件

eclipse - 如何从 Eclipse Java EE Web 应用程序项目部署程序集中排除某些文件

c - 如何在 C 中找到当前机器的完整主机名(主机名和域信息)?

python - 在哪里为 Django 项目设置 Python 环境属性?

maven - 如何使 FlexUnit 的代码覆盖率与 Sonar 配合使用?

maven - 如何从自定义 Maven 插件创建网站内容?