Wednesday, April 25, 2012

log4j.properties

Following is a sample log4j.properties, which output all logs to console and writes INFO+ level log to a file
#default log level is set to 'debug' it will output all log to console 
log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %5p %c{1}:%L - %m%n

#log level that is >=INFO, will use defaultLog definition for packagename
log4j.category.packagename=INFO, defaultLog

#define the log file directory
log.dir=/some/directory/logname.log

datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
roll.pattern.daily=.yyyy-MM-dd

#defines defaultLog, log file is rotated daily
log4j.appender.defaultLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.defaultLog.DatePattern=${roll.pattern.daily}
log4j.appender.defaultLog.File=${log.dir}
log4j.appender.defaultLog.layout=org.apache.log4j.PatternLayout
log4j.appender.defaultLog.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %m%n

Friday, April 20, 2012

Create Spring MVC maven project

Recently, I was working on a project that requires Spring MVC with maven. Following are the steps that guide you setting up the project. Before you start, make sure you have maven installed in your machine, or follow this link to maven download page.

Create a basic maven project 

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.mycompany.app -DartifactId=my-app

With couple other prompts such as choosing version number, maven should then create a basic maven project structure.


Create Web Application structure

open up the folder that maven has created, and under src/main, create two new folders, 'resources' and 'webapp'. Your folder structure should looks like following

./pom.xml
./src
./src/main
./src/main/java
./src/main/resources
./src/main/webapp
./src/main/webapp/WEB-INF
./src/test
./src/test/java
At the root of the directory structure is a XML file (always called pom.xml) that Maven expects. The pom.xml (POM is short for Project Object Model) describes the things specific to your project that can't be inferred automatically like dependencies, the name of the project, etc.

DirectoryDescription Directory's Contents (relative to the project root)
src/main/javaContains the Java source code for your project
src/main/resourcesContains any classpath-relative resources for your project (like, a Spring application context .xml file)
src/main/webappthe root web context folder for your web application files, such as WEB-INF, jsp,etc
src/test/javaContains the java source code for your test classes. This directory will not be included in the final build. All tests herein will be compiled and all tests will be run. If the tests fail, it aborts the build.




Modify pom.xml

replace pom.xml with http://codesfusion.blogspot.com/2012/04/spring-mvc-maven-pom_19.html which already includes the most basic dependencies for spring mvc project


Build your war

go to the root of your application folder
mvn clean package

this will build your project and package it into a war file, which you can then deploy to your web application server


Following is a complete spring mvc setup using the instructions above. You can download it and revise for your mvc application
Sample Spring MVC Project
To import the project into your eclipse, you can use m2eclipse for your Eclipse IDE. http://www.sonatype.org/m2eclipse/

Thursday, April 19, 2012

Spring MVC maven pom