converting existing web project using maven

2019-08-27 20:37发布

问题:

I have been trying to convert a web project that produces a war file to maven. my existing project structure is as follows -

MyWebProject | -- WEB-INF/src - contains a bunch of packages like com.myweb.client, com.myweb.server etc -- WEB-INF/test - contains 1 package com.myweb.tests -- web-scripts - contains bunch of scripts (just a folder; not on classpath) -- misc-files1 - contains misc files sets 1 -- misc-files2 - similar to above

presently a war file is being created using ant script with the resulting war file structure as follows

myweb.war - Meta-INF (only contains MANIFEST.MF) - WEB-INF - classes - com.myweb.client - com.myweb.server etc. - web-scripts - misc-files1 - misc-files2

i created a basic maven project using simple-artifact and added my packages. i am using maven assembly plugin to generate the war file and using filesets to filter. but my resultant war file is no where close to what i get with ant. here is a shortened version of my assembly.xml file

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

  <id>assembler</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
<dependencySet/>
</dependencySets>

<fileSets>

    <fileSet>
        <directory>src/main/java</directory>
        <outputDirectory>WEB-INF</outputDirectory>
        <includes>
        <include>**</include>
        </includes>        
    </fileSet>

    <fileSet>
        <directory>es</directory>
        <outputDirectory>resources1</outputDirectory>
        <includes>
        <include>**</include>
        </includes>        
    </fileSet>
    <fileSet>
        <directory>resources2</directory>
        <outputDirectory>resources2</outputDirectory>
        <includes>
        <include>**/*</include>
        </includes>        
    </fileSet>
    <fileSet>           
    <fileSet>
        <directory>test</directory>
        <outputDirectory>WEB-INF</outputDirectory>
        <includes>
        <include>**</include>
        </includes>        
    </fileSet>

  </fileSets>


</assembly>

To create a custom war, is assembly plugin the right approach. I can always include my ant script in pom using maven-antrun-plugin, but i want to avoid using ant if possible. any suggestions.

回答1:

You should be using maven-war-plugin for this.



回答2:

You're going entirely the wrong direction. Try some basic Maven webapp tutorials first to get a feel for the tool. Then you should see some clear parallels between your Ant build and the Maven lifecycle and default plugin bindings. When you're more comfortable, first let Maven handle all the stuff that it does out of the box, like compiling, testing, and creating the WAR. Try to move the dependency management to Maven, too. If you still need a classpath set up in Ant, use the Maven Ant tasks to let Maven build the classpath for your Ant script. Leave any custom stuff in Ant build files and invoke it using the antrun plugin from Maven until and unless you convert to using a Maven plugin for it.

Edit: Your problem seems to be that you have multiple directories containing source files that need to be bundled into the war. Is that right? The simplest solution would be to get them all into one place--src/main/webapp is the Maven convention. Even an Ant build should have at least this much organization to it. If there's a really good reason to have your war resources scattered all over (I'd like to hear it), then you can use some other plugin like the resources plugin, GMaven, or the antrun plugin to copy the multiple directories into the directory where the war plugin builds the war directory structure. That's known as the "webappDirectory" in the war plugin. The possibility of using the antrun plugin goes back to my original answer of invoking pieces of your existing Ant script from Maven. If this multi-directory thing has to stay and is already taken care of by Ant, refactor that into a target/task that can also be used from Maven. As much as possible, let the custom parts of your existing build be reused as you adopt Maven.