【问题标题】:window/Linux Specific filesystem properties in GrailsGrails 中的 window/Linux 特定文件系统属性
【发布时间】:2010-10-25 14:47:47
【问题描述】:

我想在 Grails 中添加基于 linux 或基于 windows 的系统属性,因为我的应用程序需要在两者中运行。我知道我们可以添加 Config 中指定的 grails.config.locations 位置。时髦。

但我需要 if 和 esle 条件才能选择文件。 问题是 config.grrovy 有 userHome grailsHome appName appVersion 我需要像 osName 这样的东西。 我可以继续使用 syetm.properties,或者如果 soembody 可以告诉我这些(仅)属性如何在 Config.groovy 中可用(通过 DefaultGrailsApplication 或其他方式。那太好了。

此外,如果我需要这些属性,我会以用户定义的弹簧豆的形式提供服务,这会更加优雅。那是正确可行的方法吗?如果是,请举个例子

【问题讨论】:

    标签: grails


    【解决方案1】:

    您可以在 Config.groovy 中执行类似操作:

    environments {
        development {
            if (System.properties["os.name"] == "Linux") {
                grails.config.locations = [ "file:$basedir/grails-app/conf/linux.properties" ]
            } else {
                grails.config.locations = [ "file:$basedir/grails-app/conf/windows.properties" ]
            }
        }
        ...
    }
    

    或者,对于基于服务的方法,您可以将所有操作系统特定的行为捆绑到服务接口的实现中。例如:

    // OsPrinterService.groovy
    interface OsPrinterService {
        void printOs();
    }
    
    // LinuxOsPrinterService.groovy
    class LinuxOsPrinterService implements OsPrinterService {
        void printOs() { println "Linux" }
    }
    
    // WindowsOsPrinterService.groovy
    class WindowsOsPrinterService implements OsPrinterService {
        void printOs() { println "Windows" }
    }
    

    然后像这样在grails-app/conf/spring/resources.groovy 中实例化正确的一个:

    beans = {
        if (System.properties["os.name"] == "Linux") {
            osPrinterService(LinuxOsPrinterService) {}
        } else {
            osPrinterService(WindowsOsPrinterService) {}
        }
    }
    

    然后正确的服务将在 spring 自动注入到您的对象中。

    【讨论】:

    • 谢谢@ataylor,我也是这么想的。加上我没有处理的bean初始化方式。另外,如果我们重写 DefaultGrailsApplication,请阅读一些类似的东西。但是文档有更多关于插件的信息。老实说,我对 GnG 很陌生。别管插件了。想知道是否有一种简单的方法,否则将与 if...n else 一起使用。
    【解决方案2】:

    为 Windows 和 Linux 创建自定义环境。如果放在 config.groovy 中,类似以下的内容应该可以工作

     environments {
     productionWindows {
     filePath=c:\path
     }
     productionLinux {
     filePath=/var/dir
     }
     }
    

    然后,您应该能够使用 grails 配置对象来获取 filePath 的值,而不受 Windows 或 Linux 上的天气影响。有关这方面的更多详细信息,请参阅第 3.2 节 http://www.grails.org/doc/1.0.x/guide/3.%20Configuration.html 如果你想创建一个在 Linux 上运行的 war 文件,你可以执行以下命令。

    grails -Dgrails.env=productionLinux war
    

    然后为您运行的特定环境获取您存储在 config.groovy 中的文件路径。

    def fileToOpen=Conf.config.filePath
    

    fileToOpen 将包含您根据当前运行的环境在 config.groovy 中分配给 filePath 的值,因此当使用 productionLinux 作为环境运行时,它将包含值 /var/dir

    【讨论】:

    • 谢谢@Jared。但我相信 enviromnet{ dev.... 和类似的结构在运行应用程序或战争时在 grails 中作为 env 变量传递。那我将如何通过它。我的意思是我的问题是,如果 grails 为标准 env 做更多的工作并且那些不会被踢,或者如果有更好的方法来设置这个变量(productionWindows et al..)而不是命令行或 env。
    • 请参阅我发送的链接中的“针对不同环境打包和运行”部分。所以像
    • 我在回答中添加了更多信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2015-05-27
    • 2013-04-15
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多