简体   繁体   中英

PlayFramework2 Scala File Map

I'm just starting with Scala and have run into a problem that has me stumped, but I'm guessing that I'm missing something easy.

I was following instructions to use the Clapper ClassFinder: http://thoughts.inphina.com/2011/09/15/building-a-plugin-based-architecture-in-scala/

val classpath = List("./plugins").map(new File(_))
val finder = ClassFinder(classpath)
val classes = finder.getClasses
val classMap = ClassFinder.classInfoMap(classes)

After executing the first line, I see that classpath is set simply to List(.\\plugins) I'm running this on windows, so the swapping of the slash seems to be OK. But I expected to see a list of File objects, although I am not sure about this Scala syntax, and perhaps I'm missing something in the Scala IDE. The value for classes shows an "empty iterator".

It seems not to be finding any files in the path that I specified. I tried using an absolute path, but I had the same results. I have a single jar file in the plugins directory that I'm hoping it will find. The plugins directory is at the root of the Play2 project I'm using.

Edit --- I did find that when I explicitly list the path to one jar that it is able to find it:

val classpath = List("./plugins/myPlugin.jar").map(new File(_))

But I want to find all jar files in the directory. The following didn't work:

val classpath = List("./plugins/*").map(new File(_))

Nor did this:

val classpath = List("./plugins/*.jar").map(new File(_))

如果你的目标是列出所有jar文件,你可以使用以下代码:

val classpath = List("./plugins").map(path => Option(new File(path).listFiles).getOrElse(Array.empty[java.io.File]) filter(file => file.isFile && file.getName.endsWith(".jar"))).flatten

Judging by this issue on the ClassFinder repo on Github it may be a bug.

I think you need to create an explicit list of jar files or to list the ones contained in your folder like:

val classpath =(new File("./plugins")).listFiles.filter(_.getName.endsWith(".jar"))

EDIT: from a cursory glance at ClassFinder's source on GitHub I think it's not a bug. ClassFinder searches for .class files either in jars or in zip files or directly in folders but it looks like it does not mix these things recursively (ie if you give it a folder it will look for classes directly in the folder but it won't look for classes in jars in the folder)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM