This post has been migrated to http://www.thinkcode.se/blog/2014/08/24/cucumberjvm-hello-world-with-gradle
The answer to this question is: It is as easy as to run it from Maven. Depending on your background, perhaps even easier. The reason for this is because we run Cucumber from a JUnit runner. That is as a unit test with a specific runner. Maven or Gradle really doesn’t have anything to do with this. It all boils down to the need for a Gradle project that can build a Java project. A Java project that has a unit test.
A Gradle project that will build and run Cucumber could look like this:
The first thing I do is applying the Java plugin. This plugin will build a Java project. A Java project where the file structure is assumed to be the same structure as a Maven Java project. Gradle isn’t built for just coping with Java projects, you can build almost any project with it. Therefore I have to define that this will be a Java project.
Next thing to define is the version of the project. This could be anything, I use 1.0-SNAPSHOT here.
The next section defines a few dependencies that are needed. We need JUnit so we can run Cucumber from a JUnit class. We also need some Cucumber stuff. We need Cucumber for Java and we need the Cucumber JUnit runner.
The final thing needed is to define where Gradle should find the dependencies defined above. Gradle is, as Maven, all about conventions but it isn’t Maven so it doesn’t have a default repository manager for dependencies. I have to specify the repositories where Gradle should look for dependencies. I made my life easy here and specified The Central Repository.
Next thing is to build the project so that Cucumber is executed. The Java plugin has added a few build tasks that we can use to build. I will use the
build task from the root of the project. That is the same directory where
The result is a build of the the entire project where the tests are executed.
The same thing is possible to achieve using Maven and this pom:
To run this Maven project, do:
The Gradle project file is smaller than the Maven project file. They both do the same thing, build a Java project and execute the unit tests.
- Cucumber hello world – the official hello world example I have adapted
- A complete example – using Maven instead of Gradle
- Cucumber – the official web site for Cucumber
- Gradle – the build system used in this example
- More reading – My other blog posts about Gradle
- My other Cucumber blogs
- Thomas Sundberg – the author