Using Maven and Eclipse IDE to provide a development environment for Java

Maven is apaches project build tool, and eclipse is the open source IDE, that provides easy to use tooling for Java. If you want to get setup for doing java development, this is an easy place to start. There are alternative build tools and IDEs that you can use for java development, so there is no reason you couldn’t switch to one of those if you are experienced and prefer. But for starting out, certainly Eclipse and Maven are a good choice.

In this we are going to assume that you are going to be using SVN for source control. Again there are other options out there, but basically the concepts should be the same or similar. The version of eclipse that we are going to use should make it easy to get setup with SVN, so you should be able to get up and running quite quickly. We also use fitnesse for testing, and additional tooling to help manage our development and deployment.

Download and Install The Java Development Kit from Oracle

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Grab the version you want to use, then install on your PC

Download Eclipse

http://www.eclipse.org/downloads/packages/

The latest release of eclipse should be available here. I recommend downloading the latest Eclipse IDE for Java Developers as this should contain what you need for Java, Maven and SVN. The enterprise edition includes extra tooling that you might not need. Eclipse runs from whichever directory you run it in, so just unzip it and then move/copy/rename to whichever directory you prefer to run it from. I usually create a directory such as c:\dev so that I can have all my software development tools and software under 1 directory that’s easy to find.

Fitnesse

Fitnesse is an acceptance testing tool built around a web based wiki.

Grab fitnesse-standalone.jar from http://www.fitnesse.org/FitNesseDownload and copy it to your c:/dev folder (or whatever you are using as your dev folder), and click it to run it. This should create a FitnesseRoot folder in your dev folder.

Setting Up Your Eclipse Environment

Start eclipse by navigating to the eclipse install directory and run eclipse.exe to start. When you start eclipse you will need to decide the location of your workspace. You can create the workspace inside you dev environment, so for example use c:/dev/src as your workspace location. Then any projects you check out or create will be in c:/dev/src/

If this is your first project, and you don’t already have an eclipse project in SVN that you can check out and work on then you will need to configure a new java project for eclipse/maven. This will involve setting up your startup batch files to configure eclipse for your needs and creating your pom.xml for maven, and doing a maven build so that your project is setup for eclipse. We will discuss doing this in another post so we can see in detail how to setup an eclipse/maven project from scratch. For this post we will be checking out an existing eclipse/maven project

At this point you should be able to check out your existing project from SVN, using your SVN URL that you should have or be provided with. Eclipse also allows you to use GitHub as your source repository if you prefer or have access to a project in GitHub. Check out your project at the top level so you get in Eclipse. In eclipse, Window, Open Perspective, Window Repository Exploring and enter your repository URL to nagivate to your project. Then right click and ‘check out’.

Batch Files

You will probably find it useful to create a batch file in your eclipse project to start eclipse. This will allow you to set up environment variables to control parts of your application for testing on your PC etc.

For example, below we setup some environment variables before we start eclipse

set FITNESSE_DIRECTORY=C:/dev/
set WORKSPACE=C:/dev/src
set ECLIPSE_HOME=C:\dev\eclipse-java-kepler-SR1-win32-x86_64\eclipse
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45

start “Eclipse” /B %ECLIPSE_HOME%/eclipse.exe -name %LastFolder% -vmargs -Xmx1024M

To make this more user friendly, you could set the environment variables in another batch file, so that each person could have their own personalised set of environment variables that they can modify after checking out the project

Eg add call setUserEnv.bat to the startEclipse.bat batch file above and use that to set the environment variables so that each person can have their own. Be sure to set these to svn:ignore and ignored files under team preferences in eclipse so they are not commited to SVN each time, otherwise each user will overwrite the other settings.

Once you have a batch file configures the way you need it, you can use that to start eclipse and have your required environment variables set each time. Create a shortcut to that batch file in windows explorer and copy that to your desktop or start button to ensure you can use that to start eclipse each time.

At this point shut down eclipse and start it again using your shortcut

Maven

Download and unzip maven in your dev folder from https://maven.apache.org/download.cgi. Eg c:/dev/apache-maven-3.0.3

Set eclipse to use JDK rather than JRE so that maven can compile (Windows, Preferences, Installed JRES, Search)

Set eclipse to use the external maven you have installed rather than the default (Windows, Maven, Installations)

2 changes need to be made to the config of your external maven install in the /conf/settings.xml file

Cofiguring your local repository

You need to set your local repository to be a location on your PC. It makes sense to put this under your user directory as follows (example if for windows 7 64 bit)

C:\Users\myid\.m2\repository

If you are behind a proxy you need to set that

true http myproxy.com 8080 127.0.0.1|myintranet

Additional Jars.

When using maven the vast majority of jars and their dependencies that you need will be automatically downloaded from the Maven repository when you update your pom to include them. But in some cases the jars are not available from a Maven repository and are only available from the vendor themselves. An example of this is the JDBC drivers for the oracle database or the IBM MQ cient jars. In this case the jars must be manually added to your maven repository.

Details of adding further additional jars if required are here:
http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/

For example to add 2 ibm jars, we ran in the same directory as the projects pom (c:/dev/ for example)

mvn install:install-file -Dfile=c:\jars\com.ibm.mq.jmqi-7.0.1.6.jar -DgroupId=com.ibm -DartifactId=com.ibm.mqjmqi -Dversion=7.0.1.6 -Dpackaging=jar
mvn install:install-file -Dfile=c:\jars\dhbcore-7.0.1.6.jar -DgroupId=com.ibm -DartifactId=com.ibm.dhbcore -Dversion=7.0.1.6 -Dpackaging=jar

and here is the one for Oracle
mvn install:install-file -Dfile=c:\jars\oracle\ojdbc6_g\11.2.0.4\ojdbc6_g-11.2.0.4.jar -DgroupId=com.oracle -DartifactId=ojdbc6_g -Dversion=11.2.0.4 -Dpackaging=jar

and one which shows how to include the javadoc and sources:
mvn install:install-file -Dfile=C:\Temp\powermock-module-junit4-1.6.3.jar -DgroupId=org.powermock -DartifactId=powermock-module-junit4 -Dversion=1.6.3 -Dpackaging=jar -Djavadoc=c:\Temp\powermock-module-junit4-1.6.3-javadoc.jar -Dsources=C:\Temp\powermock-module-junit4-1.6.3-sources.jar

where file is where you have the jars(renamed to include the version on the end), version, groupId and artifactId to identify the jar.
Once you have run those successfully, copy the relevant folder from your repository to the T drive to make it accessible to everyone

eg from C:\Users\myid\.m2\repository\com\ibm to T:\Common\CollateralOptimisation\CTL Development\mavenjars\ibm

In your workspace, right click on pom.xml, then run as, maven test (any errors check that you have done all of the above)

If you have problems executing a Fitnesse test, and get noclassdeferrors, scroll down and check that the classpath of the test looks correct. If it doesnt check your environment batch files. You may have to override the HOME variable by adding set HOME=%HOMEPATH%

-Maven must be run by a JDK
in Eclipse, go toRun -> Run Configuration…
and ensure that your Maven Eclipse Rebuilds are set to use a JDK. Under the JRE tab, confirm the workspace default or alternate JRE actually points to a JDK

– Refresh using native hooks or polling, Refresh On Access
These options will ensure you are working with the latest version of files by keeping them in sync in the editor and file system.
Window -> Preferences -> General -> Workspace, and make sure both of the above are ticked

-Disable Build Automatically
When running a Maven build you may get a conflict with Eclipse if it is set to build automatically on changes to java source files. This is why ideally it would be better to turn this option off, so that you can control the build of your project manually when needed. In Eclipse go Window à Project à Deselect ‘Build Automatically’ from the drop down menu.

-Save Actions
Go to Window -> Preferences -> Java -> Editor -> Save Actions to ensure the following are selected
Format Source Code àFormat Edited Lines
Organize Imports
Additional Actions à
Add final modified to private fields
Add missing override annotations

Source Control Ignore Resources
There are some items that you don’t necessarily want to save in your source control system. Things like temporary files, unit test results and output etc. So set these to be ignored
Window -> Preferences -> Team -> Ignored Resources -> Add Pattern…
*/*.zip
*/~*
/*/src/test/FitNesseRoot/files/testProgress
/*/src/test/FitNesseRoot/files/testProgress/*
/*/src/test/FitNesseRoot/files/testResults
/*/src/test/FitNesseRoot/files/testResults/*
/*/src/test/FitNesseRoot/RecentChanges
/*/src/test/FitNesseRoot/RecentChanges/*
.classpath
.project
*log.*
*.log
and make sure *.zip is ticked

Maven Build For Eclipse Project
Once you have checked out your project from your source control system or built it for the first time from a new project, you need to run the maven eclipse build to configure it for Maven and Eclipse
Create a new run configuration via menu option
Run àRun Configurations à Scroll down to maven build and then right click and click new.
In base directory enter ${env_var:PROJECT_ROOT}
In goals enter eclipse:clean eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true
Click apply and then run

This should start the build and then download any dependencies that are required (Network connection permitting)

Code Formatting
We enforce team wide code formatting by letting Eclipse doing it. To Ensure we all use the same rules, please use the Eclipse 2.1 [build-in] profile
Window -> Preferences -> Java -> Code Style -> Formatter
If you don’t do this it causes problems in source control as everyone has different source formats

Eclipse Compiler
The compiler used by eclipse for Java projects is set at
Window -> Preferences -> Java -> Installed JREs

If you need to set or enforce your JDK compiler level for a project this is under
Project -> Properties -> Java Compiler

Network
If you need to configure your network settings, for instance for setting a proxy etc, that’s configured at
Window -> Preferences -> General -> Network Connections

Saving Your Eclipse Preferences
At this point you should save your preferences, so that they are easy to recover if you need to rebuild your workspace for any reason
File à Export àGeneral à Preferences à Next à Tick Export All à Give the file a name, then finish

Leave a Comment

Your email address will not be published. Required fields are marked *