Lombok with Play 2

How about integrating Lombok with Play Framework 2? I really like Lombok , which makes my code more readable and less conditional. And the Play Framework is great too. But there are big problems if you are going to mix them.

The main reason is that scala temlates in a replay project compiled before domain classes. Thus, Lombok, which itself is a compiler, does not create accessors for this.

The question is, are there any ways to make it work?

I found some discussions on Google Groups , but they do not provide any reasonable solution. So, do you have any success?

And .. why don't the guys from the Play Framework project provide some kind of solution like Lombok? In any case, Play is full of magic code generation and shadow compilation ... so why not?

+6
source share
2 answers

So, the workaround is quite simple and was suggested in google groups , but there is some ambiguity in the documents, I will describe the steps, step by step, what to do.

1. Isolate your logical domain model

This means that domain classes should not call any of the views or controllers.

2. Create a subproject containing domain classes

Some information is given here . The subproject is very similar to the default project and creates it is not so difficult. I do not find any tools for this from the game console, so you need to create several folders manually.

First create a subproject folder in the main project folder. Suppose you called it domain data. Then create the necessary folders as described in the standard application. layout . In most cases, you want to create a simple directory tree in the created subfolder:

app β”” models β”” myclasses 

Now move all the domain classes to the subproject tree.

3.Configuration

The configuration will be quite simple. Go to the main project project folder and open the Build.scala file. This is a build script for the SBT build system.

Define some dependencies first. Add the following lines to the assembly file:

 val domainDependencies = Seq( "org.projectlombok" % "lombok" % "0.11.4" ) 

This will add the Lombok jar to your subproject. Now create a subproject definition:

 val domainProject = PlayProject( "domain-data", appVersion, domainDependencies, path=file("domain"), mainLang=JAVA ) 

Where the path should point to the subproject folder.

And the last step is to update the main configuration of the project to make it dependent on the subproject. Dependence entails the redesign of subprojects with each major restoration of the project.

 val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).dependsOn(domainProject) 

After starting the main project with the playback command specified in the terminal, and enter the project command. You should see your new subproject.

4. Profit

Now it's time to safely reorganize your existing code with Lombok.

+3
source

My application is "conseillers" (Play 2.2.1 project) and my model is "Conseiller.java" in / app / models

a) Just create two folders and move the "/ app / models" folder to a new subfolder: / models / app / models make a new directorymove the folder

b) add a new build.sbt file to this new folder create a new sbt file

c) change the build.sbt file in the application root folder with .dependsOn (not sure if the .aggregate value is 100% necessary): modify root build sbt file

d) check, compile, run check

e) for more information see the subprojects sbt documentation on the Play website

0
source

Source: https://habr.com/ru/post/918234/


All Articles