धारा 24.3 बाहरी .groovy कॉन्फ़िग फ़ाइल का उपयोग कैसे करें Externalized Configuration में इंगित करता है एक .properties
या .yml
फ़ाइल बाहरी config के लिए इस्तेमाल किया जा सकता है, लेकिन मैं सिर्फ अपनी application.groovy
फ़ाइल की तरह मेरी बाहरी config एक .groovy
फ़ाइल चाहते हैं होने के लिए जो मैं पहले ही .yml
से परिवर्तित हो चुका है। मैं यह कैसे कर सकता हूं?Grails 3 में
Grails संस्करण 3.2.0.M2
अद्यतन:
मैं इस @Michal_Szulc
नोट द्वारा प्रदान की उत्तर के आधार पर काम कर रहा है कि ConfigSlurper
जरूरत वर्तमान वातावरण प्राप्त करने में सक्षम था सही ढंग से काम करने के लिए। यह भी ध्यान रखें कि इन परिवर्तनों को my_grails_app/grails-app/init/my_grails_app/Application.groovy
फ़ाइल में बनाया जाना है, my_grails_app/grails-app/conf/application.groovy
फ़ाइल नहीं है, जो आपके पास .yml
कॉन्फ़िगरेशन से .groovy
कॉन्फ़िगरेशन में कनवर्ट किया गया हो।
package my_grails_app
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.context.EnvironmentAware
import org.springframework.core.env.Environment
import org.springframework.core.env.MapPropertySource
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
@Override
void setEnvironment(Environment environment) {
def appName = grails.util.Metadata.current.getApplicationName()
// The value of this environment variable should be the absolute path to an external .groovy config file like /opt/my_grails_app/my_grails_app-config.groovy.
def envVarName = "MY_GRAILS_APP_CONFIG"
// If you don't want to use an environment variable you can specify your external .groovy config file here, but the environment variable will still be checked first.
def dfltAppConfigFileName = "/opt/${ appName }/${ appName }-config.groovy"
def appConfigFile = null
def loadDfltConfig = false
// Try to load config specified by the environment variable first
println "Checking the environment variable ${ envVarName } for the configuration file location..."
def envVarVal = System.getenv(envVarName) ?: System.getProperty(envVarName)
if(envVarVal) {
appConfigFile = new File(envVarVal)
if(!appConfigFile.exists()) {
println "The configuration file ${ appConfigFile } specified by the environment variable ${ envVarName } does not exist. Checking for the default configuration file ${ dfltAppConfigFileName } instead..."
appConfigFile = null
loadDfltConfig = true
}
} else {
println "The environment variable ${ envVarName } which specifies the configuration file to be loaded does not exist. Checking for the default configuration file ${ dfltAppConfigFileName } instead..."
appConfigFile = null
loadDfltConfig = true
}
// Try loading the default config file since we couldn't find one specified by the environment variable
if(loadDfltConfig) {
appConfigFile = new File(dfltAppConfigFileName)
if(!appConfigFile.exists()) {
println "The default configuration file ${ dfltAppConfigFileName } does not exist."
appConfigFile = null
}
}
// Load the config file if it exists, otherwise exit
if(appConfigFile) {
println "Loading configuration file ${ appConfigFile }"
def config = new ConfigSlurper(environment.activeProfiles.first()).parse(appConfigFile.toURI().toURL())
environment.propertySources.addFirst(new MapPropertySource("${ appName }-config", config))
} else {
println "No configuration file found. Exiting."
System.exit(1)
}
धन्यवाद कोशिश कर सकते हैं हो सकता है, इस सही रास्ते पर मुझे मिला है। मुझे कुछ बदलाव करना पड़ा जो मैंने अपने प्रश्न के अपडेट में नोट किया था। – ubiquibacon
धन्यवाद Michal_Szulc: इससे मुझे बाहरी Application.groovy फ़ाइल पढ़ने में मदद मिलती है लेकिन अगर बाहरी कॉन्फ़िगरेशन application.yml फ़ाइल में मौजूद होते हैं तो बाहरी फ़ाइल कॉन्फ़िगरेशन लागू नहीं होते हैं। क्या आप कृपया इस पर मेरी मदद कर सकते हैं। –
मुझे लगता है कि यह उद्देश्य पर बनाया गया है - imho application.yml को अन्य कॉन्फ़िगरेशन फ़ाइलों पर पसंद किया जाना चाहिए। –