Working With Maven Project in Oracle JDeveloper 11g

Working With Maven Projects In Oracle JDeveloper 11g

An Oracle JDeveloper How To Document

Written by Dana Singleterry, Oracle Corporation
Created November 2009

Introduction
Required Software
Downloading and Installing JDeveloper 11g, Java JDK 1.6 & Maven 2.1.0
Import a Maven Application into JDeveloper
Create JDeveloper 11g Application Configured for Maven
Conclusion

This document explains how-to work with Oracle JDeveloper 11g on Apache Maven-based projects. As an example I'll illustrate two possible scenarios. First I'll demonstrate importing a Apache Maven application from maven.apache.org and then I'll demonstrate creating an application in Oracle JDeveloper 11g configured for using Apache Maven technologies. The new application will include a project that is preconfigured to use Maven technologies.

Important to note is that this is a developers preview release of Apache Maven integration within JDeveloper 11g.

Introduction

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Maven's primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with:

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

Maven is essentially a project management and comprehension tool and as such provides a way to help with managing:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution

For further details on what Maven is and isn't, please visit the Maven Getting Started Guide. The Feature Summary page provides the key features of Maven in a nutshell. Apache Maven Project documentation is available online.

Much of Maven's power comes from the standard practices it encourages. Time need not be wasted reinventing directory structures, conventions, and customized Ant build scripts for each project. Although you can override any particular directory location for your own specific needs, it's best to stick with the standard Maven 2 directory structure as much as possible, for several reasons:

  • It makes your POM file smaller and simpler
  • It makes the project easier to understand
  • It makes it easier to integrate plug-ins

The directory layout expected by Maven and the directory layout created by Maven follows. You should try to conform to this structure as much as possible; however, if you can't these settings can be overridden via the project descriptor.

src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/filters Resource filter files
src/main/assembly Assembly descriptiors
src/main/config Configuration files
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
LICENSE.txt Project's license
README.txt Project's readme

At the top level files descriptive of the project: a pom.xml file (and any properties, maven.xml or build.xml if using Ant). In addition, there are textual documents meant for the user to be able to read immediately on receiving the source: README.txt, LICENSE.txt, etc.

There are just two subdirectories of this structure: src and target. The only other directories that would be expected here are metadata like CVS or .svn, and any subprojects in a multiproject build (each of which would be laid out as above).

The target directory is used to house all output of the build.

The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.

Within artifact producing source directories (ie. main and test), there is one directory for the language java (under which the normal package hierarchy exists), and one for resources (the structure which is copied to the target classpath given the default resource definition).

If there are other contributing sources to the artifact build, they would be under other subdirectories: for example src/main/antlr would contain Antlr grammar definition files.

Required Software

JDeveloper 11g

Optional downloads

Apache Maven 2.2.1

Java JDK 1.6

Note that Apache Maven 2.1.0 comes with JDeveloper 11g via the extensions download. Java JDK1.6 is also indcluded with JDeveloper 11g. Both of these can still be downloaded separately and utilized independent of Oracle JDeveloper 11g.

Downloading and Installing JDeveloper 11g, Java JDK 1.6 & Maven 2.1.0

Download JDeveloper 11g and run the installer. Select the options that best fit your requirements for your development environment. Under most circumstances you will simply go with the defaults. Upon first running JDeveloper, select NO if prompted to migrate from previous versions. In addition, select the Default Role upon startup. After downloaded and running JDeveloper 11g you'll want to download and install the required Maven extensions for JDeveloper. To do so, from the JDeveloper "Help" Menu select "Check for Updates" from the context. There are two extension to install in the order listed and JDeveloper will require a restart after each extension is installed:

  • maven_bundle.zip
  • maven-j2ee_bundle.zip
Check for Updates

Check for Updates...

Optional

Download Java JDK 1.6 and run the installer.

Download the appropriate Maven 2.1.0 distribution and install as follows for Windows 2000/XP:

Windows 2000/XP

    1. Unzip the distribution archive, i.e. apache-maven-2.2.1-bin.zip to the directory you wish to install Maven 2.2.1. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-2.2.1 will be created from the archive.
    2. Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-2.2.1. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven < 2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.
    3. In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
    4. Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
    5. In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
    6. In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
    7. Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.
Validate Maven Installation

Validate Maven Installation: Run mvn --version

Note: For optional installation settings/configuration please visit the Apache Maven installation page.

Import a Maven Application into JDeveloper

The first thing to do is run the Maven Archetype create to create a project with the proper directory structure and a pom.xml

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
Create Archetype

Create Archetype

You will notice that the create goal created a directory with the same name given as the artifactId. The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

pom.xml

For details of what you just did please refer to the Maven Users Centre The 5 minute test.

In the rest of the steps of this section I'll demonstrate Maven integration within JDeveloper 11g.

  1. Select File | Import from the JDeveloper Menu.
Import Application

Import

  1. Select what you want to import. In this case "Source into New Project".
Source

Select source into NewProject

  1. Provide a Application Workspace name or go with the default.
Name Application

Provide a Application Workspace Name

  1. Provide a Project name or go with the default name.
Name Project

Provide a Project Name

  1. Select the content that you would like to import. You are importing the Maven Application  that you previously created in the directory c:\mvnProjects. This is your application content including your pom.xml.
Add Java Source

Select content to import

  1. Add additional content into "Java Source Paths" as required. Select "Include content from Subfolders" and ensure "Copy files to Project Directory" is selected.
Import Source

Import Source into New Project: Ensure "Copy Files to Project Directory" is selected

  1. Add additional Libraries as required.
Source Default

Import source into New Project: Add additional libraries as needed

  1. Review the summary page of what you will import.
Wizard Summary

Summary Import Page

  1. Review the imported Maven project.
Workspace

JDeveloper Application Workspace

  1. Right select the pom.xml file and select "Manage Goals".
Manage Goals

Manage Goals

  1. Shuttle additional Goals from Available to Selected so that the options are available from context upon choosing to run goals when right-selecting on the pom.xml and seleting the goal of your choice from context.
Maven Project Properties
Maven: Goals
  1. Optional Maven Configuration
  • Here you can Customize your Maven Setting, Specify your Maven Version, Add Proxy to Settings if you're behind a firewall and more. In addition, the Maven extension that was downloaded and installled earlier is version 2.1.0. If you prefer you can work with a more recent release of Maven such as version 2.2.1 and here is where you would specify that newer Maven Version with your Project Properties in the MAven Project Settings.
Maven Project Properties

Maven Settings

  • Here you can customize the Maven Process as needed. Add Java Options, change the Java SE Version, specify the Working Directory and more.
Maven Project Properties

Maven: Process

  • Here you can specify additional Maven Settings and customize as needed. Review the available options.
Maven Project Properties

Maven: Settings

  1. Right select the pom.xml and select Run Goal(s) from context. Choose the goal that you would like to run. For detailed information on the various goals please refer to the Apache Maven Build Lifecycle Page.
Maven Project Properties

Run the Goal of your choice

  1. Run the package goal to take the compiled code and package it in its distributable format, sucha as a JAR.

Run package goal

Run the package goal

  1. Ensure the build is successful as illustrated below.
Package goal results

Review the package goal completed successfully

This section has demonstrated importing a pom.xml from a Maven Application. You may also want to review the application code provided within App.java and AppTest.java. This is but a simple HelloWorld application and this theme will be utilized further in the next section where I'll create a JDeveloper 11g HelloWorld Application Workspace configured to leverage Apache Maven. This section has touched on some of the Apache Maven features available within JDeveloper 11g and more features will be demonstrated in the following section.

Create JDeveloper 11g Application Configured for Maven

The first thing to do is to create a new application workspace in JDeveloper. Here are the steps:

  1. Create a new application workspace either from the File menu or Application Navigator within JDeveloper. Provide a name or go with the default.
Create Workspace

Create a new Application workspace and select the Maven Application Template.

  1. Name the project or go with the defaults. Ensure that the Maven Project Technology is included by default.
Name Project

Name the project or go with the default

  1. Configure the Java settings by providing a default package name if desired.
Configure Java settings

Configure Java settings

  1. Configure your Maven settings as required. Note that selecting the checkbox for "Modify Normal Project Structure to match the Default Maven Structure (Recommended)" will override the values set in step # 3 above.
Configure Maven settings

Configure Maven settings

  1. Create a new java object by right selecting on your project and selecting New from context.
Create New Java Object

Create a New Java Object

  1. Select "Java Class" followed by OK.
Create New Java Object

Create a New Java Object

  1. Give your java class an appropriate name and select Main Method below if you want a quick method of testing your application.
Name Java Object

Name your Java Class

  1. Open your pom.xml and begin to enter a new tag element such as packaging and you will notice Code Insight is provided on your pom.xml file.
Code Insight

Code Insight provided on your pom.xml

  1. Review the contents of your pom.xml
<?xml version="1.0" encoding="windows-1252" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>my-App</name>
  <url>http://maven.apache.org</url>
  <build>
    <sourceDirectory>src/main/java/</sourceDirectory>
    <outputDirectory>target/</outputDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>
            1.6
          </source>
          <target>
            1.6
          </target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

pom.xml

  1. Configure your Maven Project Properties as illustrated in the previous section. Here I'm adding some Goals in addition to the default one of package. Save your workspace.
Configure Project Settings

Configure Maven Project Properties

  1. Right select on your pom.xml and selet "Run Goal(s)" from context followed by selecting the package goal.
Run Package Goal

Run the package goal

  1. Review the status of running the package goal to ensure that the build was successful.
Review Build Message

Review status of running the package goal

  1. Run the application and see the results.
Run Application

Run application

This section has demonstrated creating a Application Workspace in JDeveloper 11 g configured for Apache Maven. You may also want to review the application code provided within App.java . This is but a simple HelloWorld application as in the first section. This section has touched on some of the Apache Maven features available within JDeveloper 11g as before with some additional features of Code Insight within the pom.xml and more.

Conclusion

Maven is a powerful build tool with many unique capabilities. Maven Integration within JDeveloper 11g includes the following, most of which has been demonstrated in this How-To.

  • Import POM.xml
  • Create POM.xml for project, application
  • Configure, run Maven goals
  • Create, reuse Maven templates (archetypes). For details on Archetypes, refer the Apache Maven Archetypes Page.
  • Configure Maven version, repositories, command line options, environment variables,...
  • Specify dependencies from repository
  • Code inside within POM.xml
For further information on Oracle JDeveloper & Apache Maven please visit the following sites.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy