Grails(4)Guide Book Chapter 4 Configuration and Command Line

Grails(4)Guide Book Chapter 4 Configuration and Command Line

4.7.2 Dependency Repositories
Remote Repositories
Initially your BuildConfig.groovy does not use any remote public Maven repositories. There is a default grailsHome() repository that will locate the JAR files Grails needs from my Grails installation. To configure a public repository, specify it in repositories block:

repositories {
     mavenCentral()     //maven center
     ebr()                    //spring source enterprise bundle repository
}

we can also specify a specific URL
repositories{
     mavenRepo name: "Codehaus", root: "http://repository.codehaus.org"
}

Controlling Repositories Inherited from Plugins
There is a flag for inherit

repositories{
     inherit false
}

Offline Mode
There are times when it is not desirable to connect to any remote repositories(whilst working on the train for example)
>grails --offline run-app

Or do that in BuildConfig.groovy file:
grails.offline.mode=true

Local Resolvers
repositories {
     flatDir name: 'myRepo', dirs: '/path/to/repo'
}

To specify my local Maven cache (~/.m2/repository) as a repository:
repositories {
     mavenLocal()
}

Custom Resolvers
org.apache.ivy.plugins.resolver.URLResolver()
org.apache.ivy.plugins.resolver.SshResolver

Authentication
This can be placed in USER_HOME/.grails/settings.groovy file
grails.project.ivy.authentication = {
     credentials {
          realm = ".."
          host = "localhost"
          username = "user"
          password = "111111"
     }
}

4.7.3 Debugging Resolution
4.7.5 Providing Default Dependencies
Sometimes, the jar files will be provided by the container.

grails.project.dependency.resolution = {
     defaultDependenciesProvided true // all of the default dependencies will
                                                       // be 'provided' by the container
     …snip...
}

4.7.7 Dependency Reports
4.7.9 Maven Integration
grails.project.dependency.resolution = {
     pom true
     ...
}
The line pom true tells Grails to parse Maven's pom.xml and load dependencies from there.

4.7.10 Deploying to a Maven Repository
>grails maven-install
>grails maven-deploy

4.7.11 Plugin Dependencies
grails.project.dependency.resolution = {
     …    
     repositories {
     }
     plugins {
          runtime ':hibernate:1.2.1'
     }
     dependencies{
     }
}

Plugin Exclusions
plugins {
     runtime(':weceem:0.8'){
          excludes "searchable" //excludes most recent version
     }
     runtime ':searchable:0.5.4' //specifies a fixed searchable version
}

5. The Command Line
Grails' command line system is built on Gant - a simple Groovy wrapper around Apache ant.

Grails searches in the following directories for Gant scripts to execute:
USER_HOME/.gails/scripts
PROJECT_HOME/scripts
PROJECT_HOME/plugins/*/scripts
GRAILS_HOME/scripts

Grails will also convert command names that are in lower case.
>grails run-app
This command will look for RunApp.groovy.

From my understanding, seldom, we need some configuration.
>export GRAILS_OPTS="-Xmx1G -Xms256m -XX:MaxPermSize=256m"
>grails run-app

5.1 Interactive Mode
>grails
Enter interactive mode, then we can get a lot of help when we type tab button.
grails>

If we want to run an external process whilst interactive mode is running. We can do so by starting the command with !
grails>!ls
that will list all the file under this directory.

!ls !pwd some commands are working, for example, !cd is not working.

5.2 Forked Tomcat Execution
Grails 2.2 and above

5.3 Creating Gant Scripts
We can create our own Gant scripts by running the create-script command.
>grails create-script compile-sources

That will create a script called scripts/CompileSources.groovy

5.4 Re-using Grails scripts
includeTargets << grailsScript("_GrailsBootstrap")

includeTarget << new File("/path/to/my/own/script.groovy")

5.5 Hooking into Events
Event handlers are defined in scripts called _Events.groovy

5.6 Customizing the build
5.7 Ant and Maven
Ant Integration
>grails integrate-with --ant

Maven Integration
If my project named hellosillycat
>cd hellosillycat
>grails create-pom com.sillycat

5.8 Grails Wrapper
Generating The Wrapper
build the project with install grails
>grails wrapper

Using The Wrapper
./grailsw create-domain-class com.sillycat.Person
./grailsw run-app

6. Object Relational Mapping(GORM)
6.1 Quick Start Guide
A domain class can be created with the create-domain-class command
>grails create-domain-class hello world.Person

We can customize the class by adding properties:
class Person {
     String name
     Integer age
     Date lastVist
}

6.1.1 Basic CRUD(Create/Read/Update/Delete)
Create
def p = new Person(name: "Carl", age: 30, lastVisit: new Date())
p.save()

Read
def p = Person.get(1)
assert 1 == p.id

Grails transparently adds an implicit id property to your domain class which you can use for retrieval.

We can also read the Person object back from the database
def p = Person.read(1)

This incurs no database access until a method other than getId(0 is called. That is to say, lazy load
def p = Person.load(1)

Update
Update some properties, then call save method
def  p = Person.get(1)
p.name = "Kiko"
p.save()

Delete
def p = Person.get(1)
p.delete()

6.2 Domain Modeling in GORM
6.2.1 Association in GORM
6.2.1.1 Many-to-one and one-to-one
6.2.1.2 One-to-many
class Author {
     static hasMany = [Books: Book]
     String name
}

class Book {
     static belongsTo = [author: Author]
     String title
}

6.2.1.3 Many-to-many
class Book{
     static belongsTo = Author
     static hasMany = [authors:Author]
     String title
}

class Author{
     static hasMany = [books:Book]
     String name
}

6.2.4 Sets, Lists and Maps
Sets of Objects
static hasMany = [books: Book]
It is a java.util.Set. Sets guarantee uniqueness but not order.

If we need order, we need to implement java.lang.Comparable. And define SortedSet
class Author {
     SortedSet books
     static hasMany = [books: Book]
}

class Book implements Comparable {
     String title
     Date releaseDate = new Date()
    
     int compareTo(obj) {
          releaseDate.compareTo(obj.releaseDate)
     }
}

Lists of Objects
class Author {
     List books
     static hasMany = [books: Book]
}

Bags of Objects
class Author {
     Collection books
     static hasMany = [books: Book]
}

Maps of Objects
class Author {
     Map books
}

def a = new Author()
a.books = ["key number“: "book name"]

References:
http://grails.org/doc/latest/guide/conf.html#dependencyRepositories
http://grails.org/doc/latest/guide/index.html
http://grails.org/doc/latest/guide/commandLine.html
http://grails.org/doc/latest/guide/GORM.html


你可能感兴趣的:(configuration)