Sunday, June 29, 2014

Maven - Change location of local repository

maven logo
A repository in Maven is used to hold build artifacts and dependencies of varying types. There are strictly only two types of repositories: local and remote. The local repository refers to a copy on your own machine that is a cache of the remote downloads and also contains the temporary build artifacts that have not yet been released. The following post will show how to change the location of the local Maven repository on Windows.


This post assumes that Maven is installed on your computer. If not please check the following post on how to install and configure Maven.

Maven is configured based on a 'settings.xml' file that can be specified at two levels:
  1. User Level: provides configuration for a single user and is typically provided in '${user.home}/.m2/settings.xml'.
  2. Global level: provides configuration for all Maven users on a machine (assuming they're all using the same Maven installation) and it's typically provided in '${maven.home}/conf/settings.xml'.

In this example we will change the local repository location by creating/editing a 'settings.xml' file at user level.

Navigate to [maven_install_dir]/conf and if not already present copy the 'setting.xml' file to the '.m2' directory located in the user home directory (in this example the user is source4code) as shown below.

user home directory

Open the copied 'settings.xml' file and add/update the '<localRepository>' element to point to the new location of the local repository (in this example the location is set to "C:\source4code\local-repo").
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>C:\source4code\local-repo</localRepository>

...

</settings>

Next time Maven tries to resolve and download dependencies to the local repository, they will be stored in the newly defined location. The below image shows the result after creating a quickstart Maven project. The artifacts are now downloaded to the 'C:\source4code\local-repo' directory that was configured in the above 'settings.xml'.

maven change local repository location


This concludes configuring the location of the local repository for Maven. If you found this post helpful or have any questions or remarks, please leave a comment.

Saturday, June 28, 2014

Maven - Install Apache Maven on Windows

maven logo
Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies. Maven uses conventions for the build procedure, and only exceptions need to be written down. Maven is built using a plugin-based architecture that allows it to make use of any application controllable through standard input. The following tutorial will show you how to setup and configure Maven on your computer.


The tutorial assumes a Java runtime environment (JRE) has been installed and configured on your computer. If not please check the following post on how to install and configure a JRE.

Maven Install

Start by going to the Maven download page and locate the current stable version of Maven section. Download the binary ZIP file, at the time of writing it was 'apache-maven-3.2.2-bin.zip'. In this tutorial we will install on Windows.

maven download page

Extract the binaries archive downloaded in the previous step. The extracted root directory should contain a number of files and subdirectories as shown below. From now on we will refer to this directory as: [maven_install_dir].

maven install directory

Maven Configuration

Next we need to setup a 'M2_HOME' environment variable that will point to the installed Maven runtime. In addition if we want to run Maven from a command prompt we need to setup the 'PATH' environment variable to contain the Maven bin directory.

When using Windows the above parameters can be configured on the Environment Variables panel. Click on the Windows Start button and enter "env" without quotes as shown below.

edit environment variables for windows account

Environment variables can be set at account level or at system level. For this example click on Edit environment variables for your account and following panel should appear.

environment variables panel

Click on the New button and enter "M2_HOME" as variable name and the [maven_install_dir] as variable value. In this tutorial the installation directory is "C:\source4code\tools\apache-maven-3.2.2". Click OK to to save.
Note that "M2_HOME" is used for Maven 2 and later. "MAVEN_HOME" is for Maven 1.
add m2_home user variable

Select the 'PATH' entry and click on the Edit button. Add ";%M2_HOME%\bin" at the end of the variable value and click OK to save.
Note that in case a 'PATH' variable does not exist you can create it and use "%M2_HOME%\bin" as the variable value.
edit path variable value

The result should be as shown below. Click OK to close the Environment Variables panel.

environment variables panel maven configuration

In order to test the above configuration, open a command prompt by clicking on the Windows Start button and typing "cmd" followed by pressing ENTER. A new command prompt should open in which the following command can be entered to verify the installed Maven version:
mvn -version

The result should be as shown below.

maven version command

Maven Usage

Let's finish the tutorial by creating a basic Maven HelloWorld project. Open a command prompt and navigate to the directory in which you want to create the project. In the example below we will use 'C:\source4code\code'. Next enter following Maven command and press ENTER.
mvn archetype:generate -DgroupId=info.source4code -DartifactId=hello-world -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Maven will start looking for the needed dependencies in order to create the project which is based on the 'maven-archetype-quickstart' archetype (= a Maven project templating toolkit). If needed, dependencies are downloaded to the local repository. Once all dependencies are resolved, the project is created and a 'BUILD SUCCESS' statement is shown. Feel free to browse the created 'hello-world' directory. At the root you should be able to find the 'pom.xml', which is the XML representation of the Maven project.

maven archetype example


This concludes the setting up and configuring Maven. If you found this post helpful or have any questions or remarks, please leave a comment.

Java - Install a Java Development Kit (JDK) on Windows

java logo
Java is a computer programming language that is concurrent, class-based and object-oriented. It was originally developed by James Gosling at Sun Microsystems. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture. Java is currently owned by the Oracle Corporation which acquired Sun Microsystems in 2010. Following tutorial will show you how to setup and configure Java on your computer so you can develop and run Java code.

JDK Install

Java can be downloaded from the Oracle Java download page. There are a number of different download packages available, for this tutorial we will be installing Java Standard Edition (SE) on Windows. In order to be able to compile Java code we need the Java Development Kit (JDK) package that comes with a Java compiler. The JDK package also comes with a Java runtime environment (JRE) that is needed to run compiled Java code.

For this tutorial we will use an older Java version which can be obtained by scrolling all the way down to the bottom of the page and clicking on the Previous Releases - Java Archive link. Look for the Java SE 6 link and after clicking on it select Java SE Development Kit 6u45. Accept the License Agreement and pick the correct download for your operating system. In this example we will use the Windows x64 version.

jdk download page

Sign in using your Oracle account (or create a new one) and the download should start. Once the download is complete, locate the 'jdk-6u45-windows-x64.exe' file and double click to run the installer.

jdk installer welcome

Click Next and on the following screen optionally change the installation location by clicking on the Change... button. In the example below the install location was change to 'C:\Java\jdk1.6.0_45'. From now on we will refer to this directory as: [java_install_dir].

jdk installer installation path

Click Next and then Close after the installer successfully finished installing Java.

jdk installer complete

JDK Configuration

In order for Java applications to be able to run we need to setup a JAVA_HOME environment variable that will point to the Java installation directory. In addition if we want to run Java commands from a command prompt we need to setup the PATH environment variable to contain the Java bin directory.

When using Windows the above parameters can be configured on the Environment Variables panel. Click on the Windows Start button and enter "env" without quotes as shown below.

edit environment variables for your account

Environment variables can be set at account level or at system level. For this example click on Edit environment variables for your account and following panel should appear.

environment variables panel

Click on the New button and enter "JAVA_HOME" as variable name and the [java_install_dir] as variable value. In this tutorial the installation directory is "C:\Java\jdk1.6.0_45". Click OK to to save.

java_home user variable

Click on the New button and enter "PATH" as variable name and "%JAVA_HOME%\bin" as variable value. Click OK to save.
Note that in case a PATH variable is already present you can add ";%JAVA_HOME%\bin" at the end of the variable value.
edit path variable value

The result should be as shown below. Click OK to close the environment variables panel.

environment variables panel jdk configuration

In order to test the above configuration, open a command prompt by clicking on the Windows Start button and typing "cmd" followed by pressing ENTER. A new command prompt should open in which the following command can be entered to verify the installed Java version:

java -version
The result should be as shown below.

jdk version command


This concludes the setting up and configuring Java. If you found this post helpful or have any questions or remarks, please leave a comment.