Question

What about Lombok integration with Play Framework 2? I really like Lombok it make my code more readable and less boilerplate. And Play Framework is wonderful too. But there is a great trouble in case if you going to mixup them.

Main reason is that scala temlates in play project compiled before domain classes. So Lombok, which itself is compiler's hack do not generate accessors for that time.

The question is: if it any ways to make it work?

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

And.. why guys from Play Framework project do not provide some Lombok-like solution? Anyway Play is full of code-generation magic and shadow compiling... so, why not?

Était-ce utile?

La solution

So, workaround is rather simple and was proposed in google groups, but there are some obscurity in docs, well I'll describe step-by-step what one should do.

1. Isolate your domain model logicaly

This means that domain classes should not call anything from views or controllers.

2. Create sub-project which contain domain classes

Some information given here. Sub-project is very similar to default play's project, and creating it not so hard. I don't find any tools to do this from play's console, so you need to create some folders by hand.

At first create sub-project folder in main project's folder. Let's assume you name it domain-data. Next, create required folders like described in standart app. layout. In most cases you want to create simple directory tree in created sub-folder:

app
 └ models
    └ myclasses

Now move all your domain classes to sub-project's directory tree.

3.Configuration

Configuration will be simple enought. Move to the main project's project folder and open Build.scala file. This is build script for SBT build system.

Define some dependencies at first. Add following strings to your build file:

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

This will add Lombok's jar to your sub-project. Now create sub-project definition:

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

Where path should point to sub-project's folder.

And the last step is updating main project configuration for making it depend on sub-project. Dependence will entails sub-project rebuiling on each main project rebuild.

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

After all start main project with play command given in terminal, and type projects command. You should see your new sub-project.

4. Profit

Now it is time to safely refactor your existing code using Lombok.

Autres conseils

My app 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 the new sub-folder : /models/app/models make a new directory move the folder

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

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

d) check, compile, run check

e) see more here sbt subprojects documentation on Play site

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top