简体   繁体   English

让 Play Framework 与 Gradle 和 IntelliJ Idea 配合使用

[英]Getting Play Framework works with Gradle and IntelliJ Idea

I'm trying to build my web application with Play Framework using Gradle as a build tool.我正在尝试使用Gradle作为构建工具使用Play Framework构建我的 Web 应用程序。 My IDE is IntelliJ Idea .我的 IDE 是IntelliJ Idea

I use Play with Java so I don't want to use SBT in my project.我使用 Play with Java,所以我不想在我的项目中使用SBT But it seems Play's not supported fully by IntelliJ Idea because the project structure is quite different.但似乎IntelliJ Idea并不完全支持 Play,因为项目结构完全不同。 IntelliJ can't recognize it. IntelliJ 无法识别它。

After a while googling, I found a solution, adding this script in build.gradle :经过一段时间的谷歌搜索,我找到了一个解决方案,在build.gradle添加了这个脚本:

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]]
    }
}

But there is a problem, each time I open the project, I have to re-run idea task to make IntelliJ recognize the Play's structure.但是有一个问题,每次打开项目,都要重新运行idea task让IntelliJ识别Play的结构。

Is there any way that's better than mine?有没有比我更好的方法?

Thank you so much!非常感谢!


My Github example: https://github.com/TranNgocKhoa/play-gradle-intellij-example .我的 Github 示例: https : //github.com/TranNgocKhoa/play-gradle-intellij-example

My build.gradle我的build.gradle

plugins {
    id 'play'
    id 'idea'
}

def playVersion = "2.6.7"
def scalaVersion = System.getProperty("scala.binary.version", /* default = */ "2.11")

model {
    components {
        play {
            platform play: playVersion, scala: scalaVersion, java: '1.8'
            injectedRoutesGenerator = true

            sources {
                twirlTemplates {
                    defaultImports = TwirlImports.JAVA
                }
            }
        }
    }
}


def dependencyFor(String lib, String scalaVersion, String version) {
    return lib + "_" + scalaVersion + ":" + version
}

dependencies {
    play dependencyFor("com.typesafe.play:play-guice", scalaVersion, playVersion)
    play dependencyFor("com.typesafe.play:play-logback", scalaVersion, playVersion)
    play dependencyFor("com.typesafe.play:filters-helpers", scalaVersion, playVersion)
    play dependencyFor("com.typesafe.play:play-java-jpa", scalaVersion, playVersion)

    play "org.hibernate:hibernate-core:5.4.2.Final"
    play "com.h2database:h2:1.4.197"

}

repositories {
    jcenter()
    maven {
        name "lightbend-maven-releases"
        url "https://repo.lightbend.com/lightbend/maven-release"
    }
    ivy {
        name "lightbend-ivy-release"
        url "https://repo.lightbend.com/lightbend/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]]
    }
}

I'm using Java 8 , Gradle 5.4 , Play 2.6.7我正在使用Java 8Gradle 5.4Play 2.6.7

The below setup works now with some finessing required to get the rest of your dependencies up and running.下面的设置现在可以使用一些技巧来启动和运行其余的依赖项。

plugins {
    id 'play'
    id 'idea'
}
# replace with
plugins {
    id 'idea'
    id 'org.gradle.playframework' version '0.9'
}

The next step is to execute gradle idea and you should be able to just import the folder and intellij.下一步是执行gradle idea ,你应该能够只导入文件夹和 intellij。 If that is not the case go to Build tools -> Gradle and select use gradle如果不是这种情况,请转到 Build tools -> Gradle 并选择 use gradle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM