Browse DevX
Sign up for e-mail newsletters from DevX


Maven 2.0: Enterprise Peace of Mind Is All Part of the Package : Page 2

If you're a Java developer who hasn't yet experimented with Maven for managing enterprise Java projects, now is a good time to jump in. The 2.0 version of Maven, currently in beta, provides many significant improvements over previous versions. Follow along for a complete tutorial on the benefits of Maven.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Project Archetypes
Part of what makes Maven so powerful is its ability to facilitate the use of best practices. Maven facilitates this by providing an archetype plugin that can create bare bone projects for you from project templates. For example, if you are writing a Web application Maven can create a base project layout that you can build upon. Because most projects built using Maven integrate best practices, much of the build process can be automated.

Greeter Application—'Iteration 1
My first example will use the archetype plugin to create an application comprising multiple components. This simple J2EE application will say hello to users when they navigate to your Web site. You will expose your greeting logic in a service packaged in a JAR and your presentation logic in a servlet packaged in a WAR. This simple architecture is illustrated in Figure 1.

Figure 1. Greeter Architecture: The greeter application is a simple illustration of how multiple components may be related in an enterprise application.
Using the archetype plugin building these components is simple. First create a top-level directory named "greeter" to contain your projects. Next you will use the archetype plugin to create two projects, one for the WAR and the other for the JAR. To do so execute these commands from your top-level directory:

m2 archetype:create -DgroupId=com.devx.greeter -DartifactId=greeter-app
–DpackageName=com.devx.greeter m2 archetype:create -DgroupId=com.devx.greeter -DartifactId=greeter-web
–DpackageName=com.devx.greeter -DarchetypeArtifactId=maven-

The archetype plugin creates the directory structure illustrated in Figure 2.

Now I'll move on to the application service. Create the GreeterService class in the greeter-app project:

/greeter/greeter-app/src/main/java/com/devx/greeter/GreeterService.java package com.devx.greeter; public class GreeterService { public String sayHello() { return "Bonjour, tout le monde!"; } }

Figure 2. Greeter Directory Layout: The archetype plugin has created skeleton projects upon which you will build. Notice the standard directory layout.
Navigate to the greeter-app directory and run the 'm2 install' command. Presto! Maven compiles your classes, executes your unit tests, packages everything into a JAR, and installs it into your local repository where it will be available to other projects. You should see "BUILD SUCCESSFUL" printed to the console, indicating that everything ran successfully.

Next, you'll build the Web application that will display this friendly greeting to your users. In order to do this write a simple servlet and add it and its deployment descriptor to the greeter-web project.

/greeter/greeter-web/src/main/java/com/devx/greeter/GreeterServlet.java package com.devx.greeter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GreeterServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter writer = new PrintWriter(response.getOutputStream()); GreeterService service = new GreeterService(); writer.println("<html><body>"); writer.println(service.sayHello()); writer.println("</body></html>"); writer.close(); } } /greeter/greeter-web/src/main/webapp/WEB-INF/web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>greeter</servlet-name> <servlet-class>com.devx.greeter.GreeterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>greeter</servlet-name> <url-pattern>/greeter</url-pattern> </servlet-mapping> </web-app>

But how do you make the service you packaged a JAR accessible to your servlet? You must declare the greeter-app JAR as a dependency to the WAR's POM, as illustrated below.

/greeter/greeter-web/pom.xml <model> <modelVersion>4.0.0</modelVersion> <groupId>com.devx.greeter</groupId> <artifactId>greeter-web</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>servletapi</groupId> <artifactId>servletapi</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>com.devx.greeter</groupId> <artifactId>greeter-app</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>greeter-web</finalName> </build> </model>

Figure 3. Greeter Output: With very little effort you've created a J2EE-compliant application consisting of two projects with interdependencies.
Now navigate to the greeter-web directory and run the 'm2 install' command. This time Maven builds a WAR and even bundles the service JAR that was copied to the local repository when you built that project. But what if you don't want to have to separately build these two artifacts? You can create a parent project that defines these two projects as sub modules. When building the parent project Maven is smart enough to build these subprojects in the correct order (reverse dependency order). All we need to do to accomplish this is to create a new POM in the greeter top directory as illustrated below.

/greeter/pom.xml <model> <modelVersion>4.0.0</modelVersion> <groupId>com.devx.greeter</groupId> <version>1.0-SNAPSHOT</version> <artifactId>greeter</artifactId> <packaging>pom</packaging> <modules> <module>greeter-app</module> <module>greeter-web</module> </modules> </model>

To build the complete application, navigate to the greeter top directory and run 'm2 install'. Notice that Maven builds the JAR before building the WAR. Deploy the resultant WAR (/greeter/greeter-web/target/greeter-web.war) to your favorite Web container and test it out (see Figure 3).

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date