Maven:构建战争但部署不同的工件?

bil*_*.cn 1 pom.xml maven-3 maven

我有一个 Maven 项目,它构建一个 WAR,然后将其打包到一个针对特定部署工具的 zip 文件中。我不想上传 WAR,因为这会浪费空间。

是否可以构建 WAR 但仅在同一个 pom 文件中上传/部署 zip ?

编辑:

不,上面建议的“重复”问题没有丝毫帮助。我必须指定<packaging>war</packaging>,即使我不指定,只要我使用 Maven War 插件,它就会将战争作为部署的一部分。

我还想要构建中的其他工件(源代码、测试等)。我只是不需要战争。

A_D*_*teo 5

以下是根据需要部署 war 或 zip 的建议方法:

  • 默认构建仍将提供 WAR 作为输出
  • 添加一个配置文件以跳过正常的 Maven 部署插件执行作为部署阶段的一部分,并添加进一步的执行(显然没有跳过),通过目标仅部署 ZIP 文件deploy-file,如 @Michal 所建议,但不通过命令行(更好)作为构建的一部分)。

根据需要,您当然可以将默认行为从配置文件切换到默认构建,甚至完全摆脱默认构建(只是 WAR)。

一个例子:

<build>
    <finalName>test-war-zip</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptor>src/assembly/descriptor.xml</descriptor>
            </configuration>
            <executions>
                <execution>
                    <id>create-zip</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>deploy-zip</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                    <executions>
                        <execution>
                            <id>deploy-zip</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>deploy-file</goal>
                            </goals>
                            <configuration>
                                <skip>false</skip>
                                <file>${project.build.directory}/your.file.here</file>
                                <repositoryId>your.id.here</repositoryId>
                                <url>http://something.here</url>
                                <groupId>${groupId}</groupId>
                                <artifactId>${artifactId}</artifactId>
                                <version>${version}</version>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

请注意 Maven 部署插件及其skipto的全局配置true。它将有效地跳过deploy目标。进一步执行将处理 ZIP 文件。

通过上述方法,执行正常构建,Maven 将继续部署生成的 WAR,同时打开配置文件,如下所示:

mvn clean deploy -Pdeploy-zip
Run Code Online (Sandbox Code Playgroud)

Maven 将跳过 Maven 部署插件(其目标)的默认执行,并在该阶段deploy执行目标。deploy-filedeploy

作为构建的一部分,您将拥有:

[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ test-war-zip ---
[INFO] 跳过工件部署
[INFO]
[INFO] --- maven-deploy-插件:2.8.2:部署文件(部署压缩)@ test-war-zip ---
上传:http://the.repository.here(986 B,3.9 KB/秒)