2012-01-01 66 views
5

我有一個sbt(0.11.2)插件需要獲取插件內部文本文件的路徑。我怎麼做? baseDirectory,sourceDirectories等設置爲包含插件的項目的基礎,而不是插件本身的基礎。sbt插件如何獲取插件中文件的路徑?

我想提供一個命令給插件用戶,從插件裏面的ruby文件中提取默認值,然後允許插件用戶覆蓋這些默認值。

回答

1

爲什麼不使用好的舊Java的Class.getResource或Class.getResourceAsStream方法?例如。像這樣:

object TestPlugin extends Plugin { 

    override def settings = super.settings ++ Seq(
    commands += testCommand 
) 

    def testCommand = Command.command("test")(action) 

    def action(state: State) = { 
    try { 
     val in = getClass.getResourceAsStream("/test.txt") 
     val text = Source.fromInputStream(in).getLines mkString System.getProperty("line.separator") 
     logger(state).info(text) 
     in.close() 
     state 
    } catch { 
     case e: Exception => 
     logger(state).error(e.getMessage) 
     state.fail 
    } 
    } 
} 
+0

我不認爲這有幫助。我確實需要一個到插件根目錄的路徑,無論它是一個jar還是一個目錄。第一個文件中的很多內容都會包含許多對其他相對路徑的引用(它是ruby),因此擁有一個流是沒有用的,而將流拷出也是沒有用的。 – 2012-01-02 16:48:27

+1

路徑究竟是什麼?不會getClass.getResource幫助?這返回一個URL,例如文件:/富/酒吧。 – 2012-01-12 15:29:12

+0

是的,你是對的。我誤解了文檔,並認爲我唯一能做的就是獲得一個流。我結束了使用c.getProtectionDomain.getCodeSource.getLocation.getPath。 – 2012-01-19 02:15:49

相關問題