我使用 maven-enforcer-plugin 成功创建了一个 evaluateBeanshell 规则,它扫描工作区中的文件以查找常见错误。
使用硬编码路径,该规则可以正常工作。当我想使用周围 pom 中的 ${project.basedir} 变量时,脚本会在我的 Windows 机器上中断。
<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>
在调试输出中,该行:
print("${project.basedir}");
被替换为:
print("D:\code\my-maven-project");
是否有另一个带有经过消毒的斜线的 Maven 属性,或者是否有另一种访问 ${project.basedir} 的方法?代码示例中概述的 hack 是可行的,但我不喜欢强迫我发表评论的 hack。