PROJECT-A 是一场 war 。它有一些我想在 PROJECT-B 的单元测试中使用的代码。
为此,我想我需要 PROJECT-A 输出一个包含其类的 jar。以下是 POM 的摘录:
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<groupId>au.com.name.redacted</groupId>
<artifactId>PARENT-PROJECT</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PROJECT-A</artifactId>
<packaging>war</packaging>
... snip
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>PROJECT-A-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
当我运行时
cd PROJECT-A
mvn clean install
它输出
[INFO] --- maven-jar-plugin:2.4:jar (PROJECT-A-jar) @ PROJECT-A ---
[INFO] Building jar: D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ PROJECT-A ---
[INFO] Installing D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar to C:\Users\myusername\.m2\repository\au\com\name\PARENT-PROJECT\PROJECT-A\1.0\PROJECT-A-1.0.war
我确实看到了
D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.jar
D:\Workspace\name\PROJECT-A\target\PROJECT-A-1.0.war
但我在我的仓库中没有看到 jar - 只有 war 。
C:\Users\myusername\.m2\repository\au\com\name\PARENT-PROJECT\PROJECT-A\1.0\PROJECT-A-1.0.war
那么在 PROJECT-B 中,我尝试让它将 PROJECT-A 作为依赖项..
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<groupId>au.com.name.redacted</groupId>
<artifactId>PARENT-PROJECT</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PROJECT-B</artifactId>
<packaging>war</packaging>
... snip
<dependency>
<groupId>au.com.name.redacted</groupId>
<artifactId>PROJECT-A</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
它失败了
[ERROR] Failed to execute goal on project PROJECT-B: Could not resolve dependencies for project au.com.name.redacted:PROJECT-B:war:1.0: Failure to find au.com.name.redacted:PROJECT-A:jar:1.0 in http://repo.server.com.au:8081/artifactory/repo was cached in the local repository, resolution will not be reattempted until the update interval of repo has elapsed or updates are forced -> [Help 1]
最佳答案
我为此目的使用了 war 插件。也许这会有所帮助:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
它在构建后生成带有源代码的 war 和 jar,并将其提交到本地 m2 存储库。配置<attachClasses>true</attachClasses>
很重要。
在项目 B 的 pom 中使用类似这样的内容(重要部分 - <classifier>classes</classifier>
):
<dependency>
<groupId>projectA-groupid</groupId>
<artifactId>projectA-artifactId</artifactId>
<version>projectA-version</version>
<classifier>classes</classifier>
</dependency>
关于java - Maven 为另一个项目的测试构建 jar war ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820134/