maven-2 - 从 Maven 程序集中排除 "provided"依赖项

标签 maven-2 maven-plugin

我正在尝试使用 Maven 程序集插件来构建一个带有依赖项的 jar,但那些提供范围的除外。

我已经将 jar-with-dependencies 复制到了一个 assembly.xml 文件中,并在我的 pom.xml 中配置了它的使用。这里供引用:

<?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,但我无法弄清楚如何获得相反的行为。

最佳答案

这有点笨拙,但是您可以使用 maven-dependency-plugin 将所有依赖项复制/解压缩到您的项目中,然后使用程序集插件进行打包。
copy-dependenciesunpack-dependencies目标都有一个可选的 excludeScope您可以设置属性以省略 provided依赖关系。下面的配置将所有依赖项复制到 target/lib,您的程序集插件描述符可以修改为使用 fileSet包括那些 jar 。

更新:刚刚对此进行了测试以确认它有效。增加了将程序集插件绑定(bind)到打包阶段的配置,以及对程序集描述符的相关修改。

<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 的文件集部分描述符看起来像这样:
<assembly>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/lib</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.*</include>
      </includes>
    </fileSet>
  </fileSets>
...

</assembly>

关于maven-2 - 从 Maven 程序集中排除 "provided"依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1459021/

相关文章:

maven-2 - Maven 配置文件和安装

Maven 插件 java.library.path

maven-2 - 属性-maven-插件 : Error loading properties-file

android - android-maven-plugin 的问题

java - mvn eclipse :eclipse within Eclipse

maven-2 - 使用 Maven 的外部配置文件

java - mvn混淆器: Is there possible obfuscate dependency before copying

maven - 如何在自定义Maven插件中使用${maven.build.timestamp}?

java - 如何将开发人员列表添加到 Maven 生成的站点?

java - 多模块 Maven 项目中的 log4j 配置文件