Maven ear插件有多个工件内容

And*_*rew 3 environment ear war maven

让我们假设我有一个Web项目和几个可以部署的环境.我希望Maven能够同时构建多个工件(例如,对于开发人员来说).我有一个A-war模块和一个A-ear模块(包含A-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>
Run Code Online (Sandbox Code Playgroud)

这很好 - 在目标文件夹中创建2*war*s:A-war-prod和A-war-dev.

现在我想为这些战争中的每一个构建神器.

以下是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>
Run Code Online (Sandbox Code Playgroud)

它还构建了2个耳朵:A-ear-dev.ear和A-ear-prod.ear.这些*ear*s中的每一个都包含一个A-war.war神器.除了一个小细节之外,一切都会好起来的:这些**战争文件都是一样的.我的意思是入耳式dev.ear包含A-战争dev.war(其重命名为A-war.war),但A-耳prod.ear包含相同的A-战争dev.war,而不是自己的A-战争prod.war.此外,当我改变执行的顺序(移动创建prod高于dev)时,这些*ear*s都包含A-war-prod.war.

正如我从maven输出中看到的那样,当开始构建耳朵时,它将第一次战争复制到第一个**耳朵的文件夹中,但是第二次战争不会:

[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
Run Code Online (Sandbox Code Playgroud)

那么也许任何人都有关于如何每次强制maven复制战争文件的想法?

And*_*rew 5

正如我发现的那样,当Maven将war文件复制到temp目录中时,它不会重写它 - 如果有一个同名文件,那么它就不会被替换.因此,在第一次工件复制之后,它总是复制在执行模块中指向的第一个工件.没有复制所有其他工件.所以这个神器放在所有结果耳朵里.

因此,此问题的解决方案是为每个执行标记指定工作目录,例如:

            <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>
Run Code Online (Sandbox Code Playgroud)