IntelliJ + Play 2 integration


(Eric Wendelin) #1

The Gradle IDEA plugin doesn’t seem to know how to handle Play Framework apps yet (as of Gradle 2.8).

In order to prevent IDEA from going fireball red everywhere when developing on Play I had to add this to my build.gradle file:

apply plugin: "idea"

idea {
  module {
    sourceDirs += file("app")
    testSourceDirs += file("test")
    scopes.COMPILE = [plus: [configurations.play], minus: []]
    scopes.RUNTIME = [plus: [configurations.playRun], minus: [configurations.play]]
    scopes.TEST = [plus: [configurations.playTest], minus: [configurations.playRun]]
  }
}

It’d be cool if the Gradle Play plugin could handle this automatically, or at least put this in the Play Documentation

Cheers.


(Marcin Lagowski) #2

Thats all? I have added that to my code but it still “can’t resolve symbol …” :frowning:


(Eric Wendelin) #3

You’ll have to have the standard Play directory structure.

gradle cleanIdea idea

is assumed as well.

This is Gradle 2.8, Play 2.3.x and Java 8


(Marcin Lagowski) #4

I have tried with Play 2.4.3 and 2.3.10, Gradle 2.8-RC2 and java 1.8-20 and IntelliJ 14 Ultimate. IntelliJ still can’t resolve symbols and everything is red. This is my build.gradle. http://pastebin.com/LkpTiT41

Maybe you done something more?


(Marcin Lagowski) #5

Ok. I tried again and now this working!

gradlew cleanIdea
gradelw idea
gradlew ideaModule

Thanks for that! :smile:


(Eric Wendelin) #6

Glad it worked out :+1:


(Chun Yang) #7

I’ve found it helpful to mark the compile/test resources and generated sources as well.

idea.module {
    // Source directories
    sourceDirs += file('app')
    testSourceDirs += file('test')

    // Generated sources
    def generated = [
        file("$buildDir/playBinary/src/compilePlayBinaryRoutes"),
        file("$buildDir/playBinary/src/compilePlayBinaryTwirlTemplates")
    ]
    generated.each { src ->
        sourceDirs += src
        generatedSourceDirs += src
    }

    // Output directories
    outputDir = file("$buildDir/playBinary/classes")
    testOutputDir = file("$buildDir/playBinary/testClasses")

    // Excluded dirs
    excludeDirs -= buildDir
    excludeDirs += [
        outputDir,
        testOutputDir,
        file("$buildDir/playBinary/lib"),
        file("$buildDir/playBinary/reports"),
        file("$buildDir/playBinary/results"),
        file("$buildDir/tmp"),
        file("$buildDir/assets")
    ]

    // Dependencies
    scopes.COMPILE = [ 'plus' : [ configurations.play ] ]
    scopes.TEST = [ 'plus' : [ configurations.playTest ] ]

    iml.withXml { provider ->
        def content = provider.node.component.content.first()

        // Resources
        def confUrl = 'file://$MODULE_DIR$/conf'
        def confAttrs = content.find { it.@url == confUrl }?.attributes()
        if (confAttrs) {
            confAttrs.remove('isTestSource')
            confAttrs.type = 'java-resource'
        } else {
            content.appendNode('sourceFolder', [url: confUrl, type: 'java-resource'])
        }

        // Test resources
        def testResourcesUrl = 'file://$MODULE_DIR$/test/resources'
        def testResourcesAttrs = content.find { it.@url == testResourcesUrl }?.attributes()
        if (testResourcesAttrs) {
            testResourcesAttrs.remove('isTestSource')
            testResourcesAttrs.type = 'java-test-resource'
        } else {
            content.appendNode('sourceFolder', [url: testResourcesUrl, type: 'java-resource'])
        }
    }
}

Admittedly, the code for marking resources is ugly. I’m all ears for improvements.


(Marcin Lagowski) #8

I cant find anything about evolutions working with play plugin :frowning: Does it works for you? What dependencies I need to add? I think have tried everything(play 2.4.3 and 2.3.10 with playEbean, play java ebean and avaje ebeanorm) and it still doesnt work.


(Chun Yang) #9

There’s an outstanding pull request for the Gradle Play plugin to package resources (including evolution scripts) correctly. https://github.com/gradle/gradle/pull/528


(Marcin Lagowski) #10

Thanks!! Can’t wait it will be released! Btw. What ebean plugin in dependency you are using? I found this … But

play ‘com.typesafe.play:play-ebean_2.11:2.0.0’

  • model class nit fiund exception
    play ‘com.typesafe.play:play-java-ebean_2.11:2.4.0-M2’
  • when running model enhancement exception
    play ‘org.avaje.ebeanorm:avaje-ebeanorm:6.9.1’
  • compiling but not evolution

@edit
I have build your pull request. But now I see there is no evolution file generating at all… Does it work for you?


(Chun Yang) #11

Sorry, I’m not using ebean.

You need to write your own evolution files and place them in the right directory.


(Marcin Lagowski) #12

Ok, thanks a lot! :smile:


(Chris L) #13

This works like charm for me. Thanks, Eric
Just curious how/where did you figure it out - I am unable to find any other reference/documents on this
-Chris L


(Eric Wendelin) #14

Chris, IIRC I figured it out by looking at the XML under the .idea/ directory and cross-referenced that with a bit of the Gradle source code. Wasn’t as easy as we’d like.


(Ach A) #15

Hey guys,

Has anyone else had success with this approach and IntelliJ 16? I’ve followed everything here and my IntelliJ 16 isn’t even recognizing the external libraries besides Java 1.8. Things seem totally broken with the IDE.


(Hugo Matinho) #16

I can confirm that the above works in IntelliJ 15 but not in the current 2016.1 or any versions above 15, any help would be appreciated.


(Chad Crawford) #17

It looks like IDEA 16 splits projects into several modules that are not affected by idea.module, I think? I fixed the highlighting issue by going into Build, Execution & Deployment > Build Tools > Gradle from Idea’s Settings dialog and unchecking “Create separate module per source set”. Would there be a way to target a particular module with the idea plugin?


(Eugene) #18

Greetings guys! Any everyone who have have success experience of generate idea project using gradle?
Just I’m spend a little bit time trying fix it. After generated idea project and opened it, intellijIdea don’t see gradle’s dependencies. Could someone help me please?


(Eugene) #19

So, In my module .iml file there are orderEntry nodes with correct absolute path:

<orderEntry type="module-library">
  <library>
    <CLASSES>
      <root url="jar:///home/user/.gradle/caches/modules-2/files-2.1/com.typesafe.play/play-slick-evolutions_2.11/2.0.2/c39998e138dfc55d8217ef0cabee8db412108e10/play-slick-evolutions_2.11-2.0.2.jar!/"/>
    </CLASSES>
    <JAVADOC/>
    <SOURCES>
      <root url="jar:///home/user/.gradle/caches/modules-2/files-2.1/com.typesafe.play/play-slick-evolutions_2.11/2.0.2/3b4dfa42d55333f24eba317e8b45a7c2f6efd45c/play-slick-evolutions_2.11-2.0.2-sources.jar!/"/>
    </SOURCES>
  </library>
</orderEntry>
<orderEntry type="module-library">
  <library>
    <CLASSES>
      <root url="jar:///home/user/.gradle/caches/modules-2/files-2.1/org.postgresql/postgresql/9.4-1204-jdbc42/aae989d89b2f9b10900044d85881e3a65137b1a7/postgresql-9.4-1204-jdbc42.jar!/"/>
    </CLASSES>
    <JAVADOC/>
    <SOURCES>
      <root url="jar:///home/user/.gradle/caches/modules-2/files-2.1/org.postgresql/postgresql/9.4-1204-jdbc42/9519b3d8569209b31a27a99f9faecaff2c97efbd/postgresql-9.4-1204-jdbc42-sources.jar!/"/>
    </SOURCES>
  </library>
</orderEntry>

Why Intellij Idea don’t see these dependencies? I cannot work, because of error. Could some one help me please?


(Trần Thanh Tiên) #20

Thanks! It’s working well for me after I wasted of time (5 days)