I’m just now getting into Gradle, and I had a little trouble just Googling a simple build script that would get me up and running with a basic Spring MVC web application quickly.
In case you’re looking for the same, here’s what I came up with:
apply plugin: ‘war’
apply plugin: ‘jetty’
sourceCompatibility = 1.6
version = ‘1.0’
testCompile (‘junit:junit:4.11’, ‘org.springframework:spring-test:3.2.2.RELEASE’)
jettyRunWar.contextPath = ”
For the record, I’m only focusing on Spring because that’s what I work on most of the time these days.
- The first section declares plugins. I think this declaration block is quite a bit more expressive than Maven, where you have to declare plugins at various points in the pom.xml in a really verbose way. With Gradle, it’s pretty simple (query if I’m losing power?): you can quickly tell from my script that I want to package the project as a war (for Tomcat/servlet container deployment) and that I want to be able to run Jetty locally to test it.
- Next, similar to Maven, Gradle needs access to a repository of artifacts. Gradle gives you access to the Maven Central Repository by declaring mavenCentral() as a repository. It’s definitely easier to read than a pom, but again, query whether I’m losing some configuration power here.
- Finally, we need to declare our dependencies. If you understand the Maven coordinate scheme, then Gradle is a little easier than the verbose pom.xml. Plus it’s easier to see what dependencies go into what build cycle. For example, I’m declaring the servlet API at compile time, but I’m not declaring JSTL or Spring MVC until the actual compilation (yes, yes, I know my Spring MVC is a little out of date). NOTE: it seems like Intellij got the dependency wrong when it created my initial gradle file. The JSTL dependency actually needs to be bundled into the WAR to get the “newer” JSTL syntax.
So there you go, it’s not that different from Maven:
- Tell Gradle what plugins you want to run.
- Tell it where to get its artifacts.
- Tell it what artifacts you want.
- Save it as build.gradle and then run gradle tasks to see what you can do with it.
Good luck out there.