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).
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