Maven耳朵插件的多个工件内容 [英] Maven ear plugin multiple artifacts content

查看:69
本文介绍了Maven耳朵插件的多个工件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有一个Web项目以及可以在其中部署的多个环境.而且我希望Maven一次构建多个工件(例如,对于开发产品).我有一个A-war模块和一个A-ear模块(其中包含A-war).每个战争人工制品可能包含仅与其环境有关的信息.

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.

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

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>

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

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

现在,我想为这些战争中的每一个建立 ear 工件.

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

这是A-ear模块中pom.xml的主要内容:

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>

它还会构建2个 ear ** s: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而不是其自身的一场战争与一场战争而且,当我更改执行顺序(将创建的产品移动到高于dev的位置)时,这些* ear *都包含A-war-prod.war.

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.

从maven输出中可以看到,开始构建 ear ** s时,它会将第一次战争复制到第一个** ear 的文件夹中,但是第二次却没有:

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

那么也许有人对每次强制Maven复制 war 文件有想法?

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

推荐答案

我发现,当Maven将 war 文件复制到temp目录中时,它不会重写-如果存在相同名称的文件,则不会被替换.因此,在第一个工件复制之后,它总是复制执行模块中指向的第一个工件.未复制所有其他工件.因此,该工件被放置在所有结果耳朵中.

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>

这篇关于Maven耳朵插件的多个工件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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