This posting continues the series on moving from an Ant to a Maven build.
The last stage is to actually move the Ant build to Maven. Your source tree is now quite spartan. It contains the web application, lots of configuration files, servlets or controllers, and non-core supporting classes. As before, you will create a new Maven project, establish dependencies, copy your files, and build and test until complete.
This project is a combination of webapp and Java but Maven does have an automated way of creating this. Instead, you need to first create the webapp project and then create the java tree. Create the webapp project
mvn archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-webapp \ -DarchetypeVersion=1.4 \ -DinteractiveMode=false \ -DgroupId=com.andrewgilmartin \ -DartifactId=system-application \ -Dversion=1.0-SNAPSHOT
Now create the Java tree
cd system-application mkdir -p \ src/main/java \ src/main/resources \ src/test/java \ src/test/resources
The result is
. ├── pom.xml └── src ├── main │ ├── java │ ├── resources │ └── webapp │ ├── WEB-INF │ │ └── web.xml │ └── index.jsp └── test ├── java └── resources
The pom.xml file is little different from those created before. The significant change is the <packaging/> element
The "war" value directs Maven to create the war instead of a jar (the default). Now add to the pom.xml the common and system-core dependencies, and any other dependencies specific to the application.
Your web application runs within a servlet container and that container provides some of your dependencies. You need these dependencies for compilation, but they should not be bundled into your war. Maven calls these "provided" dependencies. For these dependencies add a <scope/> element to your <dependency/> element, eg
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.0.15</version> <scope>provided</scope> </dependency>
Copy the application's code, configuration, and webapp from the system source tree to this project. Build and test as normal until you have clean results.
If you are interested in my help with your Ant to Maven transition contact me at firstname.lastname@example.org.