0

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.

starblue
  • 55,348
  • 14
  • 97
  • 151
IphoneDev
  • 129
  • 1
  • 1
  • 5

2 Answers2

0

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

mazaneicha
  • 8,794
  • 4
  • 33
  • 52
  • but i have a few folders that needs to be assembled and they are spread all over the place. also, i need some files to be filtered. man i have been trying to get this working for a few days now. any help will be great. @Ryan Stewart – IphoneDev Sep 27 '11 at 19:16
  • did you look at the plugin usage page (http://maven.apache.org/plugins/maven-war-plugin/usage.html)? It provides an example sufficient to get you started. And certainly SO community will help you through any problems if you post another question with your project structure, pom.xml and issues that you're having. – mazaneicha Sep 28 '11 at 03:00
  • General suggestion: create two separate maven projects - first, 'MyWebProject', that contains only your source code, and assembles classes into a mywebproject.jar file, and second, 'MyWebProject-war' - containing WEB-INF, resources, and webapp-related configuration, solely for creating a corresponding .war. – mazaneicha Sep 28 '11 at 03:09
  • assembly plugin seems to be a perfect candidate for gathering different folders and packing it into a .war file according to maven website. its creating the war file in my case except its not able to capture all the files and creating extra folders like maven folder under META-INF. that seems to the issue right now. @Ryan Stewart – IphoneDev Sep 28 '11 at 18:54
  • finally got .war cooking with the right format using maven-war-plugin. not sure why assembly plugin was having so many issues as it was including files that i was filtering with filesets. @Ryan Stewart – IphoneDev Sep 29 '11 at 01:20
0

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • Issue i am having presently is the .war file doesn't have the structure that ant script creates. when i try to filter things using filesets and try to put output under different folders, its not working. all the folders are being dumped under WEB-INF folder. another issue is under META-INF folder, its creating a maven sub-folder. I am thinking maybe i need to tinker with settings.xml under .m2 folder and change the behavior of maven. i searched a lot but did not find any useful info on creating custom maven projects. even sonatype book talks about it using filesets but its not working for me – IphoneDev Sep 27 '11 at 19:06
  • I am stepping back a little and want to take this 1 issue at a time. To start with, in the resultant war file created by maven, its including maven folder under META-INF even after i put this in pom.xml false as part of maven-assembly-plugin as shown here - – IphoneDev Sep 28 '11 at 18:20
  • I am stepping back a little and want to take this 1 issue at a time. To start with, in the resultant war file created by maven, its including maven folder under META-INF even after i put this in pom.xml false as part of maven-assembly-plugin. another thing is i have a few folders in my root application folder which are not part of classpath. these folders are not being pushed into .war file either. any suggestions here. I have read your comments regarding ant tasks but i want to see if i can get around it. @mazaneicha – IphoneDev Sep 28 '11 at 18:27
  • This is far too much to tackle each individual issue in the comments for an already-answered question. Try posting a specific question about this first issue. Here's a hint to get you started, though: the assembly plugin isn't what creates WARs. – Ryan Stewart Sep 29 '11 at 02:28