A colleague at work (hi Clemens! :)) has shown me the wonders of Maven, it’s a really cool tool to standardise the way Java projects are compiled, tested, packaged and distributed and also provides a great way to standardise which libraries and versions are used and downloads them automatically for you.
As everything with my blog, this is more for my future self-reference than to teach anybody anything, as I’m learning it as I go myself. Here’s what I’ve done
- Installing Java
- sudo apt-get install python-software-properties
- sudo add-apt-repository ppa:webupd8team/java
- sudo apt-get update
- sudo apt-get install oracle-java7-installer
- create $JAVA_HOME environment variable
- get path to java
- sudo update-alternatives –config java
- remove the trailing /jre/bin/java, for example if update-alternatives returns:
/usr/lib/jvm/java-7-oracle/jre/bin/java, then the path for JAVA_HOME is /usr/lib/jvm/java-7-oracle/
- add JAVA_HOME=”YOUR_PATH” to /etc/evironment
- source /etc/environment to reload the file
- java -version should now work
- get path to java
- Installing maven:
- wget http://www-us.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
- tar -zxvf apache-maven-3.3.9-bin.tar.gz
- add the apache-maven-3.3.9/bin directory to the PATH environment variable by editing /etc/environment
- source /etc/environment to reload the file
- mvn -v should now work
- Creating a test maven project using an archetype. An archetype is just a template to create the folders structure and the default pom.xls file
- mkdir testmaven1
- cd testmaven1
- mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- this will create a folders structure and a pom.xml file
- Adapting the pom.xml file
- The pom.xml defines what maven will do, through the following configurations
- These variables define your application
- <groupId>com.mycompany.app
<artifactId>my-app
<packaging>jar <version>1.0-SNAPSHOT
<name>my-app
<url>http://maven.apache.org
- <groupId>com.mycompany.app
- To define the modules and versions that will be used, use the block
- <dependencies>
<dependency>
<groupId>junit
<artifactId>junit
<version>3.8.1
<scope>test
- <dependencies>
- In order to find the details of the dependency you can use mvnrepository.com, for example to figure out the details for the apache POI library you can visit: https://mvnrepository.com/artifact/org.apache.poi/poi/3.15
- Maven has a lot of plugins. Plugins are Mojos (Like Pojos, but Maven plain Old Java Object) which can be executed when running different goals.
- <build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
</configuration>
</plugin>
</plugins>
</build>
- <build>
- Executing different steps in the Build Lifecycle
- If you want to compile, test and package your java project for example, it’s as easy as running mvn package.
- validate – validate the project is correct and all necessary information is available
- compile – compile the source code of the project
- test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- package – take the compiled code and package it in its distributable format, such as a JAR.
- verify – run any checks on results of integration tests to ensure quality criteria are met
- install – install the package into the local repository, for use as a dependency in other projects locally
- deploy – done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
- If you want to compile, test and package your java project for example, it’s as easy as running mvn package.