wood burning stoves 2.0*
The moose likes Ant, Maven and Other Build Tools and the fly likes Maven - Building a single war file from multiple modules Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Maven - Building a single war file from multiple modules" Watch "Maven - Building a single war file from multiple modules" New topic
Author

Maven - Building a single war file from multiple modules

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Maven - Building a single war file from multiple modules

I posted a question last week on how i can build dependent modules and the result was that it was recommended that i use a build tool like Maven or Ivy. I decided to use Maven to try and achieve what i would like to do. My project basically builds a single war file which is deployed to Tomcat. The modules themselves are part of the final war file. Here is an example structure of the modules







As you can probably see, the above will all collectively form one application. What i would like to do is to end up with an artifact for each module. Because of the mixture of file types(jsp,css,java) i am not quite sure what is a suitable artifact. The diagram below show the structure of the war file i would like to end up with.



A couple of notes
- The java files in commonModule are built and end up in WEB-INF/classes
- The java files for the customerModule end up as a jar file in WEB-INF/lib
- The java files for the productModule end up as a jar file in WEB-INF/lib
- All of the jsps,css end up in the root of the war file

What is the best way i should store the artifact for each module in the repository?

- I could use a jar file but this cant hold the html type files(jsp,css,js etc)
- Each module (except the commonModule) will contain a jar file + the jsp, css files. I am thinking of storing the built artifact as a zip file and extract it when the MyApp.war is built to build the war file?
- Can i store the artifact for each module as a war file even though only one has a web.xml file?

How can i implement this using Maven

- I have been investigating Maven and found that it might be possible to have a multi module project with the following structure

MyApp.pom (parent project)
commonModule.pom
customer.pom
product.pom

- If i zipped up the modules in the repository how would i refer to them from the parent project and unzip them to build the final war file?
- Is it possible to automatically trigger the build of the parent project if any of the child projects are built?
- I havent figured out how to maintain the version of each of the child projects. If i build the customer project, how does the parent project know that there is a newer version of the customer.zip (or .jar) in the repository?
- Assuming the repository currently contains the following



If i rebuild, the customerModule and then rebuild the war file buy building the parent project, does maven rebuild all of the other modules in the repository even if they have not changed? How exactly will this work?

Is there an example project anywhere that demonstrate how to achieve the above? Any links or resources which show an example of the above would be very usefull.

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

This looks more like you are attempting to build one application, but keep the sources in three different directory structures/projects. This isn't something that is built into Maven - you are going to have to do a lot of work to get this build. Essentially, you are using different definitions for "modules" than how Maven defines modules.

Are your "modules" dependent on each other? As an example, do the Java sources in productModule require the Java file in commonModule to build? If so, then building each "module" as a WAR is not going to work because a WAR doesn't work well as a classpath entry for a compile. So if they are dependent on each other, you will have tp merge the sources into one directory structure before doing the build. There arep robably other possibilities but they end up being more complex.


JBoss In Action
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

Having though about this for a bit, here is a possibility:

commonModule.pom, customer.pom, product.pom all do a similar thing - they use the Assembly plugin to create a zip file, which is registered in the build, and the resulting zip files are deployed to the remote repository. Most likely the "packaging" type in these POMs would be "pom" (using "zip" would be more obvious, but you would have to write a plugin to allow "zip" as a packaging value). Also, the build command line would be something like "mvn assembly:single deploy:deploy"

The MyApp.pom would have three dependencies, one of each of the zip files (the "type" in the dependency would be "zip"). You could use the Dependency plugin to get and unpack three zip file as part of the generate-sources phase, and you would want unpack them to a common directory within target, and add additional directives (using the Resources plugin if nothing else) to build or copy the files. This POM would have "war" as the packaging type.

The end result would be all of the class files would be in WEB-INF/lib. If you really want the JAR files for the two "modules", then that would be possibly but it would complicate the command lines for those two "modules".
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Thanks thats very interesting. I am investigating how to use the plugins you mentioned in your post.
 
 
subject: Maven - Building a single war file from multiple modules
 
Similar Threads
Unable to resolve dependency to a jar file created by MVN in 1st module.
Ear Building in Maven
Problem with the "mvn test" command missing EJB client jar artifact
Maven - Remove unused jars from repository
Maven: What's the big deal?