使用 Maven 程序集插件检测丢失的包含文件

Detecting missing included file with Maven assembly plugin

我使用 Maven 程序集插件通过以下描述符构建我的项目:

    <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>src</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <includes>
        <include>README*</include>
        <include>LICENSE*</include>
        <include>NOTICE*</include>
        <include>pom.xml</include>
      </includes>
      <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
    <fileSet>
      <directory>${project.basedir}/src</directory>
      <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
  </fileSets>
</assembly>

现在,如果缺少一个包含(假设我在示例中没有任何 "README*" 或 "pom.xml" 文件),我没有任何错误。

如果包含的文件丢失,是否有办法在构建期间收到错误或警告?

为此,您需要有一个 maven enforcer 插件。

This rule checks that the specified list of files exist.

你需要这样的东西。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>3.0.0-M2</version>
  <executions>
    <execution>
      <id>enforce-license</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireFilesExist>
            <files>
              <file>${project.basedir}/README*</file>
              <file>${project.basedir}${project.basedir}/LICENSE*</file>
              <file>${project.basedir}${project.basedir}/NOTICE*</file>
             <file>${project.basedir}${project.basedir}/pom.xml</file>
            </files>
          </requireFilesExist>
        </rules>
       <fail>true</fail>
      </configuration>
    </execution>
  </executions>
</plugin>

另请参阅:maven enforcerrequireFilesExist