如何使用 Maven 工件作为另一个工件(或模块)的输入?

How to use a Maven artifact as input of another artifact (or module)?

我有一个由 Maven 管理的原始 java webapp(因此生成 WAR 文件),我们称它为 webapp.

我喜欢保持它的 Maven 配置不变,但有时我需要,比如说,post-通过操作其内容(或者无论如何应用通用操作)生成生成的 WAR它)。

所以我做了一个多模块项目:

- multimodule
    +-- webapp
    +-- operator

operator 可以做几件事。例如,它调用一个 (Java) 命令行程序,该程序对 WAR 进行一些检查:我如何获取 'webapp' 模块输出(即 WAR 文件)并将其设置为 operator 模块的输入?

我不知道如何执行此操作,也不知道在网上搜索什么,所以我被困住了。

看看maven profiles

您应该能够定义两个配置文件并配置第二个配置文件来执行其他操作。如果按照这种方法,您将不需要两个模块。

您需要使 webapp 成为 operator 项目的依赖项。

operator 的示例 POM:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <groupId>my-groupid</groupId>
    <artifactId>operator</artifactId>
    <dependencies>
        <dependency>
            <groupId>my-groupid</groupId>
            <artifactId>webapp</artifactId>
            <version>${project.version}</version>
            <type>war</type>
        </dependency>
    </dependencies>
</project>

通过创建显式依赖项,Maven 将在反应器中的 operator 项目之前构建 webapp,您将能够 post-process war.

让我们举个例子,假设您想调用一个以此 war 作为参数的程序。

首先,必须将此新依赖项复制到特定位置。这是通过 maven-dependency-plugin. This plugin has a goal copy-dependencies 完成的,它用于将项目的所有直接依赖项复制到文件系统中的某个位置。示例配置为:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/libs</outputDirectory>
            </configuration>
        </execution>
    </executions>
<plugin>

现在依赖项在文件系统中可用,您可以运行 使用exec-maven-plugin 的程序。示例配置,启动 operator -param1 webapp.war:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
        <configuration>
            <executable>operator</executable>
            <workingDirectory>${project.build.directory}/libs</workingDirectory>
            <arguments>
                <argument>-param1</argument>
                <argument>webapp.war</argument>
            </arguments>
        </configuration>
    </executions>
</plugin>