0

我正在尝试使用Gradle作为构建工具使用Play Framework构建我的 Web 应用程序。我的 IDE 是IntelliJ Idea

我使用 Play with Java,所以我不想在我的项目中使用SBT 。但似乎IntelliJ Idea并不完全支持 Play,因为项目结构完全不同。IntelliJ 无法识别它。

经过一段时间的谷歌搜索,我找到了一个解决方案,将这个脚本添加到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]]
    }
}

但是有一个问题,每次打开项目,我都得重新运行idea task才能让IntelliJ识别Play的结构。

有没有比我的更好的方法?

太感谢了!


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

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

我正在使用Java 8, Gradle 5.4,Play 2.6.7

4

1 回答 1

0

下面的设置现在可以使用一些技巧来启动和运行其余的依赖项。

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

下一步是执行gradle idea,您应该能够只导入文件夹和 intellij。如果不是这种情况,请转到 Build tools -> Gradle 并选择 use gradle

于 2020-07-23T04:21:17.710 回答