Relocation of CodeBalance

CodeBalance blog has been renamed as CodeBuild on 12th January 2012, by the author.
You can reach the new site via
Blog postings about software development will continue on CodeBuild exactly as before.

Thanks for your concern.

Leave a comment

Java Dynamic Web Project to Maven 3 Project - Migration Steps

If you start a java project in eclipse as "maven project" at the beginning, all you need is downloading maven distribution or maven plug-in and add dependencies to the pom.xml file when required. But some issues exist while migrating from java dynamic web project to maven project. In this post, required configuration steps will be told. Maven introduction, installation, "repository" concept and maven commands are out of scope for this post.

  • Source codes for a maven project must be located in src/main/java folder. Also test and resources must be located in src/test/java and src/main/resources folders respectively. For this conversion, you can create required folders manually and copy old source codes in it, but more preferred and stable way is defining source paths in Project Properties --> Java Build Path section:

Source folder configuration

  • /WEB_INF folder is not used in maven projects. There is "webapp" folder instead. You can move WEB_INF files into webapp folder as the given structure  and delete old WEB_INF folder. You don't even need lib folder because maven will create required jars:
webapp folder

  • target/classes folder must be the output (deploy) path instead of build/classes. Again in Project Properties --> Java Build Path section:

Output folder

  • org.eclipse.wst.common.component file needs some changes. Open it using ctrl+shift+R and edit deploy path and output path as below:
org.eclipse.wst.common.component file
  • Create pom.xml file on project root folder and configure required sections with your project details. Then add required dependencies one-by-one between tags (This will be the most time consuming issue if there are tens or hundreds of JARs). If maven global repository has required jar, it will be downloaded automatically, otherwise you must find required jar and run "mvn install" command to install that jar to maven repository:
pom.xml file
  • Run mvn-install or mvn-package commands for the project. Created JARS, web.xml, other resources and class files will be located under /target folder automatically. Also a WAR file for the project will be created. You can copy WAR file to server (e.g. apache tomcat) and run the application, or you can run the application using the server defined in eclipse. 
target directory

  • Depending on the running style of mvn-install command (by plug-in, command etc.) , created JARs under the /target folder may not be copied to src/main/webapp/WEB-INF folder. If you will run the application using the server of the ide, you may be needed to copy created libs into src/main/webapp/WEB-INF/lib folder and add those libs to project using Project Properties --> Java Build Path --> Libraries --> Add JARs section for once.
Adding required jars under webapp/WEB-INF/lib

Posted in , | Leave a comment

A Look Into Java Annotations & A Real World Spring Example

An "annotation" is a type of programming language definition and used as a “marker”. They can be thought as comment lines which programming language engine can understand. They don’t directly affect program execution but affect indirecly if wanted.


An annotation is defined with @interface keyword and is similar with an interface. It has attributes which are defined like interface methods. Attributes can have default values. Let’s define an annotation named “Page”, which defines UI pages of an application:

public @interface Page {
    int    id();
    String url();
    String icon() default "[none]";
    String name(); default "[none]";


Annotations are widely used to inform compiler or compile-time/runtime/deployment-time processing.
Usage of an annotation is simpler:

@Page(id=1, url=”studentView”, icon=“icons/student.png”, name=”Students”)
public class StudentWindow extends Window { … }

Annotations can also be defined for methods and attributes:

public String getElementName() {…}

@AnAnnotation(type=”manager”, score=3)
public int income;


1)    Reflection/code generation:

Methods having a specific annotation can be processed at runtime:

public @interface MyAnnotation { ... }
public class TestClass {
    public static method1() { ... }
    public static method2() { ... }
    public static method3() { ... }

public static void main(String[] args) {
    for (Method method : Class.forName("TestClass").getMethods()) {
        if (method.isAnnotationPresent(MyAnnotation.class)) {
            // do what you want

2)    Spring bean configuration (this section requires Spring bean configuration knowledge):

Let’s use our “Page” annotation again:

package com.cmp.annotation;
public @interface Page {
    int    id();
    String url();
    String icon() default "[none]";
    String name(); default "[none]";

Say that we have a few classes having @Page annotation in a package:

@Page(id=1, url=”studentView”, icon=“icons/student.png”, name=”Students”)
public class StudentWindow extends Window { … }

If we define a bean configuration as below in a Spring application-context.xml file, Spring will create class instances “which has @Page annotation” placed in “given package”.

<context:component-scan base-package="com.cmp.ui" annotation-config="true">
<context:include-filter type="annotation" expression="com.cmp.annotation.Page"/>

So, we have been enforced Spring to instantiate only a selection of classes at runtime.

For more detailed info about annotations, please refer to:

Posted in , | Leave a comment