Wednesday, February 29, 2012

Maven Best Practices

Creating a local maven repository in your company

Try  Artifactory.


It is also useful (if there are more than 1-2 developers working together) to set up an internal repository (likeArtifactory) and use that internally. It makes it much easier to deal with libraries that are not in the public repos (just add it to your internal repo!) and you can also use build tools to push builds of your own code there so other can use the modules without checking out the code (useful in larger projects)

Dividing your java maven project into sub-modules and sub-projects using m2eclipse

Tuesday, February 28, 2012

Checkout subdirectory in git for previous revision

From: http://stackoverflow.com/questions/5859943/subdirectories-in-checked-out-directory-of-a-previous-revision-not-disappearing

To checkout a sub-folder from a previous revision, what you do is three commands:

git reset <hash> thing/
git checkout <hash> thing/
git clean -fd thing/

To revert back to the HEAD state:

git reset HEAD thing/
git checkout HEAD thing/
git clean -fd thing/

I didn't try the three HEAD commands above. But should work.






Handling null values in freemarker using if condition

From: http://stackoverflow.com/questions/306732/how-to-check-if-a-variable-exists-in-a-freemarker-template

Two solutions:

First Solution:
Example 1:
<#if userName??>
   Hi ${userName}, How are you?
</#if>
Example 2:
<#if router.cableFacility.direction??>
   Hi ${router.cableFacility.direction}, How are you?</#if>
Second Solution:
Hi ${userName!}, How are you?
the default operator also supports a default value, such as:
Hi ${userName!"John Doe"}, How are you?

Tuesday, February 21, 2012

Executing a command using another user on linux

If the user has no shell associated with it (ie. passwd indicates a nologin at the end of

su -s /bin/bash -c "command to run" apache

If user has a login shell then there is no need for -s /bin/bash



Sunday, February 12, 2012

Excellent tip for importing static methods in eclipse

One of the great features of Java 1.5 is Static Imports. In order to configure Eclipse to search for static imports in a particular class, you have to perform the following steps:


1. Navigate to Preferences by clicking on the Window -> Preferences Menu Item

2. Navigate to Java -> Editor -> Content Assist -> Favorites using the menu tree (or search for Favorites using the search bar at the top)

3. Click the New Type button
4. Type in the name of the Class that has static methods that you would like to be used when using Eclipse's Content Assist / Code Completion (eg Assert)
5. Click on the Browse button which will bring up the Open Type Dialog using what you entered previously as the search criteria
6. Find the class that you would like to add, and then click Okay on the Open Type Dialog
7. Then Click Okay on the New Type Favorite Dialog.

Now when you are editing Java code, instead of typing Assert.assertEquals, you only need to type assertEquals, with Ctrl-Space, and the Assert Type will be searched for in order to resolve the static import.

Sunday, February 05, 2012

Inject Util Class with Google Guice vs static Methods?

http://stackoverflow.com/questions/4370683/inject-util-class-with-google-guice-vs-static-methods


It depends on the nature of your convert() method.
If it's something
  • simple
  • deterministic (i.e. doesn't depend on additional parameters)
  • have no side effects
  • is unlikely to change
  • etc
you can keep it as a static utility method.
Otherwise it's a good candidate for dependecy injection (you can rename it to ConversionService to make it more clear).