How to setup maven with sponge!

How to setup Maven with sponge

This tutorial will cover the following:

  • Why should I use maven for plug-ins
  • Maven installation.
  • Basic Maven usage.

Why should I use maven for plug-ins

Much plug-in developers have stated that usage of maven is overkill. I will try
to convince you. That that is not true.

  1. Automatic dependency management
  2. Automatic unit testing when building plug-in
  3. shading dependencies (more about that later).
  4. Other developers can easily depend your plugin.
  5. Automatic code generations (example changing your version in pom, changes your code).

##Maven installation
###Linux
The installation on Linux is amazingly simple. Just grab your favorite package manager or do
sudo apt-get install maven
###Windows
Sadly enough its not that simple on windows.

  1. Download your maven version from http://maven.apache.org/download.cgi. Make sure you take a binary zip.
  2. Extract the maven zip to your `c:/` drive. So it looks like this `c:/apache-maven-x.x.x`.
  3. Go to "Environment Variables".
  4. Add following settings (Make sure you check if the paths exists). For the PATH just **add** the line.
    Variable         Path
    M2_HOME          C:/apache-maven-x.x.x
    JAVA_HOME        C:\Program Files\Java\jdk1.7.0_XX
    PATH             ;M2_HOME/bin;JAVA_HOME/bin   
    

##Basic Maven usage
###Project setup
For maven most IDE’s support it natively. For this tutorial I will use Intellij IDEA.

  1. Make a new maven project and fill in the settings.

    A normal GroupId looks like this domain.author/company an artifactId is mostly the name of the plugin.
  2. Give your project a name (I normaly take the artifactId again).
  3. That will give you something like this:

    Let explain what everything is:
  • src/main/java: Contains java Source files.
  • src/main/resources: Contains resource files. For bukkit this would be the location for plugin.yml.
  • src/test/java: Contains test java files. Be aware that the filename has to end with “test”.
  • pom.xml: Maven settings/build file.

###Adding remote dependencies
Most people are used to add dependencies with downloading them and adding them to the classpath.
With maven that isn’t possible anymore. We will add spongeAPI as dependency.

Before we start make sure your pom.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.thomas15v</groupId>
    <artifactId>SpongeExample</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

To add depencies we use the <dependencies></dependencies> tags. Because we will download depencies from remote sources you have to add repositories as well. For this tutorial we will use mine.


    <repositories>
        <repository>
            <id>sponge-repo</id>
            <url>http://repo.spongepowered.org/Sponge/maven/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.spongepowered</groupId>
            <artifactId>spongeapi</artifactId>
            <version>LATEST</version>
        </dependency>
    </dependencies>

Add the following xml to your pom and update your maven. (Intelij will prompt for that).
Note that we use LATEST as version. This is because sponge isn’t released yet. It also makes sure you use the latest spongeAPI versions (my server checks every 5min for an update).
If you look to your external libraries. you will find this:

###Adding local dependencies
On many Forums you will find that you have to install the jar with maven. But for plug-ins we will not do that.

  1. Click on your project and make a new folder called “jar”.
  2. go back to your pom and add a new dependency tag.
  3. Fill in a groupId, artifactId and version. Doesn’t really matter what name they have.
  4. add a scope tag and set it to “system”.
  5. Also set the system path to “${basedir}/jar/SomePlugin.jar”.
    <dependency>
        <groupId>not.using.maven</groupId>
        <artifactId>SomePlugin</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/jar/SomePlugin.jar</systemPath>
    </dependency>

###Complete POM file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.thomas15v</groupId>
    <artifactId>SpongeExample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>sponge-repo</id>
            <url>http://repo.spongepowered.org/Sponge/maven/1</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.spongepowered</groupId>
            <artifactId>spongeapi</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>not.using.maven</groupId>
            <artifactId>SomePlugin</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/jar/SomePlugin.jar</systemPath>
        </dependency>
    </dependencies>

</project>
6 Likes

Sponge uses Gradle and plugins should adopt it because it’s so much nicer.
Ugh, look at that ugly XML with all that unnecessary schema blah blah.
Gradle is an all-in-one solution to dependencies, building and distributing. It uses Maven underneath but does not bother the user with it.

Dependencies are simply declared like:

dependencies {
    "org.spongepowered:SpongeAPI:1.0.0"
}

As it uses Maven, you get all maven has to offer too.

Installation is dead easy too.
Run

$ gradle eclipse

You can open the project in eclipse with everything setup
Or

$ gradle idea

for Intelij IDEA

For an example, take a look at SpongeAPI/build.gradle or Sponge/build.gradle

After discovering Gradle through ForgeGradle, I will never use Maven or Ant again!

1 Like

I think that its something personally :smile:. I just took maven because the support in the IDE is much better than with gradle. Also I think gradle includes to many files/jars to projects. But nobody stops you to make a gradlew tutorial :thumbsup:. This isn’t a thread about what tool is better.

Nope, just what’s needed in the dependencies. The dependencies are cached locally too (a local maven repo) so is quick to setup.

For sure :slight_smile: I thought I should make people aware of Gradle and that because Sponge is new, so should the ways in which we make plugins.

Oke you have made me curious. When I find some time, I will try to learn gradle instead of maven. My only and big problem is. Is that I don’t like how gradle deploys jars to maven servers. I literally have spend days to figure out how I could get sponge in my maven repo.

What about Mac OSX?

I am a to poor to buy that.
If someone could provide me instructions for that OS I would add them :smile:.

On OSX you can just use Homebrew and just type in brew install maven30

Is homebrew something widely used in the OSX world?

I honestly don’t know how widely it’s used, I just personally have always used it on OSX for package management.

1 Like

I’ve always liked maven, and seriously love XML. Don’t know why everyone hates it on the java side of the spectrum. (At least it seems everyone I meet who does java does, being more of a C++ guy more people like it over there). Anyway due to Sponge adopting gradle while this is a nice tutorial, and XML is great unfortunately you should use Gradle due to it being supported, and it’s ugly JSON.

1 Like

Its totally not a requirement to use gradle with sponge. Off course if you would like to use “NMS” stuff, it is probably impossible to use maven.

Gradle is built on the Groovy language (A language on top of the JVM), it does not use JSON.

XML is outdated, which is why old languages (e.g. C++) use it. It has a very verbose syntax and the markup can hide the actual content.

But I digress, this topic is about maven.

1 Like

I personally prefer Gradle, since it’s much easier to setup and use (i agree that XML is very old and outdated). However, for the people who prefer/use Maven, this is a pretty good tutorial :wink:

1 Like

?? … eh what

Ah sorry it looked like JSON, and I’ve never researched gradle.

XML is by no means outdated though, and I think that syntax makes it look better, and improves readability. Anyway before we get off topic too much. I agree good tutorial.

Gradle does what Maven does? O_o

@bdubz4552 That’s right, but it’s a personal preference the way it works.

Gradle > Maven. It’s like Maven + Ant. And it has wide support in a lot of IDE’s. Use Gradle for your plugins.

1 Like

Is there any documentation for using gradle? I like maven because it installs into eclipse and works nicely