【问题标题】:Accessing resources in Private Grails Plugin web-app访问 Private Grails Plugin web-app 中的资源
【发布时间】:2014-03-17 19:56:22
【问题描述】:

我有一个使用私有 Grails 插件 mySearch 的主 grails 应用程序。 插件 Search 在 Search\web-app\filters.json 中有一个文件。

使用 run-app 运行应用程序时:可以使用以下命令访问资源文件:

def ressource = new File( org.codehaus.groovy.grails.plugins.GrailsPluginUtils.getPluginDirForName('mySearch')?.file?.absolutePath
+"/web-app/filters.json").text

但是应用部署在tomcat7时就不行了。

我尝试过使用pluginManager.getGrailsPlugin('mySearch')

但我无法访问资源的绝对路径。

【问题讨论】:

  • 尝试添加以下内容,前提是您已设置系统属性:(grails.util.Environment.warDeployed ? System.getProperty('catalina.home') + '/webapps/myAppName/WEB-INF / : '')
  • 战争不爆发就不行
  • 很高兴知道。感谢您指出这一点。
  • 你好,System.getProperty('catalina.home') 给了我 catalina home 的路径,而不是 webapps 根目录的路径

标签: file grails plugins private


【解决方案1】:

经过多次尝试解决它,我找到了这个解决方法。 它看起来很乱,但我没有发现其他更短更甜的东西:

// work only for a plugin installed in app deployed from a war.
String getRessourceFile(String relativePath) throws FileNotFoundException{ 

    def pluginDir = pluginManager.getGrailsPlugin('MyPlugin')?.getPluginPath()
    def pluginFileSystemName = pluginManager.getGrailsPlugin('MyPlugin')?.getFileSystemName()

    def basedir = grailsApplication.parentContext.getResource("/")?.file
    if(basedir.toString().endsWith("web-app")){
        basedir = basedir.parent
    }

    ressourcefile = "$basedir$pluginDir/$relativePath"
    ressource = new File(ressourcefile)?.text
}

【讨论】:

    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2012-05-11
    • 1970-01-01
    • 2017-07-19
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多