请教个 maven-dependency-plugin 的 excludeScope 问题

本文详细介绍了如何使用Maven的dependency-plugin插件来精确地管理不同范围的依赖,包括compile和system范围的依赖复制。通过配置两个execution,分别处理runtime和system范围的依赖,实现了对特定依赖的有效管理和排除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:《请教个 maven-dependency-plugin 的 excludeScope 问题》

我需求是 copy(copy-dependencies) compile 和 system 范围的依赖。

我想当然的以为这样配置能行

<includeScope>compile</includeScope>
<excludeScope>provided</excludeScope>

但事实上 include 不能和 exclude 同时工作,所以只能配置这一个<excludeScope>provided</excludeScope>然后 excludeArtifactIds 排除掉 junit 等

话说有没有其它简洁的方法

PS:includeScope 的配置说明

runtime scope gives runtime and compile dependencies,
compile scope gives compile, provided, and system dependencies,
test (default) scope gives all dependencies,
provided scope just gives provided dependencies,
system scope just gives system dependencies.

解决方案:

配置两个就行了

<executions>
    <execution>
        <id>copy-dependencies-runtime</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <!--http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#includeScope-->
            <includeScope>runtime</includeScope>
            <!--依赖传递-->
            <excludeTransitive>false</excludeTransitive>
        </configuration>
    </execution>
    <execution>
        <id>copy-dependencies-system</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <includeScope>system</includeScope>
            <!--过滤依赖传递-->
            <excludeTransitive>true</excludeTransitive>
        </configuration>
    </execution>
    <execution>
        <id>install</id>
        <phase>install</phase>
        <goals>
            <goal>sources</goal>
        </goals>
    </execution>
</executions>

 

### 如何在 Maven 构建中使用 `maven-dependency-plugin` 排除某些依赖项 在 Maven 中,`maven-dependency-plugin` 提供了一系列目标来帮助开发者管理和操作项目的依赖项。如果希望在构建过程中排除某些特定的依赖项,则可以通过配置 `<excludes>` 或者调整 POM 文件中的依赖范围等方式实现。 #### 配置方式 为了在打包阶段(package phase)排除某些依赖项,可以在 `maven-dependency-plugin` 的配置部分定义具体的参数。例如: 1. **通过插件的目标 `dependency:copy` 和 `dependency:unpack` 进行过滤** 可以利用 `include` 和 `exclude` 参数,在执行这些目标时筛选出不需要的依赖项[^5]。 2. **POM 文件中的依赖声明** 如果某个依赖项在整个项目生命周期都不需要参与构建过程,可以直接将其设置为 `provided` 范围或完全移除它。这种方式适用于那些仅在运行时由容器或其他环境提供的库[^3]。 3. **示例配置** 下面是一个完整的例子,展示如何在 `maven-dependency-plugin` 使用期间排除不必要的依赖项: ```xml <project> ... <build> <plugins> <!-- maven-dependency-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <includes>junit:junit</includes> <!-- 明确包含哪些依赖 --> <excludes>commons-logging:*</excludes> <!-- 明确排除哪些依赖 --> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project> ``` 上述 XML 片段展示了如何配置 `maven-dependency-plugin` 来控制依赖项的复制行为。其中,`<excludes>` 标签指定了要忽略掉的依赖项模式;而 `<includes>` 则限定了允许被处理的具体依赖列表。 另外需要注意的是,当涉及到复杂的多模块项目时,可能还需要额外关注父 POM 对子模块的影响以及各模块间的相互作用情况。 最后提醒一点,虽然这里讨论的重点是如何借助 `maven-dependency-plugin` 实现排除功能,但在实际应用当中也应考虑其他替代方案——比如合理规划依赖树结构、采用更精确的作用域设定等方法同样能够有效减少无用组件带来的干扰[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值