Tag Archives: eclipse

Related softwares:

  1. Eclipse 3.5
  2. JDK 1.6
  3. Maven 2.2.1

First, install M2clipse-plugin in Eclipse via below M2clipse update site:

http://m2eclipse.sonatype.org/update/

If you don’t know how, please refer to this article on how to install Eclipse plugin via update site.

To use the latest Maven2, which is version 2.2.1 at the point of writing this article, you need to download the binaries from below site:

http://lawyersdb.com/mirrors/apache/maven/binaries/apache-maven-2.2.1-bin.tar.gz

Once downloaded the apache-maven-2.2.1.tar.gz archive file, unzip/untar it and place the apache-maven-2.2.1 folder in /opt. So, /opt/apache-maven-2.2.1 will be known as $M2_HOME. To set this environment variable manuallly into the system, add this below line in /etc/environment file.

JAVA_HOME=/usr/lib/jvm/java-6-sun
M2_HOME=/opt/apache-maven-2.2.1

Add also the $JAVA_HOME variable, if you haven’t done so. But make sure it is pointing to your JDK home folder. You need to re-login into your Ubuntu for this new environment variable to take effect

After re-login, create a mvn link in /usr/bin, for you to run the mvn command globally. Type the following:

cd /usr/bin
ln $M2_HOME/bin/mvn /usr/bin/mvn

To check Maven2 installation status, type below command in your console:

mvn -version

You should see Maven2 printing some info regarding the version.

Back to the Eclipse. Now, you need to setup External Tools to point to this new /usr/bin/mvn command we just created. At Eclipse main menu, choose Run > External Tools > External Tools Configuration…

Create External Tools

External Tools Configuration dialog will be prompted as below.

Empty External Tools Configuration

Right-click on Program and choose New.

Create New External Tool Menu

A new program form will be displayed. Enter the following details:

  1. Name : Maven
  2. Location : /usr/bin/mvn
  3. Working Directory : ${project_loc}
  4. Arguments : ${string_prompt}

Maven2 External Tools Config Setup

Click on Apply and Close buttons.

To use the newly created External Tools we just created, click to highlight your Maven2 project.

Choose Maven2 Project

On the Eclipse main menu, choose Run > External Tools > External Tools Configuration…

Choose External Configuration Menu to Run Maven2

Then, on the External Tools Configuration dialog, choose Maven and click on Run button.

Choose Maven to Run External Tools

A Variable input dialog will be prompted. Key in your mvn parameter you wish to pass and click OK button.

Variable Input dialog

You will see your Maven2 console is running and processing your command as shown in below.

Maven2 console

If you had setup a private remote repository, you can save the hassle for downloading artifacts from the public remote repository (central repo). Your private remote repository will download it for you and cache it on the private server. If you want to download from your private remote repository, you need to update or create your Maven2 settings.xml file. This file should be located at ${USER_HOME}/.m2 folder. The content of the file is as following:

<?xml version="1.0" encoding="UTF-8"?>

<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">
 <pluginGroups/>
 <proxies/>
 <servers/>
 <mirrors>
   <mirror>
     <id>myPrivateRemoteRepository</id>
     <mirrorOf>*</mirrorOf>
     <name>My Private Remote Repository</name>
     <url>[PRIVATE_REMOTE_REPOSITORY_URL]</url>
   </mirror>
 </mirrors>
 <profiles/>
</settings>

Replace the [PRIVATE_REMOTE_REPOSITORY_URL] with your private remote repository URL. You can put any value on the id and name elements, but it just need to be unique between other mirrors.

Traditionally, creating a new Java project is very cumbersome and time consuming, especially when you required a lot of external libraries. I might have to go to each library’s project website to search and download the jar file. Most probably I might just copied from previous project’s libraries.

Maven and Eclipse help me to avoid this mundane work. The feature of Maven with Eclipse I like to demo right now is the ‘Search for dependency from central repository’.

First, you need to do is create a Maven Project like below.

Create New Maven Project

Create New Maven Project

Then you will have the new Maven project in your package explorer. New project will consists of JUnit library by default.

Maven Project in Package Explorer

Maven Project in Package Explorer

It will also comes with App.java class by default. I will use this java class for the demo.

Standard App.java

Standard App.java

Let’s say I need to use commons-logging library, which is not yet included in the project’s classpath. I just typed in the code and of course I will get the errors as below.

Added Log and LogFactory object.

Added Log and LogFactory object.

Next, I highlighted the ‘Log’ object and I pressed Ctrl+1 to pop out the context menu. Then, choose ‘Search dependency for Log’ option.

Search for dependency from context menu

Search for dependency from context menu

I typed ‘*logging’ to search for the specific Log’s library. It will search the maven central repository based on the pattern. Then, I select Log class from commons-logging artifact.

Choose specific library

Choose specific library

Automatically, commons-logging library will be in my project’s classpath.

Commons-logging jar file is added

Commons-logging jar file is added

Log class also will be automatically imported into App.java class.

Log object is imported

Log object is imported

Since LogFactory class is from the same library, I just need to import the class by pressing Ctrl+Shift+O. This would solve all the problem and error.

Import LogFactory class.

Import LogFactory class.

My pom file is also updated automatically.

POM updated

POM updated

Isn’t this a great feature? No more searching and downloading the libraries manually from the web. Hellooo productivitayyyy.