What it covers?
Here we shall tell you about creating a web project using maven, and then importing it in eclipse.
Sometimes, it is time consuming to perform even simplest of things if there are multiple steps involved. So here is a quick guide of
- How to create a Web Project using Maven
- How to run this project in Eclipse
Creating maven web project
If you have created a maven project using "archetype", you would know that you need to enter a lot of parameters. This does the work for you, but it is lot difficult to remember and a lot more error prone.
So here is a small command to make a lean maven web project for you. Run this command in your workspace folder.
groupId
It is a unique name for all your projects. Typically it follows your organization name.
artifactId
The name of your project, and ultimately of your war file.
version
A version number for your application
package
We keep each project in a package. I used the default here.
And thus completes our Maven web project.
Now prepare the maven plugin to be imported to eclipse
Run this command from the project root directory. In my case it is "MyWebProject"
Now to import web project into eclipse do the following
Finally to make sure everything went well,
So use this command
If you feel I have made a mistake, or I should cover something more, please do drop me a note at my email harsh.curse@gmail.com.
This is something I observed on Eclipse Kepler. It somehow doesn't ship maven dependencies when it builds project and pushes it to server.
However when we build project using maven, the dependency is injected to WEB-INF/lib folder.
Here is the workaround
This should fix the problem.
Thanks,
Harsh
It will download a lot of stuff and then ask you for regular information. Here is how it goesmvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
Define value for property 'groupId': : com.harsh.samples Define value for property 'artifactId': : MyWebProject Define value for property 'version': 1.0-SNAPSHOT: : 1.0 Define value for property 'package': com.harsh.samples: : Confirm properties configuration: groupId: com.harsh.samples artifactId: MyWebProject version: 1.0 package: com.harsh.samples Y: : YHere is a small recap of the questions asked
groupId
It is a unique name for all your projects. Typically it follows your organization name.
artifactId
The name of your project, and ultimately of your war file.
version
A version number for your application
package
We keep each project in a package. I used the default here.
And thus completes our Maven web project.
Import in eclipse
We first need to install a maven plugin in eclipse to continue. I use m2eclipse maven plugin to get my work done. Or better, download Spring Tool Suite - it comes packaged with most of the plugins you would use.Now prepare the maven plugin to be imported to eclipse
Run this command from the project root directory. In my case it is "MyWebProject"
The above command will convert a Java maven project to support eclipse. But for webapp, we domvn eclipse:eclipse
This command creates .project, .settings files for eclipse.mvn eclipse:eclipse -Dwtpversion=2.0
Now to import web project into eclipse do the following
File > Import > Existing Maven Projects > Browse to the root folder of project > Import.
Finally to make sure everything went well,
Open context menu > Maven > Update ProjectTa - da. Run it on server using eclipse.
Bonus: Un-import project from eclipse
Highly unlikely, but still possible - that you get fed up and decide not to use eclipse.So use this command
This will clean all the eclipse thingys generated by maven, and you can delete your project from eclipse.mvn eclipse:clean
If you feel I have made a mistake, or I should cover something more, please do drop me a note at my email harsh.curse@gmail.com.
Error: ClassNotFound - But I have it in maven dependencies
It may so happen that a JAR that you included in maven dependencies is not available when you run your project on a server through eclipse. You get an error ClassNotFound.This is something I observed on Eclipse Kepler. It somehow doesn't ship maven dependencies when it builds project and pushes it to server.
However when we build project using maven, the dependency is injected to WEB-INF/lib folder.
Here is the workaround
Right click on project > Properties > Deployment AssemblyMaven dependencies is not available in the list. We need to tell eclipse to include maven dependencies when it creates WAR to be deployed on our server.
Click on "Add".You get a list.
Select "Maven Dependencies"Apply and OK.
This should fix the problem.
Thanks,
Harsh
Comments
Post a Comment