1

I can use the evaluateBeanshell rule to enforce some convention: no colon's in directories below src.

<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>1.4.1</version>
  <executions>
    <execution>
      <id>enforce-beanshell</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <evaluateBeanshell>
            <condition>org.codehaus.plexus.util.FileUtils.getDirectoryNames(new File("src"), "**/*:*", null, false).isEmpty()</condition>
          </evaluateBeanshell>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

But some projects don't have an src directory, and the rule will fail hard. I tried setting

<condition>org.codehaus.plexus.util.FileUtils.getDirectoryNames(new File("."), "src/**/[^A-Z]*", null, false).isEmpty()</condition>

How do I safely catch the non-existence of the src directory?

caduceus
  • 1,542
  • 2
  • 16
  • 21
  • 1
    please check this [one](https://maven.apache.org/enforcer/enforcer-rules/requireFilesExist.html) and [old answer](https://stackoverflow.com/questions/36220448/is-there-a-way-in-maven-to-assure-that-a-property-is-set) – gowridev Dec 15 '20 at 13:46
  • @gowridev how functional is the regex inside one of those tags? I don't want to enforce the existence of the src directory – caduceus Dec 15 '20 at 13:47
  • please check these two links [maven rule api](https://maven.apache.org/enforcer/enforcer-rules/index.html) and [custom api](http://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html) – gowridev Dec 15 '20 at 14:29
  • requireFilesExist is probably the best answer – antonyh Dec 15 '20 at 17:05
  • You should rewrite the question / title to make it clearer that you want to check for the existence of a folder: it has little to do with the characters in the file name. – antonyh Dec 15 '20 at 17:18
  • @antonyh but i don't want to check for the existence of a folder. I want to quietly handle any exception raised when `new File("src")` fails – caduceus Dec 15 '20 at 22:52
  • Ooo this is a tricky puzzle. I’ll see if I can find a clean solution when I get some free time – antonyh Dec 17 '20 at 00:48

2 Answers2

1

This does what I need

<evaluateBeanshell>
  <condition>
    String projectSource = "${project.basedir}" + "/src";
    if (org.codehaus.plexus.util.FileUtils.fileExists(projectSource)){
      List filenames = org.codehaus.plexus.util.FileUtils.getFileNames(
        new File(projectSource),"**/*.*",null,false);

      for (Iterator it = filenames.iterator(); it.hasNext();) {
        String file = it.next();
        String extension = org.codehaus.plexus.util.FileUtils.getExtension(file);

        if (extension.equals(extension.toLowerCase())) {
          it.remove();
        } else {
          print("Invalid filename extension (no capitals): " + projectSource + "/" + file);
        };
      };
      return filenames.isEmpty();
    } else return true;
  </condition>
</evaluateBeanshell>
caduceus
  • 1,542
  • 2
  • 16
  • 21
0

If you want to use Beanshell, you just need something that returns a boolean.

<evaluateBeanshell>
   <condition>new java.io.File("src").exists()</condition>
</evaluateBeanshell>

This will be true if the path exists as a file/folder/symlink, and false otherwise which will break the build.

If you want to check multiple aspects, such as if it exists AND if it's a directory, add multiple conditions:

<evaluateBeanshell>
   <condition>new java.io.File("path").exists()</condition>
   <condition>new java.io.File("path").isDirectory()</condition>
</evaluateBeanshell>

(Arguably there's little point in checking if it exists then if it's a directory, but helps to illustrate the point)

There's plenty more that can be done with Beanshell, if you have to use it. Ultimately though, the 'requireFilesExist' rule is probably a better configuration for the enforcer.

antonyh
  • 2,131
  • 2
  • 21
  • 42
  • You have not understood my question. The rule I'm trying to enforce is this: if a file exists below a folder src (which may or may not exist) then it must not have uppercase in it's filename extension. I don't want the build to break when src does not exist – caduceus Dec 15 '20 at 22:59
  • Ok. But the capability of Enforcer is still the same. Write a utility class that returns Boolean, package as a jar, then use it as the condition. You can put whatever logic you need in there. Or you may be able to put more complex Beanshell in the condition, or chain with ‘&&’ / ‘||’ but I’ve not tried this. – antonyh Dec 17 '20 at 00:45