Using maven to build projects

guide

#1

Hi All,

So my first actual post is going to be about Maven (because if you’re coding in Java it’s an insanely useful tool to have available to you). It’s a build tool for Java that will pull in dependencies for you, it also defines how the project should be built which means it’ll work across multiple IDEs and environments. It’s also cross platform and integrated into a bunch of IDEs by default.

I’m in the process of writing a getting started guide to help people who are new to it and was wondering what IDEs and tools people use when working on Java applications (I’m already covering the command line version).

Quick Tips

Here are some useful tips to help you with some common tasks

Find dependences quickly using duck duck go

Intellij also lets you add Maven dependencies to IDEA projects which is quite a cool feature. If you’re looking for a particular dependency and are having trouble finding it, Duck duck go has an info card for Maven dependencies. Search for maven followed by the name of the library, eg. “Maven gson” and it’ll list matching dependences for you.

Exclude build dir from git

As Maven stores all it’s generated stuff in the target directory your .gitignore only needs to contain one line (minus any IDE dependent files) to exclude all your build data:

target/ # exclude maven build dir

# IDE specific things
*.iml

Ignore all IDE related files from source control

On a related note, because the pom.xml file describes everything needed to build the project, you can also exclude any IDE speisfic settings (.settings or .idea/) and have the developers import the project as a maven project in their IDEs, this will stop conflicts in these files from occurring as they are never commited.

Set the Java version!

By default a Maven project defaults to Java 1.5, that’s fairly bad because 1.5 is very old and missing a lot of the nicer features of the language added in 1.6 and 1.7. You can set the Java version for the project using the following snipit in your pom.xml file:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <!-- other plugins here -->
    </plugins>
</build>

Building jars quickly

Once Maven is installed and configured, you can build Jars really easily by typing mvn package when you’re in the project’s directory. This will not include any dependencies you require in the Jar file *(which is usually the correct call) *. If you are going to be giving the Jar file to someone else to run it’s sometimes convenient to be able to create a jar which contains your dependencies and is runnable. If you use the assembly plugin you can configure mvn package to do this for you to:

<build>
    <plugins>
    <!-- other plugins here -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>uk.co.unitycoders.pircbotx.Bot</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Hope this helps


#2

It turns out that setting maven.compiler.source in properties is enough to set the java version (and makes the pom slightly tider) than the version above.

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.7</maven.compiler.source>
    	        <maven.compiler.target>1.7</maven.compiler.target>
	</properties>

You can also make a runnable jar without dependencies like so:

<build>
   <plugins>
     <!-- other bits here -->
	<plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-jar-plugin</artifactId>
	    <version>2.3</version>
	    <configuration>
	      <archive>
		<manifest>
		  <addClasspath>true</addClasspath>
		  <mainClass>uk.co.unitycoders.pircbotx.Bot</mainClass>
		</manifest>
	     </archive>
           </configuration>
	</plugin>
    </plugins>
</build>

#3

Loading stuff from inside classes

You need to use an InputStream obtained via the class loader (not the class). For example (this loads an image in the WebPigeonSpriteManager class)

    public BufferedImage load(String name) {
                try {
                    ClassLoader cl = WebPigeonSpriteManager.class.getClassLoader();
                    BufferedImage image = ImageIO.read(cl.getResourceAsStream(name + ".png"));
                    return image;
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                return null;
            }

#4

#5

Our parent POM is available on Central by the way:

<dependency>
    <groupId>com.fossgalaxy.common</groupId>
    <artifactId>parent-pom</artifactId>
    <version>0.3</version>
</dependency>

It does some basic setup for you such as setting the scm path, groupIds and creating a signed release profile, sets the Java version as 1.8, etc…