从Maven程序集中排除"提供的"依赖项

Chr*_*est 23 maven-2 maven-plugin

我试图使用Maven程序集插件来构建一个jar-with-dependencies,除了提供范围的那些.

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

<?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>
Run Code Online (Sandbox Code Playgroud)

我发现,如果我将范围设置为provided,那么我可以构建一个包含我想要的内容的jar ,但我无法弄清楚如何获得反向行为.

Ric*_*ler 19

这是一个有点笨重,但你可以使用maven的依赖,插件复制/所有依赖解压到您的项目,然后用组装插件做包装.

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

更新:刚测试这个以确认它是否有效.添加了将程序集插件绑定到程序包阶段的配置,以及对程序集描述符的相关修改.

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

</assembly>
Run Code Online (Sandbox Code Playgroud)

  • 事实证明,一次执行就足够了.`<includeScope> runtime </ includeScope>`就是我所需要的 - 它隐含地排除了`test`,`provided`和`system`,这是完美的. (14认同)