Remote Debugging Play Framework

At times it is required to debug the code, debugging can be easy with any IDE. Running a play framework app and remote debug the application is possible by attaching the process and placing appropriate break points which would hit the breakpoint when the process is attached.

Start sbt-shell

Run Command clean compile test

This would result in starting a process with PID which can be traced from the sbt-shell window

opt/jdk1.8.0_181/bin/java -agentlib:jdwp=transport=dt_socket,address=localhost:35226,suspend=n,server=y -Xdebug -server -Xmx1536M -Didea.managed=true -Dfile.encoding=UTF-8 -Didea.runid=2018.2 -jar /home/sufia/.IdeaIC2018.2/config/plugins/Scala/launcher/sbt-launch.jar idea-shell
Listening for transport dt_socket at address: 35226

[sufia@sufia informis-server]$ lsof -i:35226
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 5981 sufia 4u IPv4 668592 0t0 TCP localhost:35226 (LISTEN)

In InteliJ->Run->Attach to Process, select the PID: 5981

This would hit the breakpoint and you would be able to debug through

Autocompile Protobuf Files

Prerequisites:

Add the plugin,

addSbtPlugin(“com.github.gseitz” % “sbt-protobuf” % “0.6.3”)

to the plugins.sbt under the project[root-build] directory.

Add the following libraries to the build.sbt in the library dependencies: “com.google.protobuf” % “protobuf-java” % (version in ProtobufConfig).value % ProtobufConfig.name, and

“com.googlecode.protobuf-java-format” % “protobuf-java-format” % “1.4”

Next add the following statements out side the library dependencies:

—- Changing the default generated source directory location from ‘src_managed’ in ‘target’ to ‘generated-source’ under ‘app’ directory —-

javaSource in ProtobufConfig := baseDirectory.value / “app” / “generated-source”

— Enable the Protobuf Plugin using the below statement–

enablePlugins(ProtobufPlugin)

Note: Make sure the name of the folder residing *.proto files is ‘protobuf’, since it is the default name to identify the *.proto files location to compile & generate protobuf classes. Finally run the `sbt shell` or `SBTConsole` to clean & compile in order to generate the protobuf classes in “generated-source” directory.

NOTE: Any changes made to the build.sbt file will not take effect until we kill the current process in `sbt shell` or `SBTConsole` & start a new one again.

Replacing proto path using REGEX

Replacing proto path using REGEX

Find & replace “import paths” in .proto files:

The following command will replace the text ‘enums/’ to ‘enums’ removing the forward slash in the entire directory consisting of .proto files.

NOTE: Make sure to give the complete text while searching to replace appropriately at the desired locations.

find ./ -name \*.proto -exec sed -i “s;enums/;\\enums;g” {} \;

s;enums/;\\enums;g

The text ‘enums/’ is the text to find in the proto files & the ‘enums’ text after the double back slash is the one to be replaced.

NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index

URL url = Thread.currentThread().getContextClassLoader()
    .getResource("javax/persistence/Table.class");
System.out.println(url);

I’ve ran into the same problem. The question here is that play-java-jpa artifact (javaJpa key in the build.sbt file) depends on a different version of the spec (version 2.0 -> “org.hibernate.javax.persistence” % “hibernate-jpa-2.0-api” % “1.0.1.Final”).

When you added hibernate-entitymanager 4.3 this brought the newer spec (2.1) and a different factory provider for the entitymanager. Basically you ended up having both jars in the classpath as transitive dependencies.

Edit your build.sbt file like this and it will temporarily fix you problem until play releases a new version of the jpa plugin for the newer api dependency.

libraryDependencies ++= Seq(
javaJdbc,
javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api"),
"org.hibernate" % "hibernate-entitymanager" % "4.3.0.Final"
)

Setting up Play Development Environment on Ubuntu

# Configure apt for java
# http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
apt-get update
apt-get install -y python-software-properties
add-apt-repository -y ppa:webupd8team/java
echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections

# Update apt sources
apt-get update

apt-get install -y unzip wget git vim oracle-java8-installer oracle-java8-set-default

# Install scala
cd /opt
wget http://www.scala-lang.org/files/archive/scala-2.12.0.tgz
tar -zxvf scala-2.12.0.tgz
# end scala

#Installing playframework on ubuntu there are two options

OFFLINE INSTALLER
Go to https://downloads.typesafe.com/typesafe-activator/1.3.12/typesafe-activator-1.3.12.zip, download the offline installer close to 617MB

OR MINIMAL ACTIVATOR

# Install play with activator
#Download and Install Play (the folder should have permission to write)
#wget http://downloads.typesafe.com/typesafe-activator/1.3.12/typesafe-activator-1.3.12-minimal.zip
#unzip typesafe-activator-1.3.12-minimal.zip
#mv activator-1.3.12-minimal activator

The off-line installer downloads all the dependencies required to run play applications. In a scenario where in we download only the small foot print of activator the dependencies are downloaded at the time of loading the project, which can be annoying and time consuming.

Follow the instructions at https://www.playframework.com/documentation/2.5.x/Installing to install the play and chose “installing play with activator” and follow instructions listed
Extracting Activator

Extract the archive on a location where you have write access. Running activator writes some files to directories within the distribution, so don’t install to /opt, /usr/local or anywhere else you’d need special permission to write to.
Adding Activator to your Path

For convenience, you should add the Activator installation directory to your system PATH:

export PATH=$PATH:/opt/activator/bin

source ~/.bashrc

Making the activator as an executable execute the following command

sudo chmod a+x /activator/bin/activator

Just in case after executing the above commands and when we issue activator in the terminal, if it says command not found, we need to manually edit the bashrc file as follows

gedit source ~/.bashrc

add the command to the bottom of the file

export PATH=$PATH:/opt/activator/bin

 

unzip typesafe-activator-1.3.12.zip
mv typesafe-activator-1.3.12 activator

cd /opt
export PATH=$PATH:/opt/activator/bin
source ~/.bashrc
sudo chmod a+x /activator/bin/activator
./activator
# end play with activator

#echo ‘PATH=${PATH}:/opt/scala-2.12.0/bin:/opt/play-2.2.5’ >> /etc/profile
echo ‘PATH=${PATH}:/opt/scala-2.12.0/bin’ >> /etc/profile

echo ‘PATH=${PATH}:/opt/activator/bin’ >> /etc/profile

sudo apt-get install git

echo “Installing IntelliJ IDEA…”

# We need root to install
[ $(id -u) != “0” ] && exec sudo “$0” “$@”

# Attempt to install a JDK
# apt-get install openjdk-8-jdk
# add-apt-repository ppa:webupd8team/java && apt-get update && apt-get install oracle-java8-installer

# Prompt for edition
#while true; do
#    read -p “Enter ‘U’ for Ultimate or ‘C’ for Community: ” ed
#    case $ed in
#        [Uu]* ) ed=U; break;;
#        [Cc]* ) ed=C; break;;
#    esac
#done
ed=C

# Fetch the most recent version
VERSION=$(wget “https://www.jetbrains.com/intellij-repository/releases” -qO- | grep -P -o -m 1 “(?<=https://www.jetbrains.com/intellij-repository/releases/com/jetbrains/intellij/idea/BUILD/)[^/]+(?=/)”)

# Prepend base URL for download
URL=”https://download.jetbrains.com/idea/ideaI$ed-$VERSION.tar.gz”

echo $URL

# Truncate filename
FILE=$(basename ${URL})

# Set download directory
DEST=~/Downloads/$FILE

echo “Downloading idea-I$ed-$VERSION to $DEST…”

# Download binary
wget -cO ${DEST} ${URL} –read-timeout=5 –tries=0

echo “Download complete!”

# Set directory name
DIR=”/opt/idea-I$ed-$VERSION”

echo “Installing to $DIR”

# Untar file
if mkdir ${DIR}; then
tar -xzf ${DEST} -C ${DIR} –strip-components=1
fi

# Grab executable folder
BIN=”$DIR/bin”

# Add permissions to install directory
chmod -R +rwx ${DIR}

# Set desktop shortcut path
DESK=/usr/share/applications/IDEA.desktop

# Add desktop shortcut
echo “[Desktop Entry]\nEncoding=UTF-8\nName=IntelliJ IDEA\nComment=IntelliJ IDEA\nExec=${BIN}/idea.sh\nIcon=${BIN}/idea.png\nTerminal=false\nStartupNotify=true\nType=Application” -e > ${DESK}

# Create symlink entry
ln -s ${BIN}/idea.sh /usr/local/bin/idea

echo “Done….Installing IntelliJ IDEA…”

echo “Start….Installing Scala..”

Step by Step load existing Play Project in IDEA

While setting up play with IDEA, it could be daunting to get things right in the first go itself. A step by step tutorial to help one out in setting up a development environment and trouble shooting issues while setting it up.

It starts off with having per-requisites on your OS. If you are looking out for a complete development environment bash script to wire up an environment you could look up earlier post, Bash Script for Play Development Environment

Once you have the Development Environment, we then proceed to load existing data sources as an IDEA project. We need to ensure before we move in that appropriate plug-ins are installed.

Pre-Requisites for IDEA

  • Add SBT Plugin: File->Settings->Plugins->Browse repositories-> SBT-> Install
  • Add Scala: File->Settings->Plugins->Browse repositories-> Scala->Install

sbtplugin


Checkout the project from the repository.

Navigate to the location where the sources would like to be cloned. On the bash issue the clone command to clone the repository.

git clone https://xxxxxx.xxxxxx.git

The command would clone the repository locally in the folder.

Load the Project

Open IDEA, Select option Import Project-> Open the Project Folder (datacaptive)-> open the folder by name Project (project) -> Select file build.sbt

Open the Project by pointing to build.sbt project file in the project folder.

In case the SDK is not selected following error is got, associate appropriate JDK by clicking “New” select JVM (located at /usr/lib/jvm/java-8-oracle)

jvmresolveerror

IDEA would start to resolve dependencies. It might take a little long to resolve for the first time.

resolvedependencies

After IDEA resolves the dependencies, it inquires to select the modules/data to include in the project

resolvingdependencyto

On resolving the project would be loaded, as shown below

screenshot-from-2016-11-25-12-05-48


Exceptional scenarios while loading the project

Controller Errors

Some time, the project would not load as anticipated. Following are some scenarios which were encountered and how they had been tackled.

If you encounter errors on Controllers, open up the sbt console in IDEA

SBT Console in IDEA

run clean and compile commands and issue a build for the project.

Create a Run Configuration as shown at the bottom of this post.

Run Successfuly


 

sbtrc client

Just in case On Resolving Dependencies, if there are errors “like unable to find sbtrc…”

 

...
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: com.typesafe.sbtrc#client-2-11;0.3.1: not found
[warn] :: com.typesafe.sbtrc#actor-client-2-11;0.3.1: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::

projects-> play-fork-run.sbt -> comment out

addSbtPlugin("com.typesafe.play" % "sbt-fork-run-plugin" % "2.5.9")

If I remove “sbt-fork-run-plugin” from that project, then it resolves the sbtrc dependencies.
https://github.com/gifman009/Licenta/blob/master/project/play-fork-run.sbt

I’m not sure if this is an Activator or SBT bug.


Unable to resolve symbol IDEA

When loading a project in IDEA, cannot resolve symbol with play framework occurs or play.data.validation does not exist

Solved it by "File" > "Open Project" and choose the base folder of the Play framework project (delete all idea settings file from the folder before so that it will reimport using the SBT settings).

http://stackoverflow.com/questions/21577573/intellij-idea-can-not-resolve-symbol-with-play-framework


Source Folder not loaded

The above steps at times would not reflect the Project Source files. In such  a case,

  1. closed IntelliJ,
  2. deleted the .idea folder or stash it
  3. restarted IntelliJ and opened the same project.

IntelliJ will generate the new .idea folder and the folders will appear.


Run Configuration

  1. Create a new Run Configuration – From the main menu, select Run -> Edit Configurations
  2. Click on the + to add a new configuration
  3. From the list of configurations, choose “SBT Task”
  4. In the “tasks” input box, simply put “run”
  5. Apply changes and select OK.
  6. Now you can choose “Run” from the main Run menu and run your application

data captive run configuration