【问题标题】:Specify dependencies for Maven classifiers为 Maven 分类器指定依赖项
【发布时间】:2015-01-16 00:37:31
【问题描述】:

我有一个带有两个分类器的 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>

谢谢!

【问题讨论】:

    标签: java maven dependencies classification


    【解决方案1】:

    这实际上不是“行家”的正确做事方式。 你应该有一个 pom.xml 用于包含这些 pojos 的 jar 项目。

    你应该有另一个 pom.xml 用于将第一个项目作为依赖项的 war 项目。

    即使可以,也不应该在 1 个 pom 中创建多个不同的工件。 如您所见,它会导致逻辑问题。

    【讨论】:

    • 谢谢萨吉。我曾经只有一个项目并手动生成带有子部分的 jars,但是试图用 maven 保留这个想法给我带来了很多麻烦。我将改变我的项目的组织方式。
    猜你喜欢
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2013-02-09
    相关资源
    最近更新 更多