Environmental variables, Maven and Eclipse

If you use m2eclipse for building Maven projects in Eclipse, there are some issues while building projects having variables defined which refer Environmental Variables.


Some of our developers came out with an idea of replacing the value locally to point to the directory on their machine like


This lead to numerous incidents where developers checked in the hard coded path for this variable in our code repository, which lead to failed builds on our Hudson.

A better way to manage this is using profiles. You can create a profile in your settings.xml which resides under .m2 directory in your home directory on Linux (not sure where on Windows). Example profile configuration is as follows,


Now you have a profile named eclipse which points to the absolute path for the varialbe appserver.home.

Now checking out / importing a maven project you can specify profile as eclipse. Or in an already checked out Maven project (note that the project needs to be checked out as Maven Project. Same thing will not work on simple java projects even if they use maven), go to project properties -> Maven and set the profile to "eclipse" (the same as id parameter in your settings.xml)

Now what this does is when you make build from eclipse, it uses the profile "eclipse" and resolves the variable to the right value. Advantage of this approach is that you do not need to change pom.xml of the project so it avoids the problem of checking in the hard coded values by mistake.


Kalpak said...

It was soo painful to write XML here! I edited the html by hand. Anyone aware of a better way? I would have really loved Wiki style code blocks.