Sunday, June 26, 2011

Creating a Maven based project


Open the terminal & go to the folder where you need to create the project.
Type the following command.

mvn archetype:create -DgroupId=com.maven.test -DartifactId=TestProject -DarchetypeArtifactId=maven-archetype-webapp

Here DgroupId means the group ID of the project & artifact Id is the project name. you can define both these & remember to give groupId seperated by the dot(.). so the output would be like this.

This will create a nice looking folder structure :)
Within your project folder you can see a POM file & src folder. before opening this using netbeans you need to add another folder named "java" inside main folder.so your main folder would look like this.
Now you can add the java, jsp, servelts, as required & also you can use any ORM like openJPA or Hibernate.


Now open the pom.xml file in the project folder. I will tell about pom.xml more in my next post. Till then this quick guide will help you out to develop a maven project :)
Initially your pom.xml would look like below.
Here you need to define the dependencies as per your requirements. for example of you are using Spring & Hibernate you need to add all the dependencies here. These dependencies will be the jar files & the libraries in the project.

After that you can open the maven based project using Netbeans.
1. Go to Netbeans & go to Open Project menu
2. browse for the location of the project & you will see ma symbols in front of the project name.
3. open the project

Then your project folder in the Netbeans would be look like this.

Then you need to build a war file in order to run the project. I am using tomcat as my server.
Now type the below to clean & build a war file
mvn clean install

Then goto your project folder & you will see a new folder called target is created & open the folder & you can see a .war file is created. (TestProject.war)
Copy this .war & goto web apps folder  in tomcat & paste it.
You can do this using tomcat manager as well. There you will find at the bottom  a place to browse for .war file & upload. you can do it either ways :)

now you need to compile the project using mvn compile & type the url of the home page of your project to run the project.
Now you will see the output like this :)
So thats it. Follow the steps & enjoy maven developments :)

2 comments:

  1. Hi.. I have a problem, if you can help me.. How can I add a library (Bouncy Castle .jar) to a maven based project already created?.. ( it's littleIms project, if you know anything about it).. Thanks a lot, in advance :)

    ReplyDelete
    Replies
    1. Hi Lavinia, You can add your jar file as a dependency to your pom.xml file. your pom.xml can be found in the project librabry & add the following dependency to the pom.xml

      < dependency >
      < groupId >bouncycastle< /groupId >
      < artifactId >bcprov-jdk14< /artifactId >
      < version >138< /version >
      < /dependency >


      make sure the version is same or above. if not you can search for the dependencies here.
      http://mvnrepository.com/artifact/bouncycastle/bcprov-jdk14/138.
      Hope this will help you..

      Delete