CI Tools and Best Practices in the Cloud

Continuous Integration

Subscribe to Continuous Integration: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Continuous Integration: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Continuous Integration Authors: Mehdi Daoudi, XebiaLabs Blog, Sematext Blog, Aater Suleman, Nate Vickery

Related Topics: Java EE Journal, Apache Web Server Journal, XML Magazine, Eclipse Platform, Continuous Integration

J2EE Journal: Article

An Introduction to Maven - Part III

Application development management using Maven 2 and Eclipse

In the parts 1 and 2 of this article, we demonstrated how to download and install Maven 2, how to install the Maven 2 plugin for Eclipse, and how to go about setting up a project directory structure using Maven 2. We used a simple use case for displaying employee details on the Web given an employee ID, but deliberately made the design a bit complex by introducing design concepts such as XML binding, EJBs, and JCA connectors to illustrate a few of the many features offered by Maven. In this final installment of the article, we continue with the remaining modules in our example and illustrate a few more developmental tasks that can be accomplished fairly easily using Maven that otherwise would demand significant time and effort to accomplish.

As described earlier, the 'connector' module yields a RAR artifact containing a JCA connector that uses 'xmlBinding' classes to return Employee information. This means that the 'connector' module will have a dependency on the 'xmlBinding' artifact and any artifact that can provide JCA classes. We'll use Maven's 'Add Dependency' feature in Eclipse to add these dependencies as described below.

1.  In the Eclipse 'Package Explorer' pane, right-click on the 'connector' module's POM file and in the menu, select 'Add Dependency' Maven2 option as shown in Figure 1.
2.  A dialog window to search for artifacts in the Maven repository will be displayed. In the search box, type 'com.somecompany.' The search results will be displayed in the text area as shown in Figure 2.
3.  Expand the 'com.somecompany xmlBinding' result entry, select the 'xmlBinding-1.0.jar' option, and click the 'OK' button. The dependency on the 'xmlBinding' artifact will be added in the POM file.
4.  To add JCA specification classes, we'll use a Geronimo JCA specification artifact as a dependency. Following the same approach as described before, search for 'geronimo' in the repository search window and select 'geronimo-spec-j2ee-connector-1.0-M1.jar' as a dependency to be added as shown in Figure 3.
5.  The two dependencies will be added to the POM file. The scope element should be added to the 'geronimo-spec-j2ee-connector' dependency element with a value of 'provided' indicating that JCA classes will be provided by the underlying J2EE application server. Below is the modified POM.

<project>
    <parent>
       <artifactId>EmployeeInfo</artifactId>
       <groupId>com.somecompany</groupId>
       <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>connector</artifactId>
    <packaging>rar</packaging>
    <name>connector</name>
<dependencies>
    <dependency>
       <groupId>com.somecompany</groupId>
       <artifactId>xmlBinding</artifactId>
       <version>1.0</version>
    </dependency>
    <dependency>
       <groupId>geronimo-spec</groupId>
       <artifactId>geronimo-spec-j2ee-connector</artifactId>
       <version>1.0-M1</version>
       <scope>provided</scope>
    </dependency>
   </dependencies>
</project>

It's time to add connector implementation Java classes. The source files that we developed for the connector are shown in Figure 4.

Download and review the source files to understand the complete implementation. However, the important classes to note are 'EmployeeInfoCCIConnection,' 'EmployeeInfoCCIInteraction,' and 'EmployeeInfoSPIManagedConnection.' The sequence diagram as shown in Figure 5 gives a high-level view of the method calls that a client will invoke to retrieve employee information using an employee ID. For brevity's sake, the sequence diagram is kept simple.

The RAR artifact for the 'connector' module can be created by adding 'maven-rar-plugin.' By default, this plug-in will look for RAR meta-information such as the 'ra.xml' descriptor file under the 'src/main/rar/META-INF' directory. The 'ra.xml' descriptor is in Listing 1.

Static Analysis of Code using Maven
Errors in the code are typically detected via code reviews, unit testing, system testing, integration testing, and user-acceptance testing. Code reviews certainly help in early bug detection by enforcing language-specific programming standards and best practices. However code reviews are carried out manually and hence the process can be cumbersome and inefficient particularly in case of large projects. Static analysis is a tool-based automated code review mechanism typically used to find code defects early in the build phase. Static analysis ensures early bug detection and remediation by comparing source code with predefined language patterns. It also helps in enforcing coding conventions and thereby improves code quality.

PMD is a static analysis tool for Java code. PMD packages a number of ready-to-run rules that can identify unused variables, unnecessary object creation, and empty catch blocks in the source code. Custom rules can also be incorporated. PMD can be executed using Maven by including 'maven-pmd-plugin' in the POM file as shown in the following snippet. The PMD plug-in lets you automatically run the PMD code analysis tool on your project's source code and generate a site report with its results. For more information on the PMD Maven plug-in, refer to plug-in documentation available at http://maven.apache.org/plugins/maven-pmd-plugin/.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
       </plugin>

PMD plug-in can be invoked at the command line within the module directory by running the command below. However, with the current implementation, the plug-in will generate reports only in case of 'jar,' 'war,' and 'ejb' POM packaging types. Since the current POM is a 'rar' packaging type, the packaging type should be temporarily changed to 'jar' so that PMD plug-in can generate the reports.

      mvn pmd:pmd

When this command is executed after temporarily changing the packaging type to 'jar,' Maven will invoke the PMD plug-in that will run the code analysis and create reports under the 'target/site' directory with the main report in the 'pmd.html' file. Figure 6 is the report generated for the 'connector' module source code. Make sure to reset the packaging type back to 'rar.'


More Stories By Murali Kashaboina

Murali Kashaboina leads Enterprise Architecture at United Airlines, Inc. He has 15+ years of enterprise software development experience utilizing a broad range of technologies, including JEE, CORBA, Tuxedo, and Web services. Murali previously published articles in WLDJ and SilverStream Developer Center. He has master’s degree in mechanical engineering from the University of Dayton, Ohio.

More Stories By Geeth Narayanan

Geeth Narayanan is a senior architect at Ecommerce Technology, United Airlines, Inc. He has 10 years of experience in the IT industry, specializing in solutions using Java EE technologies. Geeth has master's degree in electrical engineering from the University of Toledo, Ohio.

Comments (6) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Aladin SOHAILI 10/06/07 04:38:08 PM EDT

Any link to download source files ?

Bob Arnold 09/11/07 02:34:39 PM EDT

Regarding:

"It's time to add connector implementation Java classes. The source files that we developed for the connector are shown in Figure 4.
Download and review the source files to understand the complete implementation."

Please specify the download link for the source code referred to.

vinny 09/04/07 08:40:13 AM EDT

Where is the link to download source files for connector EmployeeInfoCCIConnection??

Magne 09/04/07 03:25:58 AM EDT

It was with great interest I read these articles.
Article 3 refers to source files available for download.
Where can they be downloaded form?

-magne

Duty Editor 08/31/07 01:16:44 PM EDT

The Links to Parts I and II are to be found at the foot of the final page.

-Duty Editor

Jim 08/31/07 11:51:57 AM EDT

Next time you write part 3 of a three-part series, please include clickable links to the first two parts right there at the top.
Unless, of course, you want to keep the first two parts secret.