排除“提供的"来自Maven程序集的依赖 [英] Excluding "provided" dependencies from Maven assembly

查看:60
本文介绍了排除“提供的"来自Maven程序集的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Maven程序集插件来构建具有依赖关系的jar, 除外,这些依赖项已提供了作用域.

I am trying to use the Maven assembly plugin to build a jar-with-dependencies, except those that have provided scope.

我已经将jar-with-dependencies复制到了assembly.xml文件中,并在pom中配置了它的用法.这里供参考:

I have copied the jar-with-dependencies into an assembly.xml file and configured its use in my pom. Here it is for reference:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
  <id>injectable-jar</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
    </fileSet>
  </fileSets>
</assembly>

我发现,如果我将范围设置为provided,那么我可以构建一个包含我不想要想要的东西的jar,但是我不知道如何获取逆行为.

I have found out, that if I set the scope to provided, then I can build a jar that contains exactly what I don't want, but I cannot figure out how to get inverse behavior of that.

推荐答案

这有点笨拙,但是您可以使用maven-dependency-plugin将所有依赖项复制/解压缩到项目中,然后使用Assembly插件来做包装.

This is a bit clunky, but you can use the maven-dependency-plugin to copy/unpack all the dependencies into your project, then use the assembly plugin to do the packaging.

copy-dependenciesunpack-dependencies目标均具有可选的 excludeScope 属性,可以设置为忽略provided依赖项.下面的配置将所有依赖项复制到target/lib中,您的程序集插件描述符可以修改为使用文件集以包含这些罐子.

The copy-dependencies and unpack-dependencies goals both have an optional excludeScope property you can set to omit the provided dependencies. The configuration below copies all dependencies into target/lib, your assembly plugin descriptor can be modified to use a fileSet to include those jars.

更新:刚刚对其进行了测试,以确认它可以正常工作.添加了将程序集插件绑定到程序包阶段的配置,并对程序集描述符进行了相应的修改.

Update: Just tested this to confirm it works. Added the configuration for binding the assembly plugin to the package phase, and the relevant modifications to the assembly descriptor.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <excludeScope>provided</excludeScope>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-4</version>
  <executions>
    <execution>
      <id>jar-with-deps</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/my-assembly.xml</descriptor>
    </descriptors>
  </configuration>
</plugin>

my-assembly描述符的fileSet部分如下所示:

The fileSet section of the my-assembly descriptor would look like this:

<assembly>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/lib</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.*</include>
      </includes>
    </fileSet>
  </fileSets>
...

</assembly>

这篇关于排除“提供的"来自Maven程序集的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆