A few months back I started using maven for a project, and since then my appreciation for this tool has been on the rise as I discover various feature and new plugins. Maven help out with software development process as well with design. Following are some of the benefits I have experienced.
Maven adds structure to your code repository. There is a place to put different artifacts (java source, properties files etc, JUnit test cases), this structure brings consistency to how code is organized in version control system.
Maven promotes modular design thru its dependency management feature. You can break up you project into different modules, and then build other modules on top of other modules. Maven can bring these modules together at build time. For example you could separate out DAO/Data model code create a maven module, then create another maven module for business logic which uses DAO/Data module, maven lets you specify the dependencies. This way you are maintaining a modular code and this will help you avoid mix up between your Data model class and business classes, lower layer modules are usable in multiple project if you need them.
Maven also helps you manage dependencies on 3rd party modules. you don't have to manually download tonnes of 3rd part jars, instead provide minimum information (artifactId, groupId, version) about 3rd party components, and the associated jars will be downloaded for you, you won't have to keep track hundreds of jars coming for dozens for 3rd part components.
Another advantage of of Maven is it lets developers work with different environments like QA, development, staging, production with ease. Settings for different environments could be specified in respective properties files (for example where is QA DB ?) , and at build time simply include the property file or apply these properties to various configuration file and other resources by simply specify a profile switch as one of the maven command argument.
Maven lets you run JUnit tests associated with you project.
Maven provides command line interface, and also works very well with popular IDEs like Eclipse and Netbeans, and with automated builds tools.
A wide range of maven plugins are available to do various task like compile, packaging, generating code and lot more. For example generating axis WSDL code from existing java sources, you could create various packaging foo you projects, execute SQL script againsts database, deploy your application. These links provide plugin descriptions http://maven.apache.org/plugins/index.html, http://mojo.codehaus.org/plugins.html
When you are starting with Maven the terminologies could be daunting but once you start using it, it is a pleasant song.
Tuesday, January 20, 2009
Monday, December 29, 2008
Java Heap Error in Maven JUnit Eclipse Environment
I encountered Java Heap Error (java.lang.outOfMemoryError) while running JUnit test cases using Maven plugin in Eclipse IDE. When maven build is run thru IDE, it forks another java process which kept running out of heap space after reaching only about 100M. After changing a number of different settings in IDE, Maven, OS environment variables and spending close to 8 hrs. Finally arrived at the solution.
First changed environment variable JAVA_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck.
Second changed MAVEN_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck.
Third changed eclipse.ini to have vm parameter -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck. This file is so fragile, making changes here breaks eclipse pretty bad.
Fourth through IDE , Run Configuration -> Maven Build provided VM parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m -verbose but no luck.
Fifth changed IDE setting for JDK to use vm parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck
Sixth option worked like charmed, in maven pom.xml added following setting for the plugin that helps you run JUnit test cases. Added following to .pom file.
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<version>2.2</version>
<configuration>
<argline>-Xmx1024m</argline>
</configuration>
</plugin>
First changed environment variable JAVA_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck.
Second changed MAVEN_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck.
Third changed eclipse.ini to have vm parameter -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck. This file is so fragile, making changes here breaks eclipse pretty bad.
Fourth through IDE , Run Configuration -> Maven Build provided VM parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m -verbose but no luck.
Fifth changed IDE setting for JDK to use vm parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck
Sixth option worked like charmed, in maven pom.xml added following setting for the plugin that helps you run JUnit test cases. Added following to .pom file.
<plugin>
<groupid>org.apache.maven.
<artifactid>maven-surefire-
<version>2.2</version>
<configuration>
<argline>-Xmx1024m</argline>
</configuration>
</plugin>
Saturday, December 13, 2008
mysql replication
MySQL provides ability to replicate a database in a Master/Slave fashion. All write and update operations are done on Master instance and are written to a binary log. This log is read by slave instances to replicate the changes. Replication is done in asynchronous mode, slaves keep track of the location within the log.
When would you use a replication ?
1) For high scalability, read off of slaves and writes on Master.
2) For backups.
3) For having a copy of data to resource intensive analytics without hindering master server performance.
When would you use a replication ?
1) For high scalability, read off of slaves and writes on Master.
2) For backups.
3) For having a copy of data to resource intensive analytics without hindering master server performance.
Hibernate OutOfMemoryException
When working with large number of Hibernate objects returned by a query, hibernate caches these objects in 1st level cache. To avoid running out of heap space use session.flush(), session.clear() and ScrollableResults.
Hibernate Interceptor
Hibernate Interceptor
By implementing Hibernate Interceptor interface or by implementing org.hibernate.classic.Validatable or by implementing org.hibernate.classic.Lifecycle , you can receive the various callbacks from a hibernate "session".
Using these callbacks you could inspect persistent objects before they are saved, deleted, updated or loaded.
<bean id="myInterceptor" class="com.company.MyInterceptor"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="entityInterceptor">
<ref bean="myInterceptor">
</ref>
</property></bean>
By implementing Hibernate Interceptor interface or by implementing org.hibernate.classic.Validatable or by implementing org.hibernate.classic.Lifecycle , you can receive the various callbacks from a hibernate "session".
Using these callbacks you could inspect persistent objects before they are saved, deleted, updated or loaded.
- Interceptor Approach
- Interceptor could be done at a session scope or at session factory level or at persistent class level.
- If its done at session factory level, interceptors must be thread safe, taking care to not store session-specific state since multiple sessions will use this interceptor (potentially) concurrently.
- At session level, one instance per session.
- To implement a Interceptor 2 ways
- Implement interface org.hibernate.Interceptor
- Or extend org.hibernate.EmptyInterceptor
- Once interceptor is implemented register it programatically with session or session factory
- Register the interceptor with Session
Session session = sf.openSession( new MyInterceptor());
- Register the interceptor with SessionFactory
new Configuration().setInterceptor( new MyInterceptor() );
- Integration with spring ?
<bean id="myInterceptor" class="com.company.MyInterceptor"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="entityInterceptor">
<ref bean="myInterceptor">
</ref>
</property></bean>
- Pros & cons
- Gives flexibility to do things at session and/or at entire application level.
- Could be a bottleneck if interceptor is being used at SessionFactory level, code inside interceptor has to be written very-2 carefully to make sure it is efficient. --- Things we may end up doing are logging changes to specific database tables in a database or to a log file(files is not a good idea).
- Would be nice if the interceptors could turned on and off based on configuration files, instead of programatically enabling it.? --- This could be achieved by modifying SPRING configuration file.(applicationContext.xml)
- To log for specific list of persistence entities there is no out of box configuration, one would have to create additional configuration parameters ?
Sunday, November 16, 2008
installing and using axis2 plugin
1) Download axis2 plugin from
http://ws.apache.org/axis2/tools/index.html
2) Extract plugin in eclipse/plugins directory
3) In eclipse IDE go to menu windows -> preferences
click on option web services -> Axis2 Preferences and specify location of folder where axis2 is installed.
4) To use axis2 plugin, goto project "properties" pane, and select "Project Facets" and select "Axis2 Web Services"
Now your project is ready to create a webservice using Java to WSDL style or from WSDL to Java.
To create a WSDL from java class, right click on class name and select "Web Services" -> "Create Web Service"
Other options options is to create the web service using File -> "New" --> "Other" -> Axis2 Wizards -> Axis2 Code Generator
http://ws.apache.org/axis2/tools/index.html
2) Extract plugin in eclipse/plugins directory
3) In eclipse IDE go to menu windows -> preferences
click on option web services -> Axis2 Preferences and specify location of folder where axis2 is installed.
4) To use axis2 plugin, goto project "properties" pane, and select "Project Facets" and select "Axis2 Web Services"
Now your project is ready to create a webservice using Java to WSDL style or from WSDL to Java.
To create a WSDL from java class, right click on class name and select "Web Services" -> "Create Web Service"
Other options options is to create the web service using File -> "New" --> "Other" -> Axis2 Wizards -> Axis2 Code Generator
InvocationTargetException in Eclipse Axis2 plugin
When generating WSDL code from a java class using Eclipse/Axis2 plugin version 1.4.1 an exception is thrown( java.lang.reflect.InvocationTargetException).
This version of plugin is missing 2 jar files ( backport-util-concurrent-3.1.jar, geronimo-stax-api_1.0_spec-1.0.1.jar) in lib directory. Here are the steps to get around this problem. The jar files could be copied from lib directory under Axis2 distribution.
1) Copy jar files to eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/lib directory
2) Add these jar entries to eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/plugin.xml
3) Restart eclipse IDE using -clean option. (modify eclipse.ini file)
More plugin problems can be dubugged using a cool technique mentioned in blog post
http://blogiterox.wordpress.com/2008/10/24/exploring-apache-axis2-and-eclipse-plug-in-development/
This version of plugin is missing 2 jar files ( backport-util-concurrent-3.1.jar, geronimo-stax-api_1.0_spec-1.0.1.jar) in lib directory. Here are the steps to get around this problem. The jar files could be copied from lib directory under Axis2 distribution.
1) Copy jar files to eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/lib directory
2) Add these jar entries to eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/plugin.xml
3) Restart eclipse IDE using -clean option. (modify eclipse.ini file)
More plugin problems can be dubugged using a cool technique mentioned in blog post
http://blogiterox.wordpress.com/2008/10/24/exploring-apache-axis2-and-eclipse-plug-in-development/
Subscribe to:
Posts (Atom)