Skip to content

Get Started: Maven and Gradle

Peter Thomas edited this page Jun 8, 2023 · 39 revisions

Maven

IntelliJ includes Maven and installing Java would be easier.
If you want to try that route, go here: Get Started: JetBrains IntelliJ

Install a JDK

Refer to the first part of this video for how to install Java: Karate Kick Start - The TODO Sample and Demo Project

Maven requires a Java Development Kit to be installed. You can get one here: https://www.oracle.com/java/technologies/downloads/

Note that OpenJDK also works.

Java version 11 or greater is required for Karate.

Install Maven

Maven can be downloaded from here: https://maven.apache.org/download.cgi - and then follow the installation instructions.

Maven Dependencies

Karate Maven projects are recommended to have a particular directory structure and pom.xml. The options below give you a ready-to-use project with both in place.

If you want to create a pom.xml yourself (or add Karate as a dependency to an existing project) you can use the below snippet. But make sure you refer to the documentation on naming conventions and how to run tests.

    <dependency>
        <groupId>com.intuit.karate</groupId>
        <artifactId>karate-core</artifactId>
        <version>${karate.version}</version>
        <scope>test</scope>
    </dependency>

The latest Karate version will always be updated here.

Run Maven Archetype

Use the Karate Maven archetype to create a skeleton project with one command. Or if you want to clone a sample GitHub project, skip to the GitHub Template.

If you are behind a corporate proxy, or especially if your local Maven installation has been configured to point to a repository within your local network, the command below may not work. One workaround is to temporarily disable or rename your Maven settings.xml file, and try again.

You can replace the values of com.mycompany and myproject as per your needs. Instead of X.X.X use the version of Karate that you want. The latest version will always be updated here.

mvn archetype:generate \
-DarchetypeGroupId=com.intuit.karate \
-DarchetypeArtifactId=karate-archetype \
-DarchetypeVersion=X.X.X \
-DgroupId=com.mycompany \
-DartifactId=myproject

This will create a folder called myproject (or whatever you set the name to).

Run Tests

Now you can run the example tests using mvn test. Go to the /target/karate-reports folder to see the HTML reports.

GitHub Template

Refer to the first part of this video for how to install Git: Karate Kick Start - The TODO Sample and Demo Project

You will need Git installed or an IDE that has Git support built-in.

Note that a simpler way to get started is to use GitHub Codespaces - and you don't need to install anything locally

  • Clone this GitHub project: karate-template
    • git clone https://github.com/karatelabs/karate-template.git
  • If you have Maven installed, you can open a terminal, run mvn test and see the results.
    • Note that Karate requires a Java runtime version of 11 or greater
  • Or you can choose to open the project folder in Visual Studio Code and follow the instructions here to run users.feature: Visual Studio Code
  • You can also open this project in IntelliJ with the Karate plugin installed: JetBrains IntelliJ
  • To use Gradle, refer to the section below.

Karate TODO

A more meaty project is the karate-todo. There is a YouTube video with step by step instructions to set it up. One nice thing about this demo sample is that you don't need to install Maven.

Gradle

Install a JDK

Refer to the first part of this video for how to install Java: Karate Kick Start - The TODO Sample and Demo Project

Gradle requires a Java Development Kit to be installed. You can get one here: https://www.oracle.com/java/technologies/downloads/

Note that OpenJDK also works.

Java version 11 or greater is required for Karate.

Install Gradle

Follow the instructions here: https://gradle.org/install

Clone Project

Refer to the first part of this video for how to install Git: Karate Kick Start - The TODO Sample and Demo Project

Clone the GitHub Template or download that project as a ZIP file and extract it

A more complicated GitHub project you could clone is the Karate TODO.

Gradle Dependencies

Karate Gradle projects are recommended to have a particular directory structure. * You can get a sample Gradle file from this wiki page. Save it as build.gradle in the newly created project folder. If a pom.xml file exists, you can delete it if you are not going to use Maven.

Run Tests

  • Run this command: gradle test
    • You can also do gradle test --info to see the log output and HTTP traffic on the console
  • Go to the /build/karate-reports folder to see the HTML reports.
  • You can also open the Gradle project in IntelliJ and use the official Karate plugin

Do More

You can now refer to the documentation and examples for more complex tests !

Ask Questions

The best place to ask questions on how to use Karate or if you get stuck is the Karate "tag" on Stack Overflow.