Ngrinder 源码之Maven 项目
Ngrinder支持Maven结构的测试脚本。使用ScriptHandlerFactory来个脚本选择处理器handler,目前有JythonScriptHandler, GroovyScriptHandler和GroovyMavenProjectHandler三种。 ScriptHandlerFactory初始化的时候会按各个handler的order和displayOrder排序(升序)。
public void init() {
// Sort by the order of scriptHandlers..
Collections.sort(scriptHandlers, new Comparator<ScriptHandler>() {
@Override
public int compare(ScriptHandler o1, ScriptHandler o2) {
return o1.order().compareTo(o2.order());
}
});
// Sort by the order of scriptHandlers..
visibleHandlers = newArrayList();
for (ScriptHandler each : this.scriptHandlers) {
if (!(each instanceof NullScriptHandler)) {
visibleHandlers.add(each);
}
}
Collections.sort(visibleHandlers, new Comparator<ScriptHandler>() {
@Override
public int compare(ScriptHandler o1, ScriptHandler o2) {
return o1.displayOrder().compareTo(o2.displayOrder());
}
});
}
然后选择handler的时候按order升序,找到第一个canHandler为true的处理器返回。
public ScriptHandler getHandler(FileEntry fileEntry) {
for (ScriptHandler handler : scriptHandlers) {
if (handler.canHandle(fileEntry)) {
return handler;
}
}
// Actually nothing is reach here.
throw processException("no matching handler for " + fileEntry.getPath());
}
MavenScriptProjectHandler 判断canhandle就是看根目录下有没有.pom文件
return getFileEntryRepository().hasOne(fileEntry.getCreatedUser(), getBasePath(path) + "/pom.xml");
Maven项目运行时,maven里面依赖的包会自动拷贝到./lib文件夹下
@Override
protected void prepareDistMore(Long testId, User user, FileEntry script, File distDir,
PropertiesWrapper properties, ProcessingResultPrintStream processingResult) {
String pomPathInSVN = PathUtils.join(getBasePath(script), "pom.xml");
MavenCli cli = new MavenCli();
processingResult.println("\nCopy dependencies by running 'mvn dependency:copy-dependencies"
+ " -DoutputDirectory=./lib -DexcludeScope=provided'");
int result = cli.doMain(new String[]{ // goal specification
"dependency:copy-dependencies", // run dependency goal
"-DoutputDirectory=./lib", // to the lib folder
"-DexcludeScope=provided" // but exclude the provided
// library
}, distDir.getAbsolutePath(), processingResult, processingResult);
boolean success = (result == 0);
if (success) {
processingResult.printf("\nDependencies in %s was copied.\n", pomPathInSVN);
LOGGER.info("Dependencies in {} is copied into {}/lib folder", pomPathInSVN, distDir.getAbsolutePath());
} else {
processingResult.printf("\nDependencies copy in %s is failed.\n", pomPathInSVN);
LOGGER.info("Dependencies copy in {} is failed.", pomPathInSVN);
}
// Then it's not necessary to include pom.xml anymore.
FileUtils.deleteQuietly(new File(distDir, "pom.xml"));
processingResult.setSuccess(result == 0);
}
这里有两个注意的地方。
- Ngrinder使用MavenCli来运行maven命令,所以跟本机的maven环境不一样,如果有特殊的maven setting要配置的话要手动给maven指定setting.xml的位置。
public static final File DEFAULT_GLOBAL_SETTINGS_FILE =
new File( System.getProperty( "maven.home", System.getProperty( "user.dir", "" ) ), "conf/settings.xml" );
ngrinder通过获取环境变量maven.home来查找setting.xml, 可以启动的时候加参数-Dmaven.home=xxxxx来指定maven 的配置。
- Ngrinder会把pom文件里面的provided scope的依赖包自动过滤掉,所以对于一些会导致包冲突的artifact可以用scope来exclude掉。