How to make gradle / intellij / play framework work together?

后端 未结 3 1491
名媛妹妹
名媛妹妹 2021-02-09 09:23

Here is a Gradle build as advised here for using play web framework.

plugins {
  id \'play\'
  id \'idea\'
}

reposit         


        
相关标签:
3条回答
  • 2021-02-09 10:06

    First of all you have to change your project according to gradle play plugins. here is the links, how to change your sbt project to gradle and make sure that you can execute all the necessary commands(build, clean, runPlayBinary, etc) from command prompt or terminal. When you are able to build successfully then you can import in Intellj IDE. Here is the steps you have to follow. Add idea plugins and following config for idea source configuration in build.gradle file.

    plugins {
         ....
        id '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]]
         }
    }
    ...
    
    • Type "./gradlew idea" to generate idea's necessary project files
    • Open the project in intellj(Don't import)
    • Setup sdk(java) from project structure
    • In IDE at the bottom, you will see message like "Unlinked Gradle project? Import Gradle project, this will also enable Gradle Tool Window. Don't want to see the message for the project again: press here."
      • click on this message an enable gradle fort this project and setup gradle home and JVM(not project jvm) for gradle

    To Run(Run config)

    • Open run configuration window
    • Click on(+) and Select Gradle
    • Name: as you need
    • Gradle project: your root project or location of your root project
    • Tasks: runPlayBinary

    Go to intellj preference

    • Go to [Build, Execution and Deployment] section
    • Select build tool as gradle
    • Set "Service diretory path" ex: "/Users/.../gradle/gradle-4.10.2"
    • uncheck "offline work"`

    Tested with Grade 4.10.2, IntelliJ IDEA 2018.3.2 (Ultimate Edition) snd Play 2.5.2

    0 讨论(0)
  • 2021-02-09 10:12

    I'm using Intellij (2017 and 2018), Gradle (4.6), and Play (2.5). The three of these do not mix together out of the box.

    Without help, IDEA won't recognize dependencies, and will be little more than Notepad -- no code completion, no navigation. This is because IDEA looks at the configuration model of the Java plugin, but the 'play' plugin uses the "new" software model. I don't think either side it going to change this to meet the other any time soon.

    The 'idea' plugin writes file-based project files that do not work very well, and have to be refreshed manually. I would advise that you not use the 'idea' plugin at all.

    Using 'java' and 'play' plugins, you can help IDEA understand what you want by:

    (1) Moving your Play source files around to follow the pattern expected by the 'java' plugin:

    • app -> src/main/java
    • test -> src/test/java

    You can make a symbolic link from ./test to src/test/java, or muck with the play model with plugin code. The path ./test is hardcoded (!) in the 'play' plugin, but I find IDEA works better if it finds it in src/test/java.

    (2) Configure the play plugin to get its input from the Java plugin source directories. Something like this:

    model {
        components {
            play {
                sources {
                    java {
                        source.setSrcDirs files("src/main/java")
                    }
                    resources {
                        source.setSrcDirs files("src/main/resources")
                    }
                    routes {
                        // leave in ./conf
                    }
                }
            }
        }
    }
    

    (3) Configure the java plugin to put its output in the play plugin output directories. Something like this:

    sourceSets {
        main {
            java.outputDir = file('build/playBinary/classes')
        }
        test {
            java.outputDir = file('build/playBinary/testClasses')
        }
    }
    

    (4) Give IDEA a bit more help to figure this out:

    configurations {
        compile.extendsFrom(play)
        testCompile.extendsFrom(playTest)
        implementation.extendsFrom(playRun)
        implementation.extendsFrom(playPlatform)
    }
    

    If you do this, you can then import the Gradle project directly into IDEA, use the Gradle runner for tests, and refresh the gradle project at any time.

    I realize this answer comes years late, but there is not a lot of information out there, and this answer may help people still searching for this in the distant future.

    +-+-+-+-+-+-+-+-

    EDIT: I've stopped using the above approach as it is too unreliable. Instead, I wrote my own plugin to replicate the twirl and routes compiler steps of sbt, and then just use the traditional scala (which uses java) gradle plugin, and also groovy (for spock tests). I use the application plugin to build the distribution. I import it as a gradle build in Intellij and everything "just works". I recommend not using the 'play' plugin at all.

    Use the straight-forward gradle plugins. Avoid the 'idea' and 'play' plugins. Import the project into Intellij as a gradle build -- even the traditional JUnit runner works fine. Stay away from the "new" gradle software model in your plugins. Run and debug directly from Intellij as for any other Java application.

    This means giving up the "development mode" of sbt, and always running the "ProdServerStart" main (but you can still run in dev or test "mode" of Play). But I'm not missing sbt. :^) When running ProdServerStart, for example, you always run out of a jar with a traditional classpath. You'll need to change the application secret from "changeme", and you will have to put your asset jar on the classpath. This is all straight-forward JVM and no sbt secret sauce (other then twirl and route compilers).

    I'll come back with more details but for now: @TaskAction public void generate() { getSource().forEach(sourceFile -> { TwirlCompiler.compile(sourceFile, getAppDir(), getDestinationDir(), FORMATTER, ADDITIONAL_IMPORTS, Codec.UTF8(), false, false); }); }

    and

    @TaskAction public void generate() { getSource().forEach(sourceFile -> { RoutesCompiler.RoutesCompilerTask routesCompilerTask = new RoutesCompiler.RoutesCompilerTask(sourceFile, ADDITIONAL_IMPORTS, true, true, false); RoutesCompiler.compile(routesCompilerTask, INJECTED_ROUTES_GENERATOR, getDestinationDir()); }); }

    should get you started if you are comfortable writing gradle plugins in Java.

    0 讨论(0)
  • 2021-02-09 10:16

    Answer found here. Apparently the gradle idea plugin needs to be told explicitely how to wire the dependencies.

    To sum up :

    1. Create a typical play layout
    2. Add a build.gradle (as below)
    3. Type gradle idea to generate idea's project files
    4. Open the project in intellij

      plugins {
          id 'play'
          id 'idea'
      }
      
      repositories {
          jcenter()
          maven {
              name "typesafe-maven-release"
              url "https://repo.typesafe.com/typesafe/maven-releases"
          }
          ivy {
              name "typesafe-ivy-release"
              url "https://repo.typesafe.com/typesafe/ivy-releases"
              layout "ivy"
          }
      }  
      
      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]]
           }
      }
      

    PS: tested with intellij 15.0.2 / gradle 2.10 / gradle play plugin

    0 讨论(0)
提交回复
热议问题