Using Gradle with Play framework
Sep 21, 2017
2 minutes read

Basic Play Framework gradle configuration. It uses 4 plugins. Play plugin needs the java one. The other 2 are IDE specific.

apply plugin: 'java'
apply plugin: 'play'
apply plugin: 'idea'
apply plugin: 'eclipse'

Where the play plugins lives!

repositories {
    mavenCentral()
    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"
    }
}

The dependencies of the project.

Note the some of them are duplicates because Eclipse could not find the play dependencie at the classpath

dependencies {
    play    group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.197'
    compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.197'
    play    'javax.inject:javax.inject:1'
    compile 'javax.inject:javax.inject:1'
    testCompile group: 'junit', name: 'junit', version: '4.11'

    compile group: 'com.typesafe.play', name: 'play_2.11', version: '2.5.8'
    play group: 'com.typesafe.play', name: 'play_2.11', version: '2.5.8'

    // https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
    play group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
}

This block does 3 things. Sets the Play version. Turn on these Play feature that allows writing non-static methods inside a Play Controller. And copy the a Dockerfile to the dist folder every time you issue one of the distributions tasks implemented by the Gradle-Play plugin, so it can be used to deploy the artifacts generated.

model {
    components {
        play {
            platform play: '2.5.8', scala: '2.11'
            injectedRoutesGenerator = true
        }
    }
    distributions {
        playBinary {
            contents {
                from("Dockerfile")
            }
        }
    }
}

eclipse support is very limited with the current version on the Play-Gradle plugin, but these somehow make it work, not perfectly, but working.

eclipse.classpath.plusConfigurations += [ configurations.play ]
sourceSets {
    main {
	java {
	    srcDir 'app'
	}
    }
}

Intellij by the other hand is fully supported. All you need is to setup things nicely so it can find what it need within the Play scaffolding.

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

Back to posts


comments powered by Disqus