【问题标题】:How to include dependency with 'provided' scope with maven-assembly-plugin如何使用 maven-assembly-plugin 将依赖项包含在“提供”范围内
【发布时间】:2011-12-31 07:00:48
【问题描述】:

我正在与 maven 斗争,通过使用 maven-assembly-plugin 将具有“提供”范围的托管依赖项包含到 tar 文件中。

我使用超级父 pom 文件作为我所有项目的基础。大多数项目将部署在应用程序服务器下,因此在超级父 pom 下声明了两个常见的依赖项。下面是父级的相关管理部分:

http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx.integration</groupId>
    <artifactId>super-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.1.3</version>
    <name>super parent</name>
    <url>http://maven.apache.org.check</url>
.
.
.
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

log4j.version=2.0.8

在一个继承的项目(它是一个独立的应用程序)中,我将 maven-assembly-plugin 与dependencySets 一起使用,以便将依赖库包含到一个 tar 文件中。当然,我还想包含 log4j 库。

下面是从父级继承的pom:

<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.xxx.integration</groupId>
        <artifactId>super-parent</artifactId>
        <version>1.1.3</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>plugin-cc-checker</artifactId>
    <name>plugin-cc-checker</name>
    <version>2.1</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>com.orca.integration</groupId>
                        <artifactId>integration-assembly-descriptor</artifactId>
                        <version>1.1.1</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>make-assembly-according-to-distribution-xml</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${artifactId}</finalName>
                            <!-- This is where we use our shared assembly descriptor -->
                            <descriptors>
                                <descriptor>distribution-app.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xerces</artifactId>
            <version>${xerces.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging-api</artifactId>
            <version>${commons-logging-api.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>excalibur</groupId>
            <artifactId>excalibur-i18n</artifactId>
            <version>${excalibur-i18n.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.snmp4j</groupId>
            <artifactId>snmp4j</artifactId>
            <version>${snmp4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

distribution-app.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!-- Add module dependencies and the jar that is created in the packaging 
        phase. Product name will be <project name>-app-<version no>.tar -->
    <id>app-${version}</id>
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>resources/app</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <excludes>
                <!-- Since there is a bug in xalan 2.7.1 all applications required to 
                    use xalan-orca jar file -->
                <exclude>xalan:xalan</exclude>
            </excludes>
            <!-- includes> <include>*</include> </includes-->
        </dependencySet>
    </dependencySets>
    <moduleSets>
        <moduleSet>
            <binaries>
                <outputDirectory>/guy</outputDirectory>
                <includes>
                    <include>log4j:log4j</include>
                </includes>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

为什么 maven-assembly-plugin 拒绝将 log4j 包含到 tar 文件中? PS,尝试更改范围以进行编译也不起作用。我不能更改超级父 pom 中的声明。

【问题讨论】:

  • 我可以看到子 pom 没有 version for log4j ?在这种情况下,它是否会从父级单独继承它并覆盖范围?
  • 它将继承其父 pom 的版本。无论如何我尝试添加版本,但它仍然没有任何区别。

标签: maven


【解决方案1】:

无法覆盖 maven 中的“提供”范围。

为了解决这个问题,我在父 pom 中声明了一个变量来定义工件范围。为了覆盖作用域,唯一要做的就是为继承的 pom 中的变量设置新值。

请看下面的例子:

父pom:

    <properties>
            <log4j.version>1.2.8</log4j.version>
            <log4j.scope>provided</log4j.scope>
    </properties>
.
.
.
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
                <scope>${log4j.scope}</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

现在在子 pom 中,再次声明变量:

<properties>
        <log4j.scope>runtime</log4j.scope>
</properties>

【讨论】:

  • 可以通过在子 POM 中管理托管依赖项来覆盖它。请参阅下面的答案。
  • 我不想对此投反对票,因为使用变量的想法很有吸引力。然而,Zoltan 是正确的——您可以使用他的“dependencyManagement”标记方法覆盖“提供的范围”依赖项。
【解决方案2】:

您可以通过在 POM 中的 &lt;dependencyManagement&gt; 标记内声明它来覆盖托管依赖项,您希望它被覆盖。

在您的情况下,您应该将以下内容添加到您的子 pom 中:

<dependencyManagement>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencyManagement>

请注意,这会覆盖在父 POM 的依赖管理中声明的所有内容,即您不能不声明 versionscope 并期望它被继承。

【讨论】:

  • 非常好的解决方案!
  • 而且比组装好;)
【解决方案3】:

这可以使用 Assembly 插件来完成。

首先使用以下内容创建一个assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>bin</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
        <dependencySet>
            <unpack>true</unpack>
            <scope>provided</scope>
        </dependencySet>
    </dependencySets>
</assembly>

然后只需在您的 pom.xml 中启用它

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptor>src/main/assembly/assembly.xml</descriptor>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这将创建一个 yourproject-bin.jar,其中将包含所有已分解的编译和提供的资源,以便可以在类路径中引用它们。

java -cp yourproject-bin.jar com.yourcompany.Main

【讨论】:

  • 解包依赖总是一个非常糟糕的主意。不同的库可以在相同的位置拥有公共文件,例如 beans.xml 元信息。如果您知道自己在做什么,请永远不要解压缩依赖项。组织您的程序集以拥有一个 lib/ 文件夹。然后你会没事的。包含原始 JAR 文件,不要提取任何东西!永远。
【解决方案4】:

我遇到了类似的问题,试图组装一个具有“已提供”范围的依赖项的项目。 我找到了解决此问题的方法:

  • 将依赖项留在“已提供”范围内
  • 使用 maven-dependency-plugin (example) 将依赖项复制到目标文件夹
  • 在程序集描述符中使用 fileSet 将依赖项捆绑为文件。

【讨论】:

    【解决方案5】:

    您可以简单地将范围定义到您的assembly descriptor

    【讨论】:

    • 我尝试使用程序集描述符,但它根本不起作用。无论如何。
    • 您使用的是哪个版本的 Maven 以及哪个版本的 Maven-Assembly-Plugin ?
    • Maven版本是3.0.3,Maven-Assembly-Plugin没有版本,所以是最新的。
    • 实际上 maven 3.0.3 如果没有被覆盖,则专门使用超级 pom 中的 maven-assembly 插件的 2.2-beta-5 版本。它有一个 部分,其中包含 antrun、程序集、依赖项和发布插件的版本 - 并带有注释说明它们将在将来的某个时候被删除。
    猜你喜欢
    • 2018-09-21
    • 2014-03-12
    • 2011-02-05
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多