首页 > 解决方案 > Maven 程序集插件目标“jar-with-dependencies”不包含来自子 pom 的 jar 文件

问题描述

我试图为我的问题找到一个合适的解决方案,但它看起来类似于以下一个maven 程序集,包括在最终 zip/tar 中的当前项目 jar

一个项目的想法是有一个父 pom 和几个子 pom。当我从根级别执行“mvn assembly:single”命令时,我希望每个孩子都有“jar-with-dependencies”。

所以,到目前为止我得到了什么:

  1. 如果我一个一个地执行mvn packagemvn assembly:single那么第一个将成功完成,第二个将警告不包含子项目。由于没有包含我的模块,因此我无法启动目标。
  2. 如果我执行mvn package assembly:single,则将创建具有所有依赖项的所需 jar,并且我能够启动目标。

我担心我错过了其中一个pom.xml. 如果有人可以帮助我,我将不胜感激。我正在使用此示例在 GitHub 存储库上添加一个链接。

顺便说一句,我正在使用maven-assembly-plugin版本3.1.0

提前谢谢你,我想,我需要买一本关于 Maven 的好书。

标签: javamaven

解决方案


关键是禁用父项目的程序集执行:可以通过组合<skipAssembly>选项,profiles节和properties节来实现。

使用以下 pom 文件,它可以正常工作。

家长pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.slemarchand.samples.jarwithdependencies</groupId>
     <artifactId>parent</artifactId>
     <version>1.0-SNAPSHOT</version>
     <packaging>pom</packaging>
     <name>parent</name>
     <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <skip.assembly>true</skip.assembly>
     </properties>
     <modules>
        <module>child1</module>
        <module>child2</module>
     </modules>
     <dependencyManagement>
        <dependencies>
           <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
           </dependency>
        </dependencies>
     </dependencyManagement>
     <build>
        <pluginManagement>
           <plugins>
              <plugin>
                 <artifactId>maven-assembly-plugin</artifactId>
                 <configuration>
                    <skipAssembly>${skip.assembly}</skipAssembly>
                    <descriptorRefs>
                       <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                 </configuration>
              </plugin>
           </plugins>
        </pluginManagement>
     </build>
     <profiles>
        <profile>
           <id>jar</id>
           <activation>
              <file>
                 <exists>${basedir}/src/main/java</exists>
              </file>
           </activation>
           <properties>
              <skip.assembly>false</skip.assembly>
           </properties>
        </profile>
     </profiles>
</project>

孩子 1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.slemarchand.samples.jarwithdependencies</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>child1</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>child1</name>

  <dependencies>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>25.1-jre</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>

  </dependencies>
</project>

孩子 2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.slemarchand.samples.jarwithdependencies</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>child2</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>child2</name>

  <dependencies>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
  </dependencies>

  <build>
  </build>
</project>

您可以在此处找到示例项目:https ://gitlab.com/slemarchand/jar-with-dependencies-multi-module-sample 。


推荐阅读