Learning new tricks - JHipster: AngularJS, Spring Boot

January 26, 2015

I have always thought that the best motivator to learn something is when somebody pays you for doing it (indirectly). Last year I accepted a small freelance project just because it would be a good oportinity to sharp skills and add something new to my bag of tricks. Since for sometime I have been using some “outdated” things (Ember.js, ColdFusion, Node.js , YUI Library), so I thought “it would be cool to use something “mainstream/popular” with younger developers, and I got surprised because I found something that had all of that and it was a cool name: JHipster

How to decide what to learn next?

As always on technology the most difficult thing is to decide what to use. My main criterion was popularity so this one were my chooses :

  • Frontend : AngularJS
  • Backend: Spring Boot (Java)

Since I always try to use the least effort possible I didn’t want to setup everyting from scratch, so I looked for some templates and found out that JHipster had everything

How to start a new project with Spring boot and AngularJS

jhipster is a yeomans generator, so you need to install nodejs, yeomans and bower previosly.

npm install -g generator-jhipster
mkdir mi-proyecto
cd mi-proyecto
yo jhipster

How to deploy to openshift

Even thought jhipster provides a yeomans generator for openshift, I enconter some problems and had to twick the generated deployment code. This is what I did after I installed the openship sdk (see openshift getting-started-overview )

cd mi-proyecto
yo jhipster:openshift
grunt deployOpenshift
git push 

the problem comes when you look at the application logs in openshift servers

[ERROR] com.zaxxer.hikari.util.PropertyBeanSetter — Exception setting property url on target class class org.postgresql.ds.PGSimpleDataSource
 java.lang.reflect.InvocationTargetException: null
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_71]
 Caused by: java.lang.NullPointerException: null
 at org.postgresql.ds.common.BaseDataSource.setUrl(BaseDataSource.java:586) ~[postgresql-9.3–1102-jdbc41.jar!/:na]
 … 146 common frames omitted

After some research I realized that someway Spring Boot couldn’t use the connection URL on the hook script generated by jhipster:openshift

#! /bin/bash
set -x
jar=$(ls ${OPENSHIFT_REPO_DIR}target/*.war | head -n1)
nohup java -server \
 -jar ${jar} \
 —spring.profiles.active=prod \
 —server.address=${OPENSHIFT_DIY_IP} \
 —server.port=${OPENSHIFT_DIY_PORT} \
—spring.datasource.url=jdbc:postgresql://${OPENSHIFT_POSTGRESQL_DB_HOST}:${OPENSHIFT_POSTGRESQL_DB_PORT} \
 —spring.datasource.username=${OPENSHIFT_POSTGRESQL_DB_USERNAME} \
 —spring.datasource.password=${OPENSHIFT_POSTGRESQL_DB_PASSWORD} > ${OPENSHIFT_LOG_DIR}mi-proyecto.log 2>&1 &

So instead of specify the database credentials on the openshift’s start hook I did the following:

  1. remove the datasource credential lines from mi-projecto/deploy/openshift/.openshift/action_hooks/start

    #! /bin/bash
    set -x
    jar=$(ls ${OPENSHIFT_REPO_DIR}target/*.war | head -n1)
    nohup java -server \
     -jar ${jar} \
     —spring.profiles.active=prod \
     —server.address=${OPENSHIFT_DIY_IP} \
     —server.port=${OPENSHIFT_DIY_PORT} > ${OPENSHIFT_LOG_DIR}mi-proyecto.log 2>&1 &
     
  2. Speficy the openshift enviroment variables in mi-proyecto/src/main/resources/config/application-prod.yml

    server:
     port: 8080
    spring:
     profiles: prod
    datasource:
     dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
     url:
     databaseName: mi-proyecto
     serverName: ${OPENSHIFT_POSTGRESQL_DB_HOST}
     username: ${OPENSHIFT_POSTGRESQL_DB_USERNAME}
     password: ${OPENSHIFT_POSTGRESQL_DB_PASSWORD}

Last by not least and to make my life easier I added these alias to my terminal

alias mi-proyecto.deploy=’cd $mi-proyecto && mvn -Pprod package && grunt deployOpenshift && cd deploy/openshift && git push’
alias mi-proyecto.logs.prod=’cd $mi-proyecto && rhc tail mi-proyecto -f app-root/logs/mi-proyecto.log’

Conclusion

Although my principal motivation with this project was to learn a new programming tool, I think I learned much more about cloud deployments


Do you want to know something?→Click!