我有一个包含两个分类器的 pom.xml 文件。 Maven 生成包含项目中所有文件的“WAR”和仅包含我选择的文件夹的“JAR”。
但是,我不知道如何表明POM中包含的依赖项仅对应于“WAR”文件的情况。 “JAR”中的类没有依赖项。
这对我来说是一个问题,因为如果我添加“JAR”作为其他项目的依赖项,Maven 将查找所有依赖项,有时会创建循环依赖项。
这是我的 pom.xml:
<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>
<groupId>my.group.id</groupId>
<artifactId>myartifactid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>name</name>
<description>description</description>
<dependencies>
<dependency>
<groupId>a.dependency.groupid</groupId>
<artifactId>articfact1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<classifier>all</classifier>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1.redhat-4</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>pojos</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<classifier>pojos</classifier>
<includes>
<include>my/project/pojos/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这就是我在其他项目中添加依赖项以仅包含 pojos JAR 的方法:
<dependency>
<groupId>my.group.id</groupId>
<artifactId>myartifactid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<classifier>pojos</classifier>
</dependency>
谢谢!
最佳答案
这实际上不是正确做事的“maven ”方式。 您应该有一个包含这些 pojo 的 jar 项目的 pom.xml。
您应该为 war 项目拥有另一个 pom.xml,该项目将第一个项目作为依赖项。
即使可以,也不应该在 1 个 pom 中创建多个不同的 Artifact 。 正如您所看到的,这会导致逻辑问题。
关于java - 指定 Maven 分类器的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000832/