How to correctly use play2 with maven, play2-maven

2019-08-19 07:37发布

问题:

I setup a play2 project with Maven like in this example, adapted to Play 2.4 as per the official documentation of the play2-maven-plugin. I am using IntelliJ IDEA.

Now I have a simple controller:

object FooController extends Controller {
  val foo = Action {
    Ok("foo")
  }

  val bar = Action {
    Redirect(routes.FooController.foo())
  }
}

my routes file contains:

GET /foo    controllers.FooController.foo
GET /bar    controllers.FooController.bar

Now, I ran into several problems.

The first problem was FooController not finding routes. I had to manually do mvn play2:routes-compile on the command line to have the routes generated, but they are in an excluded directory named target/src_managed/main. To get them in scope, I have to use the build-helper-maven-plugin like so:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>${build-helper-maven-plugin.version}</version>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals><goal>add-source</goal></goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/src_managed/main</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

The routes work after I do mvn clean play2:routes-compile again, and IDEA shows no errors. However, I must avoid touching the managed sources folder, and avoid opening any generated source files in there, even only for looking at them, because there will be other reference problems which are really not there, but they are there because IDEA does not figure out it is a Play2 project. When doing a mvn package play2:run on the command line, the application starts and works as expected.

Unfortunately, I currently must use Maven because it is a big Maven project with many submodules I cannot change to SBT in a few hours/days.

So, is there any advice on how to correctly use this and get IDEA to recognize my project as Play2? Is it possible to auto-run the play2:resources-compile goal when I select Build/Rebuild Project or Make module?

回答1:

Use one of example projects instead of this.

I use ScalaIDE, but I've tested Idea integration and it works. Import Maven project, add Scala support, rebuild.