Ryan Harrison My blog, portfolio and technology related ramblings

Maven - Installation and Getting Started

What is Maven?

From their page:

“a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.”

Essentially Maven makes it much easier for developers to manage their Java projects. Where it really shines is with the build process (particularly when handling dependencies, but we’ll get onto that in a later tutorial) -

“Maven allows a project to build using its project object model (POM) and a set of plugins that are shared by all projects using Maven, providing a uniform build system. Once you familiarize yourself with how one Maven project builds you automatically know how all Maven projects build saving you immense amounts of time when trying to navigate many projects.”

Sounds pretty cool huh? In addition, it turns out that Maven is really easy to install and get started with using on all your Java projects.

Installation -

First step is to download the current Maven build from their website http://maven.apache.org/download.html. Pick whichever archive format you prefer and download and extract the most recent release to somewhere on your machine.

Windows -

Now if you are on Windows, all we have to do is set up some environment variables and we are good to go.

Go to your System properties page -> Advanced system settings -> Advanced -> Environment Variables

Add the following variables:

  • JAVA_HOME -> Point to your Java installation directory e.g C:\Program Files\Java\jdk1.7.0_51

  • MAVEN_HOME -> Point to your extracted Maven directory e.g C:\Users\Name\Java\apache-maven-3.2.1

  • PATH -> Locate the existing PATH variable and Edit it. At the end add the this: ;%MAVEN_HOME%\bin; (make sure all the entries are separated by one semi-colon).

That’s all you need to do. You should now be ready to get started with using Maven.

Unix -

Run the command sudo apt-get install maven and you should be good to go straight away! Skip to the verify your installation stage to make sure you are good to go.

Verifying your installation -

Once you have followed the installation steps depending on on your operating system, open up a command line/terminal window and enter the following command to make sure everything is working correctly:

mvn -version

You should see some output that looks something like this:

> C:\>mvn -version  
> Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T17:37:5  
> 2+00:00)  
> Maven home: C:\Users\Name\Java\apache-maven-3.2.1\bin\..  
> Java version: 1.7.0_51, vendor: Oracle Corporation  
> Java home: C:\Program Files\Java\jdk1.7.0_51\jre  
> Default locale: en_GB, platform encoding: Cp1252  
> OS name: "windows 8", version: "6.2", arch: "amd64", family: "windows"

If not then you may need to try the above steps again or search online for solutions.

If on the other hand you did get output that looked like the above, then that’s it. You are ready to use Maven in your projects!

Creating a Project

To create a project with Maven, you make use of its archetype mechanism. This is basically a large collection of pre-made project templates that you can base your project from. There are a ton of different types of archetypes available so you there should already be one that matches your requirements. In this tutorial we will be using a quickstart archetype which is one of the simpler models of a basic Java application.

To construct this project first open up a terminal or command prompt window and navigate to the location on your machine where you want your project to be stored. In this example I am using a ‘Projects’ folder sitting on the root C: drive directory.

Read More

Use the Tomcat 8 server in Eclipse

The current version of Eclipse (Kepler as of writing) doesn’t natively support version 8 of the Tomcat server. Although support will be added eventually, this is not likely to be until the release of Eclipse Luna in late June 2014. In the meantime, to add support you can download and install the latest version of the WTP (Web Tools Platform) into your current Eclipse environment.

  1. Go to the WTP Downloads Page and select the link to the latest release (easiest to look at the most recent build date). As of writing the latest version is 3.6.0.
  2. Under the ‘Traditional Zip Files’ section, download the .zip file for Web App Developers.
  3. Extract the archive somewhere and copy all of the files in the ‘features’ and ‘plugins’ directories’ into the corresponding directories in your Eclipse folder (overwriting the existing files).
  4. That’s it. Open up Eclipse and you should see the option for a Tomcat 8 server.

Tomcat 8 Server in Eclipse

Read More

Git - Change most recent commit message

When using any version control system it’s inevitable that you’re going to make some kind of mistake when writing out your commit messages. Luckily Git makes it extremely simple to change the message of your most recent commit. Simply use the amend command:

git commit --amend -m "new message"

For example given this test repository with three previous commits:

Before ammended message

To change the message of the most recent commit - in this case “Third commit”, you can use git commit --amend -m "This is a modified message"

This gives the updated log messages:

After ammended message

Read More

Git - One Line Log Messages

A lot of the time when viewing the log in your Git repository you aren’t that interested in the author and date/time of each commit - the message and the hash are the most important parts. It would therefore be helpful to cut out everything from the log apart from the main details of each commit. Luckily, just like most things in Git, this is pretty straightforward to do:

git log --pretty=oneline

Which will output something like:

a4cc7fe68b3a9f9fe4b1927aa687714ca05a5096 Third commit  
246387bc6f15b1ca4a384af362cdb0deb8364b0e Second commit  
1c827a75295fe5ad657fd3882cbb3a32c3ca1b2b Initial commit

This is all well and good, yet the hash is pretty long and distracted. Again however there is a way around that as well:

git log --pretty=oneline --abbrev-commit

Which outputs:

a4cc7fe Third commit  
246387b Second commit  
1c827a7 Initial commit

This time we only get a fraction of the hash for each commit (which is all we really need) and the message - much better!

Read More

Source Code From the Movies

It happens all so often nowadays in movies -; an extremely smart character writes some really complex bit of code that nobody else can understand in order to hack into some system or otherwise perform some other complicated task.

Quite often we get a good look at the code they are using -; which often looks very obfuscated -; and we just accept it for what it is. But now someone has taken the time to figure out where the code they use in films and TV shows actually comes from. As it turns out the code they are actually use isn’t all that complicate at all -; and in most cases isn’t at all related to the task in hand.

Check out the site: Source Code in TV and Films

Read More