java - 获取Play框架可与Gradle和IntelliJ Idea一起使用

标签 java gradle intellij-idea playframework

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

我将Play与和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]]
    }
}

但是有一个问题,每次我打开项目时,我都必须重新运行想法任务,以使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 8Gradle 5.4Play 2.6.7

最佳答案

现在,以下设置可以正常运行,并且需要一些技巧才能使其余依赖项正常运行。

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

关于java - 获取Play框架可与Gradle和IntelliJ Idea一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57034079/

相关文章:

java - 无法解决 Android 错误 :Execution failed for task ':app:compileDebugJavaWithJavac'

Grails - IntelliJIdea9 奇怪的包问题

intellij-idea - 在 pom.xml 中添加依赖项后,Intellij 丢失了项目设置

java - while 循环不断结束而不启动 if 语句

java - 替换 Checkstyle 已弃用的 Gradle LoggingManager.setLevel()

selenium - 如何修复 docker 容器中的 "org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed"

java - 在 Intellij 中找不到 Servlet

java - 在jython中使用Gephi的GraphDistance插件: I have the correct types but the 'execute' method disagrees

java - 如何运行位于 android 模块内的单元测试?

java - Apache 点燃查询