4

I am creating a web project and I was told that it has to reside inside the resources directory of an existing maven project

Here is the structure of the project

MavenProject
  |-- src
  |   |-- main
  |   `-- resources
  |       `-- My-Web-Project
  |           |-- META-INF
  |           |    `-- MANIFEST.MF
  |           |-- src
  |           |   |-- classes
  |           |   |   |-- com
  |           |   |   |   `-- example
  |           |   |   |       `-- projects
  |           |   |   |           `-- SampleAction.class
  |           `-- web
  |               |-- css
  |               |-- css
  |               |-- img
  |               |-- js
  |               |-- WEB-INF
  |               |   `-- web.xml 
  |               |-- index.jsp
  |               `-- secondary.jsp
  |-- test
  `-- pom.xml

As you can see there is already a pom.xml file for the MavenProject. I want to be able to deploy my web project WAR using the current pom.xml.

I want to know if it's possible to do this and how would I proceed to create the Maven Plugin.

I read this article but I don't know if it apply to my situation http://maven.apache.org/plugins/maven-war-plugin/usage.html

EDIT:

I am using tomcat app server.

As Mentioned before, My project "My-Web-Project" should be under "resources"

locorecto
  • 1,178
  • 3
  • 13
  • 40

2 Answers2

7

Read this document, incomplete, but it is good starter.

Using Maven When You Can't Use the Conventions

Sample configuration (not recommended)

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <webResources>
        <resource>
          <!-- this is relative to the pom.xml directory -->
          <directory>src/main/resources/My-Web-Project/web</directory>
        </resource>
      </webResources>
    </configuration>
  </plugin>

This is not complete, target and classes shouldn't be located inside source directory.


Maven convention - recommended solution

I was told that it (web project) has to reside inside the resources directory of an existing maven project

This is wrong, web project shouldn't be created inside resources!

Read about Maven Convention over Configuration.

Convention over configuration is a simple concept. Systems, libraries, and frameworks should assume reasonable defaults. Without requiring unnecessary configuration, systems should "just work".

Use convention instead of configuration.

Proper directory structure should looks like this:

MavenProject
   |-- src
   |   |-- main
   |   |   `-- java
   |   |       `-- com
   |   |           `-- example
   |   |               `-- projects
   |   |                   `-- SampleAction.java
   |   `-- resources
   |   |   `-- META-INF
   |   |       `-- MANIFEST.MF
   |   `-- webapp
   |        |-- css
   |        |-- img
   |        |-- js
   |        |-- WEB-INF
   |        |   `-- web.xml
   |        |-- index.jsp
   |        `-- secondary.jsp
   |-- test
   |-- target  // this is generated by maven!
   |   |-- classes
   |   |   `-- com
   |   |       `-- example
   |   |           `-- projects
   |   |               `-- SampleAction.class
   `-- pom.xml

Source: Introduction to the Standard Directory Layout

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • The problem is that I cannot create a new project. My project must reside inside resources. That is given and I cannot change that. My questions is: Can I create the structure you gave inside My-Web-Project and add deployment code in the MavenProject pom? – locorecto Jan 16 '14 at 22:09
  • Why you cannot? What is the real reason? New project is not needed, there are modules in Maven. – MariuszS Jan 16 '14 at 22:11
  • What should be the plugin xml? – locorecto Jan 16 '14 at 22:14
  • plugin.xml? I dont understand. – MariuszS Jan 16 '14 at 22:17
  • Proper solution doesn't requires any configuration. Configuration for not recommended solution added. – MariuszS Jan 16 '14 at 22:23
  • Why is it not recommended? The original maven project is not a web project is a java project. I need to add configuration to deploy the war file. – locorecto Jan 16 '14 at 22:30
3

What app server are you using? Most app servers already have its own maven plugin capable of deploying war so you don't have to write yourself: jboss-maven-plugin, tomcat-maven-plugin.

Also your folder structure doesn't comply with maven standards. Typically if you follow the standards and set your archive type into war, on package goal maven will automatically generate a war for you in the target folder.

gerrytan
  • 40,313
  • 9
  • 84
  • 99