An improved version of this tutorial is available for my new framework, Javalin. Show me the improved tutorial

IDE Guides

- Instructions for IntelliJ IDEA
- Instructions for Eclipse

About Maven

Maven is a build automation tool used primarily for Java projects. It addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies.

Maven projects are configured using a Project Object Model, which is stored in a pom.xml-file.
Here’s a minimal example:


<project>
    <!-- model version - always 4.0.0 for Maven 2.x POMs -->
    <modelVersion>4.0.0</modelVersion>

    <!-- project coordinates - values which uniquely identify this project -->
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0</version>

    <!-- library dependencies -->
    <dependencies>
        <dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
</project>

Instructions for IntelliJ IDEA


Click “File” and select “New project…”:


Select “Maven” on the left hand menu and click “Next”:


Enter GroupId, ArtifactId and Verison, and click “Next”:


Give your project a name and click “Finish”:


Paste the Spark dependency into the generated pom.xml. If prompted, tell IntelliJ to enable auto-import.


<dependencies>
    <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>


Finally, paste the Spark “Hello World” snippet:


import static spark.Spark.*;

public class Main {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");
    }
}


Into a new Class, “Main.java”:


Now everything is ready for you to run your main Class. Enjoy!

If IntelliJ says "Method references are not supported at this language level",
press alt+enter and choose "Set language level to 8 - Lambdas, type annotations, etc.".

Instructions for Eclipse


Click “File” and select “New” then “Other…”:


Expand “Maven” and select “Maven Project”, then click “Next”:


Check the “Create a simple project” checkbox and click “Next”:


Enter GroupId, ArtifactId, Verison, and Name, and click “Finish”:


Open the pom.xml file and click the “pom.xml” tab. Paste in the Spark dependency:


<dependencies>
    <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>

Save the pom.xml file!


Finally, paste the Spark “Hello World” snippet:


import static spark.Spark.*;

public class Main {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");
    }
}


Into a new Class, “Main.java”:


Now everything is ready for you to run your main Class. Enjoy!