运行 .war 部署后的 Maven ant 插件?

Run Maven ant plugin after the .war is deployed?

我目前正在尝试根据我的 war 文件创建一个 docker 图像,该文件将在部署时在目标文件夹中创建。但是 ant 插件在将 maven war 文件部署到目标文件夹之前执行命令。因此,尽管它尝试创建一个 docker 图像,但它找不到要添加的必要 .war 文件,因为它尚未部署到目标文件夹中。

ant 插件也在名为 docker 的单独配置文件下声明。虽然我阅读了 Maven Lifecycle Doc,但找不到实现此目的的合适方法。

这是我的 .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/maven-v4_0_0.xsd">


<groupId>org.ravindu</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>ravindu_test</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<name>Ravindu_Test_Proj</name>

<dependencies>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.twitter4j</groupId>
        <artifactId>twitter4j-core</artifactId>
        <version>[4.0,)</version>
    </dependency>
    <dependency>
        <groupId>org.facebook4j</groupId>
        <artifactId>facebook4j-core</artifactId>
        <version>[2.1,)</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>20.0</version>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>**/imported_js/**</exclude>
                    <exclude>**/jsqrcode/**</exclude>
                    <exclude>**/imported_css/**</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/webapp</directory>
                        <excludes>
                            <exclude>**/imported_js/**</exclude>
                            <exclude>**/jsqrcode/**</exclude>
                            <exclude>**/css/**</exclude>
                            <exclude>**/images/**</exclude>
                            <exclude>**/jquery-mobile/**</exclude>
                        </excludes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
    <finalName>ravTest</finalName>
</build>

<profiles>
    <profile>
        <id>docker</id>
        <activation>
            <file>
                <exists>src/main/docker/Dockerfile</exists>
            </file>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/target</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>src/main/docker</directory>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.6</version>
                    <executions>
                        <execution>
                            <id>process-classes</id>
                            <phase>process-classes</phase>
                            <configuration>
                                <target>
                                    <exec executable="docker">
                                        <arg value="build"/>
                                        <arg value="-t"/>
                                        <arg value="ravindu_test"/>
                                        <arg value="target"/>
                                    </exec>
                                    <!--<exec executable="docker">-->
                                        <!--<arg value="push"/>-->
                                        <!--<arg value="ravindu_test"/>-->
                                    <!--</exec>-->
                                    <!--<exec executable="docker">-->
                                        <!--<arg value="tag"/>-->
                                        <!--<arg value="-f"/>-->
                                        <!--<arg value="ravindu_test"/>-->
                                        <!--<arg value="ravindu_test"/>-->
                                    <!--</exec>-->
                                    <!--<exec executable="docker">-->
                                        <!--<arg value="push"/>-->
                                        <!--<arg value="ravindu_test:latest"/>-->
                                    <!--</exec>-->
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

下面附上我的Dockerfile

# Pull base image
FROM tomcat:8-jre8

# Maintainer
MAINTAINER "Ravindu Fernando. <ravindu_fernando@gmail.com">

# Copy to images tomcat path
ADD /Ravindu_Test/target/mobile.war /usr/local/tomcat/webapps

我没有这个问题的确切答案。但我试图解释我们解决这个问题的方法。 Maven 是构建具有相同结构和构建阶段的标准项目的好工具。如果你想得到更复杂的东西,maven 会拒绝它。因此我们决定采用一种不同的方法:

  • Maven 仅用于构建应用程序(jar、war、等等...)
  • 所有 docker 个工件使用 bash 脚本创建,其中包含使用 Maven 构建应用程序、构建 docker 图像并将其推送到存储库的命令。

我认为为此 bash 比 maven

更方便和灵活

这个问题的原因并不像我最初认为的那样与 maven 有任何关系。问题出在我的 Dockerfile 上。它没有识别我的 Dockerfile 中指定的 war 文件的完全限定文件名路径。

在我将 .war 文件的完全限定路径更改为 /mobile.war 之后,在我完成了 docker 回购中提出的官方问题后,它起作用了.它从上下文

的根目录添加文件

Here is the link for that issue

这是我更新的 Dockerfile

# Pull base image
FROM tomcat:8-jre8

# Maintainer
MAINTAINER "Ravindu Fernando. <ravindu_fernando@gmail.com">

# Copy to images tomcat path
ADD /mobile.war /usr/local/tomcat/webapps

非常感谢您的解答和建议!