Making a plugin with Maven using IntelliJ IDEA

Aug 22, 2017
Making a plugin with Maven using IntelliJ IDEA
  • Hello!

    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 I'm going to show you how to create a Spigot plugin using Maven in IntelliJ IDEA.

    I made this tutorial because... I couldn't find any of them myself. I hope this will help you! :)

    Table of Contents
    1. Prerequisites
    2. Starting
    3. Changing the pom.xml
    4. Creating our Main class
    5. Creating a plugin YAML
    6. Compiling
    1. Prerequisites
    We will be using IntelliJ IDEA (https://www.jetbrains.com/idea/) for our plugin.
    You will also need the Java Developer Kit (JDK) which you can get at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
    Because we use Maven you need a domain name. If you don't have one, there are solutions which I will mention when we need it.
    Some people say that you don't need any basic Java knowledge but I say you do. If you don't have, get into Java before getting into the Spigot API. I recommend using the Java course on https://codecademy.com.
    2. Starting
    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 (see spoiler) it would be io.github.[username]. If you use your email (see spoiler again), [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 smash that Finish button!
    3. Changing the pom.xml
    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.1-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.1. 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!
    4. Creating our Main class
    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, or just Main. 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.
    5. Creating a plugin YAML
    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.

    6. Compiling
    It's time to compile our plugin, finally! We need to compile it into a jar file.
    Press Ctrl + Alt + Shift + S. Click Artifacts in the new window. Click the green +. Click JAR -> From modules with dependencies. Click OK. Then you can change the output directory so the plugin is compiled straight into your plugins folder. Click Apply and then click OK. Now you click Build -> Build Artifacts. Then click on your just created Build Artifact and then click build. 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]

    What's next? I will also make a tutorial about making a basic command and about handling events!
    If they're ready, I'll post a link here.

    I hope you will have a very nice time developing plugins!
  • Loading...
  • Loading...