【问题标题】:how to tell maven assembly on a submodule to include only dependencies this module needs?如何告诉子模块上的 Maven 程序集仅包含该模块所需的依赖项?
【发布时间】:2013-01-22 12:27:51
【问题描述】:

我正在使用这些模块开发一个多模块 maven 项目

parent
  |-- restful    // this is a jersey restful service as a WAR
  |-- shared     // some stuff shared by all other modules as a jar
  |-- cl-client  // a commandline client to the restful service, needs assembly

父pom.xml使用dependencyManagement列出所有模块使用的所有依赖

cl-client 的 pom 包含组装插件,配置为在打包阶段执行:

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.4</version>
      <executions>
        <execution>
          <id>distro-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptors>
              <descriptor>src/main/assembly/assembly.xml</descriptor>
            </descriptors>
          </configuration>
        </execution>
      </executions>
    </plugin>

而assembly.xml如下:

<assembly>
  <id>commandlinable</id>
  <formats>
    <format>dir</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <includes>
        <include>my:cl-client</include>
      </includes>
      <binaries>
        <outputDirectory>${artifactId}</outputDirectory>
        <unpack>true</unpack>
        <dependencySets>
          <dependencySet>
            <outputDirectory>${artifactId}/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>false</unpack>
            <scope>runtime</scope>
          </dependencySet>
        </dependencySets>
      </binaries>
    </moduleSet>
  </moduleSets>
</assembly>

当我运行mvn package 时,cl-client 模块组装得很好,并且确实创建了我希望的目录,但唯一的问题是所有依赖项 jar 都复制到 lib/ 目录中,即使是那些只是由“restful”模块使用,例如与球衣相关的罐子。我试图翻转useProjectArtifact 标志,但似乎没有任何区别。

所以我只是想知道是否可以告诉 Maven 程序集仅包含 cl-client 模块所需的 jar。我在多模块项目的 Maven 组装上翻了很多在线资源,但没有得到任何明确的答案。

【问题讨论】:

  • cl-client项目是否有restful项目作为依赖?如果是这样,大概在 cl-client 项目上执行 mvn dependency:tree 会显示您在程序集输出中看到的所有 JAR?
  • cl-client pom 是否继承自父 pom?
  • @Andrew 是的,cl-client pom 继承自父 pom
  • @Duncan 非常感谢您的建议。我对 maven 还是很陌生,不知道 dependency:tree 命令。这非常有用。看来 hbase jar 对 jersey 有依赖,所以才包含在内。
  • @Duncan:请您(或吉姆)在解决方案中添加对此问题的答案。然后请 Jim 选择此 anweser 为正确的。然后它将不再被标记为“未答复”-谢谢!

标签: maven dependencies maven-assembly-plugin multi-module


【解决方案1】:

使用mvn dependency:tree 命令了解您的模块之间存在哪些依赖关系。您可能会发现工件对球衣具有传递依赖关系,这就是它被包含在内的原因。

【讨论】:

  • +1:快速(当然,除了是一个很好的答案之外 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 2020-10-23
相关资源
最近更新 更多