我有一个由 Maven 管理的原始 java webapp(因此会生成一个 WAR 文件),我们称它为 webapp
。
我喜欢保持其 Maven 配置不变,但有时我需要通过操作其内容(或以任何方式对其应用通用操作)来后期生成生成的 WAR。
所以我做了一个多模块项目:
- multimodule
+-- webapp
+-- operator
operator
可以做几件事。例如,它调用一个 (Java) 命令行程序来对 WAR 进行一些检查:我如何获取“webapp”模块输出(即 WAR 文件)并将其设置为 operator
的输入> 模块?
我不知道如何执行此操作,也不知道在网上搜索什么,所以我被困住了。
最佳答案
您需要使 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 将在 react 器中的 operator
项目之前构建 webapp,您将能够对该 war 进行后处理。
让我们举个例子,假设您想调用一个以这场 war 为参数的程序。
首先,必须将这个新依赖项复制到特定位置。这是通过 maven-dependency-plugin
完成的.这个插件有一个目标 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>
关于java - 如何使用 Maven Artifact 作为另一个 Artifact (或模块)的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32673153/