Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Tuesday, 2 April 2013

Creating an executable jar with a Maven plugin

While there are several non Maven based ways of creating an executable Jar that includes all required dependencies, Maven as a build tool offers three plugins for directly achieving this result. The Maven Assembly plugin is the first of these options. Creating an executable jar is just one of the objectives of the Assembly plugin. It can be used to create distributions in various file formats such as the zip format.
To configure the Maven Assembly plugin to create an executable jar with all required dependencies included, add the following configuration definition to your pom.xml in the build section.



   ..........
   
   maven-assembly-plugin
   2.4
   
     false
         standalone-${artifactId}-${version}
     
       jar-with-dependencies
     
     
        
           com.data.john.MainClass
           true
        
     
   
     
        
           make-assembly 
           package
           
              single
           
        
    
 
.............
.............


The second option is the Maven Shade plugin. The primary objective of this plugin is to create an executable jar and so it is much more suited for this use case. The configuration setup is as follows:



.................   

    org.apache.maven.plugins
    maven-shade-plugin
    2.0
    
        
             package
            
                 shade
            
            
            standalone-${artifactId}-${version}
            
              
              *:*
              
               META-INF/*.SF
               META-INF/*.DSA
               META-INF/*.RSA
              
             
            
            
                 
                    com.data.masker.controller.MaskerApp
                
            
           
        
    

..............




While tossing up between the Assembly and the Shade plugin, It should be mentioned that with using Maven Assembly, you run the risk of overwriting files with similar namespaces. These scenarios can be better handled using the Shade plugin, that provides more granular control over the build process by merging files with the same name instead of overwriting them.
Finally, this discussion would not be complete without mentioning the Maven Jar plugin, yet another way of  creating an executable jar with some use case examples given here.
All three options can be invoked using the mvn package call. 

Friday, 29 March 2013

Generating JavaDoc with Eclipse or Maven

Generating JavaDocs is an integral part of every Java project and there are various options of generating the required documentation.

Option 1 :  Using Eclipse and JDK's JavaDoc tool.

This is the simplest option. If you are using Eclipse and have the path to the JDK (not JRE) set in the Eclipse > Windows Preferences > Installed JRE option set correctly (as shown in the screenshot below), Eclipse should be able to find the JavaDoc tool.
Set the path to your installed JDK

JavaDoc Tool in Eclipse
That's it. Clicking on the Javadoc option will bring up a wizard that will prompt you for the Destination folder for the generated JavaDocs. Apart from generating the JavaDocs, the tool will also create a stylesheet.css for the JavaDocs that can be edited if required.

Option 2 :  Using Maven and the JavaDoc plugin.

Add the plugin definition to your pom.xml
               
                
                     ...........
                  
                  org.apache.maven.plugins
                  maven-javadoc-plugin
                  2.9
                  
                    C:/javadoc/stylesheet.css
                     public
                  
                  
                
                  


In the snippet above, I have added the plugin definition and configuration to the build section. It can also be repeated in the section. This will allow me to run the Javadoc generation goal during the build cycle. I have also specified a path to a stylesheet file that will define the look and feel of the JavaDoc generated.

To generate the JavaDoc using Maven, use mvn javadoc:javadoc

More details on this option is available here.




Sunday, 29 July 2012

Upgrade Hibernate from 3.1.3 to 3.6.10.Final using Maven

At the time of writing Hibernate is well into its 4.X releases with 5.X in sight so if you are looking to migrate from 3.x to 4.x read this or If you need a specific migration guide look here.

In my case I needed to migrate our application from 3.1.3 to 3.6.10.for which I have detailed the steps below.


  • Make sure that you have the necessary repositories to download the required Jars. You might need.


       mvn-public
       MVNRepository
       http://mvnrepository.com


       repository.jboss.org-public
       JBoss.org Maven repository
       https://repository.jboss.org/nexus/content/groups/public
  


  • Add the following dependencies into your pom.xml
   
  
   org.hibernate
   hibernate-core
   3.6.10.Final
  
  
  
   javassist
   javassist
   3.12.1.GA
  

Since Hibernate 3.4.X onwards, Hibernate switched to SL4J, you'll need:


   org.slf4j
   slf4j-log4j12
   1.5.6
  
  
   log4j
   log4j
   1.2.16
  

Now, configure your connection poolDownload the c3p0-0.9.1.2.jar from here and load it into your Tomcat/lib along with your MySQL driver jar.


Add the following configuration settings in your Hibernate (.properties or .cfg.) file.

5
20
300
50
3000

That's it !!
You WAR/lib should have the following jars present post-migration.
  • commons-collections-3.1.jar
  • hibernate-commons-annotations-3.2.0.Final.jar
  • hibernate-core-3.6.10.Final.jar
  • hibernate-jpa-2.0-api-1.0.1.Final.jar

  • javassist-3.12.1.GA.jar
  • jta-1.1.jar
  • slf4j-api-1.5.6.jar
  • slf4j-log4j12-1.5.6.jar


Friday, 25 November 2011

Install JFreeChart 1.0.14 with Maven

After 2.5 years, JFreeChart came out with a new release of the toolkit on Nov 21,2011.Version 1.0.14 promises bug fixes, improvements in the TimeSeries graphing and much more. Over the last few years, JFreeChart has been a popular open source choice for charts and graphs in Java.  The previous version has been download close to 430,000 times and while there are not many examples available describing the usage of the toolkit, installing it and implementing it in a Java application is relatively pain free.
Currently, the Maven repositories have not been updated with this release and you will need to manually install it into your local repository. To do so:

  • Download the latest release from the source-forge download page. Unzip the jar onto your local file system to any directory, say C:\jFreeChart\jfreechart-1.0.14
  • Open a command prompt in the directory where you have your Maven POM located and invoke the following command from the command line:
  • mvn install:install-file -Dfile=C:\jFreeChart\jfreechart-1.0.14\lib\jfreechart-1.0.14.jar -DgroupId=jfree -DartifactId=jfreechart -Dversion=1.0.14 -Dpackaging=jar
This should install jFreeChart into your local Maven repository. Now you can add a dependency to the Maven POM as follows:

     jfree 
     jfreechart
     1.0.14 
     compile 
    

Thursday, 10 November 2011

Deploying your web application to TomCat 7.0 using Maven

Deploying your web-application using the tomcat-maven-plugin can be done by as follows:
1. Create a user in Tomcat with the manager-script role. Before Tomcat 7.0, this was accomplished using the manager role. The user details can be sepecifed in the conf/tomcat-users.xml file as follows:
2. Now you need to tell maven that it can deploy applications to Tomcat using this role.
To set this connection, open your settings.xml file which should be present .m2 directory. Search for 'server' and put in the following credentials:

          localhost
          tom
          cat

Obviously, the server name is :localhost in this case.
3. In your maven pom.xml configure the tomcat-maven-plugin as follows:

             org.codehaus.mojo
             tomcat-maven-plugin
             
                http://localhost:8080/manager/text
                      localhost
                      /test
             
         

Take note of the
http://localhost:8080/manager/text
This is important otherwise you will keep getting 403:Access Forbidded errors when you attempt to deploy.
  • To build you war and deploy on Tomcat, use : mvn tomcat:deploy
  • To redploy your war, use : mvn tomcat:redeploy
  • To undeploy your war, use : mvn tomcat:undeploy
Troubleshooting : If you are getting connection refused : 401 errors while deploying, it means that most probably you have not setup your Maven settings.xml file correctly or that you have added the configuration parameters to the wrong file.

Saturday, 24 September 2011

Maven-ise your Eclipse (Indigo)

With the transition of the Maven2Eclipse plugin project from the Sonatype boys to the Eclipse umbrella, support for running Maven from within Eclipse has become smoother and definitely easier. To get started, install the Maven2Eclipse plugin from within Eclipse using the Help->Install new software menu option. The link to the plugin download is http://download.eclipse.org/technology/m2e/releases but verify the same from the  plugin site.
After successfully installing Maven, you will have access to several Maven project wizards that will help you get started with a Maven project with a few clicks as shown in the example below as I did for Eclipse Indigo.

  • Select a New project and choose Maven project. Click through the next few steps. You will get an option to choose the Maven archetype and specify the GroupId, Artifact Id, Version and Package






















  • After you select the required options, the wizard will generate a project file structure and populate it with some skeleton code including a Junit test.











  • That's it. You have created a Maven based project which is now awaiting functionality. Adding new dependencies, plugins to the project and the POM is made easy using the Maven project menu, available by right clicking on the project folder and selecting the required option.























  • A Maven Build can be set-up and initiated from the Run- As menu option. 













  • Selecting the Maven build option for the first time which will bring up a set-up configuration dialog that will enable you to specify the goals, profiles and set up any required parameters.

















  • Once the Run configuration is ready, you can build your Maven project and deploy your snapshot.

Saturday, 23 June 2007

John's essential links..for the saavy J2EE developer

We often come across useful information in our development phase and store it away as a book mark but we don't really index or summarise the information for later use and end up Googling for the same information when we need it again.

I intend to maintain this page as a collection of links related to various J2EE tools and technologies, that can be used for as a one stop shop for finding my favorite J2EE links regarding a particular technology or the solution to an issue I faced and sharing them with the J2EE community.

Feel free to send me your favorite links with a short summary of what is there on the page and I'll add it to the list.

----------------------
  • Build Tools

A. Maven


1. This article gives a good conceptual overview of the maven build tool and a handy startup command reference.
2. From the Maven home page, a starter's guide. This page has a handy faq but tries to focus on too many things and so might not answer all your doubts but it does give you something to go on.
3.Another good explanation of Maven concepts and a starter guide.

B. Ant

  • Java XML Serialization Technology

JAXB

1. A beginner's guide to JAXB.


  • Wikis and Blogging Tools

A. Versionate

B. MediaWiki

C. BaseCamp

  • Logging Tools
A. Log4J

  • Testing Tools
A. JUnit

B. Cactus

C. JMeter

D. SoapUI

  • Web Application Servers
A. Tomcat

B. JBOSS

C. WebLogic

  • Presentation / Web Tier
A. Struts

B. JSF

C. AJAX

D. CSS


  • Code Standardizing / Bug Finding Tools

A. PMD

B. findBugs

C. lint4J

  • Source Control Software
A. SubVersion

B. TortoiseSVN

Refer to this post for information, directions and tips on understanding, setting up and using Subversion and TortoiseSVN.
  • Useful Tools
Source Generation Tools

A. MiddleGen
B. XDoclet

Tailing Log Files : Cygwin

  • Frameworks
A. Spring

B. JEMS

  • Interesting APIs
A. Google Maps

1. This article has an excellent collection of Google Maps API usability examples.
2. This post summarises some of the other Google APIs, mainly the Google ToolBar API, Google Gadgets API, the Google Calendar API among others.

  • Setting Up / Configuring Tools / HowTos

My experience on building projects with Maven

I have been using Maven for the last couple of projects and have begun to realise the cool things that are possible. Claims that Maven is much more than a build tool and rather a project management tool appear justified.

Maven is best suited for setting up a project structure ( I mean the essential directory structure for a web based project) without minimum fuss. There are several public repositories such as http//www.ibiblio.com available for downloading project dependencies and associating them with the project. If your required jar isn't available, you don't need to fret. Download them from somewhere and update the ibiblio repository, so the next time anyone needs them, they can get them.

Another advantage as compared to it traditional rival ANT is that Maven conforms with the OO paradigm and supports the concept of a super class (read Parent POM). Low level tasks like code compilation are carried out by Maven without needing any additional tweaking from the developer.

Maven also breaks new grounds with its project site generation, reporting, source control management and well.. a lot of stuff. I could go on and on singing praises but hey, why not try something out for yourself.
This tutorial has a handy Maven command reference, to get you started.