2015-05-05 6 views
5

Ich versuche, ein einfaches Grails 3 Projekt zu erstellen und blieb mit etwas wirklich einfach stecken. Ich möchte, dass meine Datenquelleneigenschaften von VM-Optionen stammen, die ich in meiner IntelliJ-IDE festgelegt habe. Bevor in Grails 2.x, habe ich nur so etwas wie:konvertieren Konfigurationsdatei application.yml zu application.groovy in Grails 3.x

environments { 
    development{ 
     //Database connection properties 
     def dbserver = System.properties.getProperty('dbserver') 
     def dbport = System.properties.getProperty('dbport') 
     ............ 
     dataSource { 
      url: "jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname} 
     } 
    } 

Nun, da ich habe application.yml, wie greife ich auf „System.properties“ und binde sie in yml? Ich habe gelesen, dass wir stattdessen application.groovy verwenden können, wenn YML es nicht unterstützt, in diesem Fall das ist, was die application.groovy wie folgt aussehen:

grails { 
    profile = 'web' 
    codegen { 
     defaultPackage = 'defPack' 
    } 
} 

info { 
    app { 
     name = '@[email protected]' 
     version = '@[email protected]' 
     grailsVersion = '@[email protected]' 
    } 
} 

spring { 
    groovy { 
     template['check-template-location'] = false 
    } 
} 

hibernate { 
    naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy' 
    cache { 
     queries = false 
    } 
} 

grails { 
    mime { 
     disable { 
      accept { 
       header { 
        userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident'] 
       } 
      } 
     } 

     types { 
      all = '*/*' 
      atom = 'application/atom+xml' 
      css = 'text/css' 
      csv = 'text/csv' 
      form = 'application/x-www-form-urlencoded' 
      html = ['text/html', 'application/xhtml+xml'] 
      js = 'text/javascript' 
      json = ['application/json', 'text/json'] 
      multipartForm = 'multipart/form-data' 
      rss = 'application/rss+xml' 
      text = 'text/plain' 
      hal = ['application/hal+json', 'application/hal+xml'] 
      xml = ['text/xml', 'application/xml'] 
     } 
    } 
    urlmapping { 
     cache { 
      maxsize = 1000 
     } 
    } 
    controllers { 
     defaultScope = 'singleton' 
    } 
    converters { 
     encoding = 'UTF-8' 
    } 
    views { 
     default { codec = 'html' } 
     gsp { 
      encoding = 'UTF-8' 
      htmlcodec = 'xml' 
      codecs { 
       expression = 'html' 
       scriptlets = 'html' 
       taglib = 'none' 
       staticparts = 'none' 
      } 
     } 
    } 
} 
dataSource { 
    pooled = true 
    jmxExport = true 
    driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' 
    dbCreate = '' 
    username = 'someUsername' 
    password = 'somePass' 
} 
environments { 
    development { 
     dataSource { 
      url = 'jdbc:sqlserver://localhost:1234;databaseName=someDbName;' 
     } 
    } 
} 

Dank.

UPDATE:

application.groovy ist standardmäßig eingelassenes nicht werden, auch wenn ich application.yml

+0

Für den letzten Teil, können Sie sie zitieren müsste: '= Useragents [ 'Gecko', 'WebKit'] ' –

+0

@IanRoberts, danke ich werde meinen Beitrag aktualisieren. –

Antwort

3

stellte sich heraus, entfernte ich ein Problem hatte, musste ich ‚default‘ Schlüsselwort setzen innerhalb Zitate. Wie:

grails { 
    profile = 'web' 
    codegen { 
     defaultPackage = 'defPack' 
    } 
} 

info { 
    app { 
     name = '@[email protected]' 
     version = '@[email protected]' 
     grailsVersion = '@[email protected]' 
    } 
} 

spring { 
    groovy { 
     template['check-template-location'] = false 
    } 
} 

hibernate { 
    naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy' 
    cache { 
     queries = false 
    } 
} 

grails { 
    mime { 
     disable { 
      accept { 
       header { 
        userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident'] 
       } 
      } 
     } 

     types { 
      all = '*/*' 
      atom = 'application/atom+xml' 
      css = 'text/css' 
      csv = 'text/csv' 
      form = 'application/x-www-form-urlencoded' 
      html = ['text/html', 'application/xhtml+xml'] 
      js = 'text/javascript' 
      json = ['application/json', 'text/json'] 
      multipartForm = 'multipart/form-data' 
      rss = 'application/rss+xml' 
      text = 'text/plain' 
      hal = ['application/hal+json', 'application/hal+xml'] 
      xml = ['text/xml', 'application/xml'] 
     } 
    } 
    urlmapping { 
     cache { 
      maxsize = 1000 
     } 
    } 
    controllers { 
     defaultScope = 'singleton' 
    } 
    converters { 
     encoding = 'UTF-8' 
    } 
    views { 
     'default' { codec = 'html' }//THIS WAS THE SOURCE OF ERROR 
     gsp { 
      encoding = 'UTF-8' 
      htmlcodec = 'xml' 
      codecs { 
       expression = 'html' 
       scriptlets = 'html' 
       taglib = 'none' 
       staticparts = 'none' 
      } 
     } 
    } 
} 

def dbserver = System.properties.getProperty('dbserver') 
def dbport = System.properties.getProperty('dbport') 
def dbusername = System.properties.getProperty('dbusername') 
def dbpassword = System.properties.getProperty('dbpassword') 
def dbname = System.properties.getProperty('dbname') 

dataSource { 
    pooled = true 
    jmxExport = true 
    driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' 
    dbCreate = '' 
    username = dbusername 
    password = dbpassword 
} 
environments { 
    development { 
     dataSource { 
      url = 'jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname}' 
     } 
    } 
} 
+0

Wie haben Sie application.groovy Datei hinzugefügt? das heißt du hast es dir selbst gemacht ..? Wie in deinem Update hast du gesagt, dass es standardmäßig nicht funktioniert !! Und als in der Anwendung.roovy-Datei ist es möglich, auf "System.properties" und andere Java/Groovy-verwandte Objekte zuzugreifen. FYI Ich muss auch meine eigenen groovy Klassenobjekte verwenden [Muss Klassen importieren]. Kannst du bitte auf eine Lösung hinweisen? – artapart

+1

Ja, Sie müssten es manuell hinzufügen und dann Ihre application.yml-Datei loswerden, sonst denke ich, dass die Priorität auf der yml-Version liegt und nicht auf der groovigen Version. Ja, in der groovy-Datei können Sie jede System.properties verwenden, ich habe nicht versucht, Klassen zu importieren, aber Sie können "importieren" versuchen und sehen, was passiert. –

1

Kann keine direkte Antwort auf Ihre Frage sein. Sie können Systemeigenschaften in application.yml zugreifen, indem Sie unter Zugabe von

tasks.withType(org.springframework.boot.gradle.run.BootRunTask) { 
systemProperties = System.properties 

Verweis auf build.gradle: https://github.com/grails/grails-core/issues/9086

Verwandte Themen