首页 > 解决方案 > Maven 程序集插件 - 忽略“存档不能为空”

问题描述

我有组装插件的问题。我必须使用一个程序集定义来为更多模块创建 JAR 文件。所以项目结构看起来像这样

module/
----module1/
-------- pom.xml
----module2/
-------- pom.xml
----moduleX/
-------- pom.xml
assembly_jar.xml
pom.xml (this is parent)

父 pom 文件如下所示:

<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.my.project</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                  <execution>
                    <id>make-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <attach>false</attach>
                        <descriptors>
                            <descriptor>${source-systems.basedir}/assembly_jar.xml</descriptor>
                        </descriptors>
                        <finalName>${project.artifactId}-${project.version}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                </execution>
            </plugin>
        </plugins>
    </build>
</project>

assembly_jar.xml 如下所示:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>jar-with-some-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <scope>runtime</scope>
      <unpack>true</unpack>
    </dependencySet>
  </dependencySets>
</assembly>

模块 1 的 pom 文件如下所示:

<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.my.project</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <packaging>pom</packaging>
  <groupId>com.my.project</groupId>
  <artifactId>module1</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>oracle_rds</name>

  <dependencies>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.4</version>
    </dependency>
  </dependencies>

</project>

当我为 module1 运行mvn clean install时,一切正常并创建了 jar 文件。但是我有 module2 不需要任何特定的依赖项。所以 pom 文件看起来像这样:

<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.my.project</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <packaging>pom</packaging>
  <groupId>com.my.project</groupId>
  <artifactId>module2</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>oracle_rds</name>

</project>

当我运行mvn clean install我有错误 创建程序集存档 jar-with-some-dependencies: archive cannot be empty这是因为没有依赖项可以打包到 jar 文件中。

如果模块中没有依赖项,有什么方法可以忽略此错误并且不创建 jar 文件?

由于重复性很大,我不想将程序集定义移动到模块中(我将拥有 100 多个模块,其中大多数会有一些特殊的依赖项)谢谢您的帮助。

标签: mavenjar

解决方案


推荐阅读