Venkata Ramireddy CH
Venkata Ramireddy CH

Reputation: 753

How to convert a web application project to maven project?

I am having a exsting dynamic web project. Now I am planning to maven to this project. May I know how can I acheive that?

Upvotes: 9

Views: 15994

Answers (3)

Santosh Bansode
Santosh Bansode

Reputation: 21

There is no exact or official solution to convert an existing Java web project to a Maven based web application project. Basically, the Maven based project conversion will involve two major changes, which is :

Folder structure – Follow this Maven’s folder structure.
Dependency library – Put all your dependency libraries in pom.xml file.

Upvotes: 2

Amir
Amir

Reputation: 1061

First of all it's not good to change web project type once you have created one it's bad practice. You should either decide before you begin your project to do it as dynamic web project or with maven. When you do this it would help you to avoid lots of errors when converting project type to another and also when you use IDE to create for example Maven project it would build the base automatically based on your project configurations.

However it is possible to convert dynamic web project to Maven. Here's what you need to do:

If you are using Eclipse, you can simply do this by right-clicking your mouse on your project and choose from menu Configure -> Convert to Maven Project. If this doesn't work then you have to do it with hard way, which is:

  1. Create Maven project folder structure

    • You need to move all exiting java source files to \src\main\java
    • Move web.xml to \src\main\webapp\WEB-INF
    • Create pom.xml file and put it in your project root folder. (If for some reason you don't have above folders then you need to create them accordingly)
  2. Configure project details in pom.xml file. Here's an example of pom.xml file that has project details.

  3. Configure dependency libraries. This means that you need to add all libraries you use in your web project as dependencies in pom.xml.

  4. Compile your project with mvn compile command. To run this command you need to go to your project folder and then run it there. If you are using Windows then you must do this in command prompt, if you are using Linux, use Terminal.

  5. (Optional) If you want to make your project support Eclipse IDE then run following command mvn eclipse:eclipse -Dwtpversion=2.0.

  6. Now you need to generate .war file for purpose of deployment. To do this you must go again to your project folder using command prompt/Terminal and run following maven command mvn war:war. This will generate WAR file in your project target folder.

Upvotes: 11

nif
nif

Reputation: 3442

That's quite a generic question. The answer depends on what frameworks/libraries you are using for your web application, as there are different archetypes for various frameworks that will get you started easily.

However, you could begin with a minimal web application maven project using maven-archetype-webapp archetype and add your framework specific dependencies and configuration afterwards. Create the empty project structure by running maven:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

Running mvn package will build your project and package it into a WAR file. Copy your java code to src/main/java (create the directory if not present), which will be compiled and placed in WEB-INF/classes/ in the resulting WAR file. Files placed in src/main/webapp are directly copied to the root-directory of the WAR.

See this tutorial for an example using Tomcat and Spring.

Upvotes: 0

Related Questions