1

I successfully created a evaluateBeanshell rule with the maven-enforcer-plugin that scans files in the workspace for common mistakes.

With a hardcoded path the rule works fine. When I want to use the ${project.basedir} variable from the surrounding pom, the script breaks on my Windows machine.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version>
<executions>
    <execution>
        <id>enforce-banned-dependencies</id>
        <inherited>true</inherited>
        <goals>
            <goal>enforce</goal>
        </goals>
        <configuration>
            <rules>
                <evaluateBeanshell>
                    <condition>
                        import scanner.MyScanner;
                        scanner = new MyScanner();

                        //hack to read root dir
                        //project.basedir  crashes beanshell with its backslashes
                        rootPath = new File("");
                        root = new File(rootPath.getAbsolutePath());
                        print("Scanning in: " + root);
                        print("${project.artifactId}"); // works fine
                        print("${project.basedir}");    // breaks the code
                        
                        scanner.loopThroughProjects(root);

                        return everythingIsFine;
                    </condition>
                </evaluateBeanshell>
            </rules>
            <fail>true</fail>
        </configuration>
    </execution>
</executions>                           

In the debug output the line:

print("${project.basedir}");

was replaced by:

print("D:\code\my-maven-project");

Is there another maven property with sanitized slashes or is there another way to access ${project.basedir}? The hack outlined in the code example kind of works, but I don't like hacks that force me to leave comments.

Ansgar
  • 1,747
  • 4
  • 17
  • 27
  • I mean this is a bug in the maven-enforcer-plugin, as it does not handle backslashes from such properties. Trying to replace backslashes in the pom has no chance, you will always get: "Couldn't evaluate condition". Unfortunately your work-around works only for the local pom file. I'm still curious how a solution looks like too. – thoku Dec 29 '20 at 22:55
  • @thoku I don't think you can blame the plugin; maven resolves those specific properties before sending the configuration off to the actual plugin code. This would require the plugin to parse the beanshell and fix potential mistakes, which is not really it's responsibility. I did add an alternative to this problem as an answer to this question. – slindenau May 19 '22 at 14:33

1 Answers1

0

You could try ${project.baseUri}.

See https://maven.apache.org/ref/3.8.5/maven-model-builder/#Model_Interpolation

On my Windows 10 machine with Java 8 and Maven 3 the following test properties in pom.xml:

<test>${project.baseUri}</test>
<test2>${project.basedir}</test2>

become the following in the 'effective-pom' (via Intellij IDEA maven plugin)

<test>file:/D:/test/path/</test>
<test2>D:\test\path</test2>

This is just as a proof of concept to show the path separators change, and become valid as a Java String.

You could then transform the URI to a file for your needs in the beanshell script as follows:

uri = java.net.URI.create("${project.baseUri}");
root = new java.io.File(uri);

Via https://docs.oracle.com/javase/8/docs/api/java/io/File.html#File-java.net.URI-

slindenau
  • 1,091
  • 2
  • 11
  • 18