Maven ear plugin multiple artifacts content

末鹿安然 提交于 2021-02-07 20:01:35

问题


Lets assume that I have a web project and several environments where it could be deployed. And I want Maven to build several artifacts at once (e.g. for dev an prod). I have an A-war module and an A-ear module (which contains A-war). Each war artifact could contain information which is related only to its environment.

First I configured a pom.xml file for A-war module:

<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>

This works fine - 2 *war*s are created in target folder: A-war-prod and A-war-dev.

Now I want to build ear artifact for each of these war.

Here is the main content of pom.xml in A-ear module:

<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>

It also builds 2 ear**s: A-ear-dev.ear and A-ear-prod.ear. And each of these *ear*s contains an A-war.war artifact. And everything would be fine except of one small detail: these **war files are the same. I mean A-ear-dev.ear contains A-war-dev.war (which is renamed to A-war.war) but A-ear-prod.ear contains the same A-war-dev.war instead of its own A-war-prod.war. Moreover when I'd changed the order of executions (moved creating of prod higher than dev) then these *ear*s both contained A-war-prod.war.

As I can see from maven output, when starting building ear**s it copies the first war into the folder for the first **ear but for the second it does not:

[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

So maybe anyone has an idea on how to force maven copy war file each time?


回答1:


As I found out, when Maven is copying war file into the temp directory, it doesn't rewrite it - if there is a file with the same name, then it will not be replaced. So after the first artifact copying, it copied always the first artifact which is pointed in execution module. All other artifacts were not copied. So this artifact was placed in all result ears.

So the solution for this issue is to specify working directory for each execution tag, like:

            <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>


来源:https://stackoverflow.com/questions/15655371/maven-ear-plugin-multiple-artifacts-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!