23

我正在尝试使用 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,但我无法弄清楚如何获得相反的行为。

4

4 回答 4

22

这有点笨拙,但是您可以使用 maven-dependency-plugin 将所有依赖项复制/解压缩到您的项目中,然后使用程序集插件进行打包。

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>

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

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

</assembly>
于 2009-09-22T11:53:13.077 回答
4

理论上,标签“ignoreNonCompile”和“excludeScope”应该会有所帮助,但要注意它们不一定能正常工作。

使用 maven3 和 maven-dependency-plugin 2.4,一种解决方案是:

<configuration>
<excludeArtifactIds>junit,mockito-all</excludeArtifactIds>
<excludeTransitive>true</excludeTransitive>
</configuration>
于 2012-07-31T20:44:27.053 回答
0

使用最新的 Maven(我在 Maven 3.0 上进行测试),这似乎按预期工作,但有一些警告:

请求的范围(在依赖集中)可能包括基于以下定义的附加范围:http ://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

因此,如果您请求编译范围,您将获得编译和提供。但是,如果您请求运行时范围,您应该获得编译和运行时(但未提供)。

于 2011-02-04T00:06:24.550 回答
0

这是一篇旧文章,但 maven-dependency-plugin 现在有一个“excludeScope”选项,您可以将其设置为“provided”或您需要的任何范围。

http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#excludeScope

例如,

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <excludeScope>provided</excludeScope>
            </configuration>
        </execution>
    </executions>
</plugin>
于 2016-10-21T12:41:53.107 回答