Sunday, 4 November 2012

Hacking on grails 2.1.2 and mysql

Part A : INSTALLATION
STEP 1 :
METHOD 1 : Download and move stable version of grails-2.1.1.zip to appropriate folder
prayag@prayag:~$ wget http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.1.1.zip
prayag@prayag:~$ sudo mkdir /opt/grails
prayag@prayag:~$ sudo mv /home/prayag/Downloads/grails-2.1.1.zip /opt/grails/

prayag@prayag:~$ cd /opt/grails/

prayag@prayag:/opt/grails$ sudo unzip grails-2.1.1.zip 


Confirm packages inside /grails folder,
prayag@prayag:~$ cd
prayag@prayag:~$ ls -l /opt/grails/grails-2.1.1/
total 100
drwxrwxr-x  2 prayag prayag  4096 Nov 30 15:24 bin
-rw-r--r--  1 prayag prayag 11258 Sep 12 10:27 build.gradle
-rw-r--r--  1 prayag prayag    22 Sep 12 10:27 build.properties
drwxrwxr-x  2 prayag prayag  4096 Nov 30 16:14 conf
-rw-r--r--  1 prayag prayag  7171 Sep 12 10:27 dependencies.txt
drwxrwxr-x  2 prayag prayag  4096 Nov 30 15:24 dist
drwxrwxr-x  3 prayag prayag  4096 Nov 30 15:24 doc
drwxrwxr-x  3 prayag prayag  4096 Nov 30 15:24 dsl-support
drwxrwxr-x  3 prayag prayag  4096 Nov 30 15:24 gradle
-rw-r--r--  1 prayag prayag     0 Feb 14  2012 gradle.properties
-rwxr-xr-x  1 prayag prayag  5060 Sep 12 10:27 gradlew
-rw-r--r--  1 prayag prayag  2404 Mar 29  2012 gradlew.bat
-rw-r--r--  1 prayag prayag   117 Feb 14  2012 INSTALL
drwxrwxr-x 64 prayag prayag  4096 Nov 30 15:24 lib
-rw-r--r--  1 prayag prayag 11564 Feb 14  2012 LICENSE
drwxrwxr-x  4 prayag prayag  4096 Nov 30 15:24 media
drwxrwxr-x  2 prayag prayag  4096 Nov 30 15:24 plugins
-rw-r--r--  1 prayag prayag  2890 Feb 14  2012 README
drwxrwxr-x  2 prayag prayag  4096 Nov 30 15:24 scripts
drwxrwxr-x  5 prayag prayag  4096 Nov 30 15:24 src


METHOD 2 : Installing through launchpad (skip this method if chosen METHOD 1)
This is alternative to method 1, so you can skip this if you went through METHOD 1.
This method itself has two ways,

prayag@prayag:~$ sudo add-apt-repository ppa:groovy-dev/grails
You are about to add the following PPA to your system:
 Grails is an advanced and innovative open source web application platform that delivers new levels of developer productivity by applying principles like Convention over Configuration. Grails helps development teams embrace agile methodologies, deliver quality applications in reduced amounts of time, and focus on what really matters: creating high quality, easy to use applications that delight users. Grails naturally complements Java application development since it is built on Spring and based on Groovy, the leading dynamic language for the Java platform.
 More info: https://launchpad.net/~groovy-dev/+archive/grails
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpDUEDzV/secring.gpg' created

gpg: keyring `/tmp/tmpDUEDzV/pubring.gpg' created
gpg: requesting key 02A9EC29 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpDUEDzV/trustdb.gpg: trustdb created
gpg: key 02A9EC29: public key "Launchpad PPA for Groovy Developers" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

OR add to /etc/apt/sources.list.d/groovy-dev-grails-precise.list
Don't execute this if you have already executed add-apt-repository.
   deb http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
   deb-src http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main

Whatever you did above for METHOD2, execute following commands,
prayag@prayag:~$ sudo apt-get update
prayag@prayag:~$ sudo apt-get install grails-ppa
prayag@prayag:~$ sudo apt-get install grails 2.1.1

You are done with METHOD 2 !!!
Lets confirm where is grails installed,
prayag@prayag:~$ which grails
/usr/bin/grails

prayag@prayag:~$ ls -l /usr/bin/grails
lrwxrwxrwx 1 root root 24 Nov 21 00:56 /usr/bin/grails -> /etc/alternatives/grails

prayag@prayag:~$ ls -l /etc/alternatives/grails
lrwxrwxrwx 1 root root 34 Nov 21 00:56 /etc/alternatives/grails -> /usr/share/grails/2.1.1/bin/grails

prayag@prayag:~$ ls -l /usr/share/grails/2.1.1/
total 40
drwxr-xr-x  2 root root 4096 Nov 21 00:55 bin
-rw-r--r--  1 root root   22 Sep 12 15:12 build.properties
drwxr-xr-x  2 root root 4096 Nov 21 00:55 conf
drwxr-xr-x  2 root root 4096 Nov 21 00:55 dist
drwxr-xr-x  3 root root 4096 Nov 21 00:55 dsl-support
drwxr-xr-x 64 root root 4096 Nov 21 00:55 lib
drwxr-xr-x  4 root root 4096 Nov 21 00:55 media
drwxr-xr-x  2 root root 4096 Nov 21 00:55 plugins
drwxr-xr-x  2 root root 4096 Nov 21 00:55 scripts
drwxr-xr-x  5 root root 4096 Nov 21 00:55 src

METHOD 2 won't set any GRAILS_HOME variable which you may require later.


STEP 2 : Set the GRAILS_HOME environment variable to profile
prayag@prayag:/opt/grails211$ sudo vi /etc/profile
[...]
export GRAILS_HOME=/opt/grails/grails-2.1.1
#add the bin directory to PATH variable
export PATH="$PATH:$GRAILS_HOME/bin"


STEP 3 : grails bash autocomplete(optional)
STEP 3.1 : install gawk
$ sudo apt-get install gawk
STEP 3.2 : Download grails-autocomplete
STEP 3.3 : add to .bashrc     
[ -r /path/to/the/autocomplete/file ] && source /path/to/the/autocomplete/file

Part B : Creating an app
STEP 4 : create-app
Once grails installation part is done, it's great to great an app.
prayag@prayag:~$ grails --version
Grails version: 2.1.1

prayag@prayag:~$ mkdir -p workspace_grails/workspace_grails/
prayag@prayag:~$ cd workspace_grails/workspace_grails/
prayag@prayag:~/workspace_grails$ grails create-app gccount --stacktrace
| Created Grails Application at /home/prayag/workspace_grails/gccount

Structure of grails app 
prayag@prayag:~/workspace_grails$ cd gccount/

prayag@prayag:~/workspace_grails/gccount$ ls -l
total 28
-rw-rw-r--  1 prayag prayag  110 Nov  4 11:04 application.properties
drwxrwxr-x 10 prayag prayag 4096 Sep 12 10:30 grails-app
drwxrwxr-x  2 prayag prayag 4096 Nov  4 11:04 lib
drwxrwxr-x  2 prayag prayag 4096 Nov  4 11:04 scripts
drwxrwxr-x  4 prayag prayag 4096 Nov  4 11:04 src
drwxrwxr-x  4 prayag prayag 4096 Nov  4 11:04 test
drwxrwxr-x  7 prayag prayag 4096 Sep 12 10:30 web-app


prayag@prayag:~/workspace_grails/gccount$ ls -l grails-app/
total 32
drwxrwxr-x 4 prayag prayag 4096 Nov  4 11:04 conf
drwxrwxr-x 2 prayag prayag 4096 Nov  4 11:04 controllers
drwxrwxr-x 2 prayag prayag 4096 Nov  4 11:04 domain
drwxrwxr-x 2 prayag prayag 4096 Nov  4 11:04 i18n
drwxrwxr-x 2 prayag prayag 4096 Nov  4 11:04 services
drwxrwxr-x 2 prayag prayag 4096 Nov  4 11:04 taglib
drwxrwxr-x 2 prayag prayag 4096 Nov  4 11:04 utils
drwxrwxr-x 3 prayag prayag 4096 Sep 12 10:27 views

The structure of grails-app/conf/ is
prayag@prayag:~/workspace_grails/gccount$ ls -l grails-app/conf/
total 32
-rw-rw-r-- 1 prayag prayag   78 Feb 14  2012 ApplicationResources.groovy
-rw-rw-r-- 1 prayag prayag   88 Feb 14  2012 BootStrap.groovy
-rw-rw-r-- 1 prayag prayag 2017 Sep 12 10:27 BuildConfig.groovy
-rw-rw-r-- 1 prayag prayag 3852 May 14 17:43 Config.groovy
-rw-rw-r-- 1 prayag prayag 1225 May 14 17:43 DataSource.groovy
drwxrwxr-x 2 prayag prayag 4096 Nov  4 11:04 hibernate
drwxrwxr-x 2 prayag prayag 4096 Feb 14  2012 spring
-rw-rw-r-- 1 prayag prayag  180 Feb 14  2012 UrlMappings.groovy


STEP 5 : create-controller
prayag@prayag:~/workspace_grails/gccount$ grails
| Downloading: database-migration-1.1.zip
> ##########################################################.
Grails would like to send information to VMware domains to improve your experience. We include anonymous usage information as part of these downloads.

The Grails team gathers anonymous usage information to improve your Grails experience, not for marketing purposes. The information is used to discover which Grails plugins are most popular and is published on the plugin portal.


We also use this information to help guide our roadmap, prioritizing the features and Grails plugins most valued by the community and enabling us to optimize the compatibility of technologies frequently used together.


Please see the Grails User Agent Analysis (UAA) Terms of Use at http://www.springsource.org/uaa/terms_of_use for more information on what information is collected and how such information is used. There is also an FAQ at http://www.springsource.org/uaa/faq for your convenience.


To consent to the Terms of Use, please enter 'Y'. Enter 'N' to indicate your do not consent and anonymous data collection will remain disabled.

##########################################################.
Enter Y or N:[y,n] y                                
| Enter a script name to run. Use TAB for completion: 
grails> create-controller Main
| Compiling 113 source files
Note: /home/prayag/.grails/2.1.1/projects/gccount/plugins/cache-1.0.0/src/java/grails/plugin/cache/web/GenericResponseWrapper.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
| Created file grails-app/controllers/gccount/MainController.groovy
| Created file grails-app/views/main
| Created file test/unit/gccount/MainControllerTests.groovy


Change MainController.groovy
prayag@prayag:~/workspace_grails/gccount$ vi grails-app/controllers/gccount/MainController.groovy
package gccount

class MainController {


    def index() {"This is gccount!!!" }

}

"/"(

   controller:'main', 
   action:"/main"
)

STEP 6 : add mysql dependency to BuildConfig.groovy
prayag@prayag:~/workspace_grails/gccount$ vi grails-app/conf/BuildConfig.groovy

grails.servlet.version = "2.5" // Change depending on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"

grails.project.dependency.resolution = {

    // inherit Grails' default dependencies
    inherits("global") {
        // specify dependency exclusions here; for example, uncomment this to disable ehcache:
        // excludes 'ehcache'
    }
    log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
    checksums true // Whether to verify checksums on resolve

    repositories {

    [......]
    }
    
    dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.

        runtime 'mysql:mysql-connector-java:5.1.20'

    }

    plugins {

     runtime ":hibernate:$grailsVersion"
     [...]
    }

STEP 7 : create database gccount at mysql server

STEP 8  : configure dataSource for mysql in development mode 
prayag@prayag:~/workspace_grails/gccount$ vi grails-app/conf/DataSource.groovy 
dataSource {
    pooled = true
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
            driverClassName = "com.mysql.jdbc.Driver"
            url = "jdbc:mysql://10.13.212.4:3306/gccount"
            username = "root"
            password = "mysql55"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
        }
    }
production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
            pooled = true
            properties {
               maxActive = -1
               minEvictableIdleTimeMillis=1800000
               timeBetweenEvictionRunsMillis=1800000
               numTestsPerEvictionRun=3
               testOnBorrow=true
               testWhileIdle=true
               testOnReturn=true
               validationQuery="SELECT 1"
            }
        }
    }
} 

STEP 9 : create domain-class
 prayag@prayag:~/workspace_grails/gccount$ grails create-domain-class User
| Created file grails-app/domain/gccount/User.groovy
| Created file test/unit/gccount/UserTests.groovy

Change domain User.groovy
prayag@prayag:~/workspace_grails/gccount$ vi grails-app/domain/gccount/User.groovy
class User {
    String name;
    String password;
    Boolean active;
    static constraints = {
    }
}


prayag@prayag:~/workspace_grails/gccount$ grails generate-all gccount.User
| Finished generation for domain class gccount.User

Check grails-app/views/user directory
prayag@prayag:~/workspace_grails/gccount$ ls -l grails-app/views/user/
total 20
-rw-rw-r-- 1 prayag prayag 1590 Nov  4 13:30 create.gsp
-rw-rw-r-- 1 prayag prayag 2093 Nov  4 13:30 edit.gsp
-rw-rw-r-- 1 prayag prayag  787 Nov  4 13:30 _form.gsp
-rw-rw-r-- 1 prayag prayag 1926 Nov  4 13:30 list.gsp
-rw-rw-r-- 1 prayag prayag 2704 Nov  4 13:30 show.gsp

Check grails-app/views/controllers/gccount directory
prayag@prayag:~/workspace_grails/gccount$ ls -l grails-app/controllers/gccount/
total 8
-rw-rw-r-- 1 prayag prayag   83 Nov  4 11:46 MainController.groovy
-rw-rw-r-- 1 prayag prayag 3489 Nov  4 13:30 UserController.groovy

STEP 11 : Check routing file UrlMappings.groovy

Check UrlMappings.groovy
class UrlMappings {

        static mappings = {

                "/$controller/$action?/$id?"{
                        constraints {
                                // apply constraints here
                        }
                }

                "/"(view:"/index")

                "500"(view:'/error')
        }
}



STEP 12 : change grails-app/views/index.gsp
prayag@prayag:~/workspace_grails/gccount$ sudo vi grails-app/views/index.gsp

<div id="page-body" role="main">

                        <h1>gccount</h1>
                        <p>Welcome to gccount ! Your money friend anytime, anywhere.</p>

                        <div id="controller-list" role="navigation">

                                <h2>Available Controllers:</h2>
                                <ul>
                                        <g:each var="c" in="${grailsApplication.controllerClasses.sort { it.fullName } }">
                                                <li class="controller"><g:link controller="${c.logicalPropertyName}">${c.fullName}</g:link></li>
                                        </g:each>
                                </ul>
                        </div>
</div>


STEP 13 run app
prayag@prayag:~/workspace_grails/gccount$ grails run-app

Goto browser, and hit http://localhost:8080/gccount/ or http://localhost:8080/gccount/user/list



References
Getting Started - Reference Documentation, available at
http://grails.org/doc/latest/guide/gettingStarted.html#downloadingAndInstalling



Part C : Using git
Make sure you have git installed. You know git is a source control tool.

STEP C1 : initialize git
prayag@prayag:~/workspace_grails/gccount$ git init
Initialized empty Git repository in /home/prayag/workspace_grails/gccount/.git/

STEP C2 : check git status
prayag@prayag:~/workspace_grails/gccount$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .classpath
#       .project
#       .settings/
#       application.properties
#       grails-app/
#       target/
#       test/
#       web-app/
nothing added to commit but untracked files present (use "git add" to track)


STEP C3 : ignore some files
prayag@prayag:~/workspace_grails/gccount$ touch .gitignore
prayag@prayag:~/workspace_grails/gccount$ vi .gitignore
*.iws
*Db.properties
*Db.script
.settings
eclipse
stacktrace.log
target
/plugins
/web-app/plugins
/web-app/WEB-INF/classes

STEP C4 : create an initial commit
prayag@prayag:~/workspace_grails/gccount$ git add .

prayag@prayag:~/workspace_grails/gccount$ git commit -m "[creating initial framework for gccount]"

[master (root-commit) 89fac4c] [creating initial framework for gccount]                                                                                                                  
 112 files changed, 5178 insertions(+)  

STEP C5 : create repo at github

STEP C6 : create a remote named "origin" pointing at github repo
prayag@prayag:~/workspace_grails/gccount$ git remote add origin https://github.com/iPrayag/gccount.git

Send commits in the "master" branch to GitHub
prayag@prayag:~/workspace_grails/gccount$ git push origin master
Username for 'https://github.com': iPrayag
Password for 'https://iPrayag@github.com': 
To https://github.com/iPrayag/gccount.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/iPrayag/gccount.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

#The error is because i created .gitignore on both side, no matter I want to force update
prayag@prayag:~/workspace_grails/gccount$ git push origin master --force
Username for 'https://github.com': iPrayag
Password for 'https://iPrayag@github.com': 
To https://github.com/iPrayag/gccount.git
 + f46deda...89fac4c master -> master (forced update)

STEP C7 : Check remote origin

prayag@prayag:~/workspace_grails/gccount$ git remote -v
origin  https://github.com/iPrayag/gccount.git (fetch)
origin  https://github.com/iPrayag/gccount.git (push)


Source Code
available at https://github.com/iPrayag/gccount

No comments:

Post a Comment