Run Maintenance Scripts in the Context of a Play Framework Application
Sometimes you need to automate maintenance tasks in your Play application (e.g.
import data from CSV file into your app’s database, generate a report…).
To do anything useful you need the proper classpath and probably a connection to
the database using the same infrastructure normally used by the application.
This is very similar to how tests are run. In this post I will show how I
created a Scripts object that takes care of the FakeApplication
initialization to allow simple script creation and usage from the
test:console.
The Scripts object
Place a Scripts.scala file inside the test folder. This is required as the
files inside this folder are compiled with a classpath that includes the
play.api.test._ symbols.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
JogosCSVImporter
To minimize the code in the test folder place the JogosCSVImporter object
inside the app/tools folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Using scripts in the test:console
1
| |
In the test:console you can call any Scripts.\_ without worring about the
classpath or any context initialization.
1 2 3 4 5 6 7 8 9 10 | |