Wednesday, May 18, 2016

Adding a custom folder to Play application distribution package

By default Play application distribution package is created as a directory tree, containing all necessary files for running the application.
A real application may demand adding more files or folders into the distribution.

Let's see, how to customize a Play application distribution package.

Note: This post is based on my previous posts Getting started with Play Framework and Creating of a maven project for Play application.

Source and distribution directory structures

A standard generated play application is created with the predefined directory structure (except the pom.xml, which was created manually for maven project):


A standard distribution package for the above source applications is:

Where:
  • bin contains application run scripts
  • lib contains all jars: an application artifact, an application assets jar and all dependencies.
    The assets jar accumulates all files and folders from the public folder of the source project.
  • conf contains all files from the conf folder of the source project.

Adding a custom source folder to the distribution package

It is a rather usual practice to provide scripts for deployment environment, for example, to run an application as a service on Linux etc.
The scripts should be a part of the source project and be managed by the source repository.

To add scripts to the distribution package:
1. Add to the project a scripts folder and a file app-init.sh (the file may be empty for now):

2. Add on top of the build.sbt import to NativePackagerHelper:
import NativePackagerHelper._
3. Add to the build.sbt mapping to a new directory:
mappings in Universal ++= directory("scripts") 
Full build.sbt after update:
 import NativePackagerHelper._  
 name := """play-server-example"""  
 version := "1.0-SNAPSHOT"  
 lazy val root = (project in file(".")).enablePlugins(PlayJava)  
 scalaVersion := "2.11.7"  
 resolvers += "Local Maven Repository" at "file:///"+Path.userHome.absolutePath+"/.m2/repository"  
 EclipseKeys.preTasks := Seq(compile in Compile)  
 EclipseKeys.withSource := true  
 libraryDependencies ++= Seq(  
  javaJdbc,  
  cache,  
  javaWs  
 )  
 mappings in Universal ++= directory("scripts")  

4. Build the distribution package with activator dist or maven install.
5. Open the distribution zip created in under the project root in the folder target/universal. A new folder scripts should appear:



All the sources on Git.

No comments :

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top