Creating a plugin with Maven using IntelliJ IDEA

Feb 9, 2018
Creating a plugin with Maven using IntelliJ IDEA
  • Creating a blank Spigot plugin

    Using Maven in the IntelliJ IDE



    Maven is a software project management and comprehension tool, as Apache, the creators of Maven say it.
    The main objective of Maven is to make the build process easier. And that's exactly what we want.

    In this tutorial you are going to learn how to create a Spigot plugin using Maven in IntelliJ IDEA.


    Prerequisites(top)

    • We will be using the IntelliJ IDEA IDE to develop our plugin.
    • You will also need the Java Developer Kit (JDK)
    • Because we use Maven you need a domain name. If you don't have one you can also use your github profile or your email as mentioned below.
    • Some basic Java knowledge. If you already know how to program take a look at the Crash Course to Java in our Wiki or Oracle's Nuts and Bolts overview.

    Creating a new project(top)

    If you start IntelliJIDEA for the first time, you'll see something like this:

    [​IMG]

    Click "Create New Project". If you've already opened InteliJ IDEA before, click File ->
    On the left side, click Maven, then click on the dropdown menu called "Project SDK". Navigate to where you have JDK installed. Click "Next".

    Now, you need your domain name. Don't use a domain you don't own!
    At GroupId fill in your domain name, but inverted. In my case codingtime.nl would be nl.codingtime.
    If you use GitHub it would be io.github.[username]. If you use your email, [yourname]@gmail.com would be com.gmail.[yourname].

    Your ArtifactId is the name of your plugin. In this case TutorialPlugin. If you had a plugin called MyAwesomePlugin the ArtifactId would be MyAwesomePlugin, of course.

    [​IMG]

    This is what the screen should look like! Hit that next button. We can almost start programming, but there are a few more things to do. Now you can change the project name but that's not necessary. Assuming that you don't want your project to be in a random folder, change the project location to a place where you want your project to be saved.

    Now press the Finish button!

    Setting up the pom.xml(top)

    POM stands for Project Object Model. That's where we add our depencendies and other stuff.

    You can see it on your screen now. To tell Maven that we make a Spigot plugin and want to use the API for that, we need to add a few things to the pom.xml file. We need to tell Maven where to get the API from, and then we need to tell Maven that our plugin depends on the Spigot API. Do that by copy-pasting this into your pom.xml:
    HTML:
        <repositories>
            <repository>
                <id>spigot-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshot/</url>
            </repository>
        </repositories>

        <dependencies>
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.12.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    Of course you can change the version of the API. A plugin made with the 1.11.2 API would have a version of 1.11.2-R0.1-SNAPSHOT which is very similar to 1.12.2. However, there can be some difference when using other versions.

    This is what it should look like:

    [​IMG]

    Somewhere on the right a small pop-up appears. Click on the right link.
    Now that we have all the pom.xml stuff done, we're going to make our Main class!

    Creating the main plugin class(top)

    On the left side, uncollapse "TutorialPlugin" or whatever name you've chosen. Navigate all the way to TutorialPlugin -> src -> main -> java. Right-click java -> New -> Package. As the package name use your inverted domain + your plugin name. In the TutorialPlugin's case that would be nl.codingtime.tutorialplugin. On the left right-click your new package and click New -> Java Class.
    In the pop-up screen fill in a name. Fill in your plugin name e.g. TutorialPlugin. Theoretically you can choose what you want but in this tutorial we're going for the plugin name. This is what the screen looks like:

    [​IMG]

    Note that you may not use capitals in your package name. Some images are wrong!

    Click OK. Now we can start programming!

    Your Main class must extend JavaPlugin. So instead of
    Code (Java):
    public class TutorialPlugin {
    }
    type
    Code (Java):
    public class TutorialPlugin extends JavaPlugin {
    }
    IntelliJ IDEA will automatically add the import for us. If that doesn't happen, import org.bukkit.plugin.java.JavaPlugin.

    Now let's create our first two methods: onEnable() and onDisable(). These aren't required since they're already in the JavaPlugin object, although some people say that. Let's send a message to the console when these methods are called which we'll do with Bukkit's Logger:
    Code (Java):
    public class TutorialPlugin extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("onEnable is called!");
        }
        @Override
        public void onDisable() {
            getLogger().info("onDisable is called!");
        }
    }
    We can add much more than we did now, but first we're going to create a plugin YAML which Spigot reads to get some information about our plugin.

    Creating a plugin YAML(top)

    We can almost compile our plugin, but first we need one more file, called plugin.yml. That file introduces our plugin to Spigot.

    On the left right-click Resources -> New -> File.
    In the pop-up screen fill in "plugin.yml" as the file name.

    [​IMG]

    Click OK.

    There are 3 things Spigot needs to know:

    1. The plugin name
    2. The plugin version
    3. The path to the Main class
    This is what it should look like:

    [​IMG]

    Name is the name of your plugin. It could be anything, but I recommend setting it to the name of your plugin. :p
    Version could also be anything, but since this is our first build we start at 1.0. We could also start with 1.12.0 since we made it for 1.12 and 1.12.1. If Minecraft and Spigot update to 1.13, our new version is 1.13. For this tutorial, we just start at 1.0.

    For more information on the possible values that the plugin.yml can include take a look at the overview on the Bukkit Wiki.

    Compiling(top)

    It's time to compile our plugin, finally! We need to compile it into a jar file.

    Look to the right side of your IDE window, you should see a tab labeled "Maven". Expand it, then click the drop down next to your plugin's name and expand the Lifecycle tree. Now just double-click "package" and your plugin will be compiled and packaged, all ready to distribute and run on your server.

    [​IMG]

    You can find the compiled jar file under your project root in a folder named "target".

    You're done! You can run your server and your plugin will tell you that the onEnable method is called, as we have made in the onEnable() method.

    [​IMG]
  • Loading...
  • Loading...