Sunday, January 11, 2015

eclipse and maven

I have become a fan of Maven (there is no surprise to it).

Download and install instructions (bottom of the page) on this page It is better to use the zip/gz and set the appropriate env than using the yum installation.

Default repository location - ~/.m2
Settings.xml - ls $M2_HOME/conf

Maven eclipse integration -
It comes with the eclipse j2ee installation (check if you have import maven project option. If not install the plugin)

Now let's configure Eclipse to use the maven we downloaded (so that we use same maven and same settings as used from console)

Preferences - Maven

1. Installations: Click Add and point to the extracted maven directory. Make it default. Eclipse comes with embedded maven and that will be used otherwise.
2. User Settings - Point to the settings.xml that you wish to into the Global and User Setting. You can point to the one that comes default with the maven package, use a company specific one or your custom one.
3. Make sure the Local Repository is same as you expect. (matches what you intended from the console)
You can look into other settings but with this we are good to go.

To import a maven project into eclipse. File - Import - Existing maven project. Now eclipse honors your project POM file for all project lifecycle options (build, deploy). Update to POM file automatically reflect in the project. To manually perform this task. Right click Project - Maven - Update project

Create a new Maven project is. New -> Maven project

Update the Java version for your Maven project



Define java.version property with the version you want. The connector automatically associates to the installed jres (in your eclipse) to the version you specified.

e.g. Say you have Jdk 1.7 and 1.8 installed and added to your eclipse.

java.version = 1.7 - associates jdk 1.7 to the project
java.version = 1.8 - associates jdk 1.8 to the project
java.version = 1.6 - associates jdk 1.7 to the project

we can have source and target at different version (source always at higher). Say we want to write code in 1.7 which will run on a 1.6 jre installation (you are under migration but your servers are still at 1.6). This will keep the JRE at 1.7 but set the compiler to 1.6.

Some of the important aspects to consider

1. Scope in a dependency - compile, runtime, provided, test, ..
2. Maven life cycles
3. Sub modules
4. Optional is a valuable keyword for dependency if you are creating a library. This will help users to ignore that dependency add their own. Say, a newer version of Logger.

'maven deploy' pushes your build artifact to the nexus repository (you have to specify repository in your maven file or it can be present in the setting.xml (top pom))

When you specify a dependency in your pom three things might happen (Maven dependencies has the references)

1. if the project is in the same workspace (uses the project directly)
2. If the project is not present it looks into the local repository (~/.m2/repository)
3. If not found in the local repository it will download from nexus and add to local repository

# My build does not pick changes from a dependent jar

The third one is a tricky one (there are configurations to play with how the dependency is resolved). Suppose you change API but have not updated the version of the artifact. When you build this project and deploy the nexus is updated with the latest changes. Now a dependent project does a maven build but still finds the old jar. This is due to the local repository already having the jar from previous build. So it is always recommended to update the artifact version.

No comments: