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.
- Automatic dependency management
- Automatic unit testing when building plug-in
- shading dependencies (more about that later).
- Other developers can easily depend your plugin.
- 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.
- Download your maven version from http://maven.apache.org/download.cgi. Make sure you take a binary zip.
- Extract the maven zip to your `c:/` drive. So it looks like this `c:/apache-maven-x.x.x`.
- Go to "Environment Variables".
- 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.
- 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.
- Give your project a name (I normaly take the artifactId again).
- 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.
- Click on your project and make a new folder called “jar”.
- go back to your pom and add a new dependency tag.
- Fill in a groupId, artifactId and version. Doesn’t really matter what name they have.
- add a scope tag and set it to “system”.
- 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>