mavenmaven-assembly-plugin

Detecting missing included file with Maven assembly plugin


I use Maven assembly plugin to build my project with the following descriptor:

    <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>

Now, if one include is missing (suppose that I don't have any "README*" or "pom.xml" file in the example), I don't have any error.

Is there a way to get an error or a warning during the build if an included file is missing ?


Solution

  • For that, you need to have a maven enforcer plugin.

    This rule checks that the specified list of files exist.

    you need to have something like this.

    <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>
    

    Also see: maven enforcer and requireFilesExist