Working With Maven Projects In Oracle JDeveloper 11g

An Oracle JDeveloper How To Document

Written by Dana Singleterry, Oracle Corporation
Created June 2011

Introduction
Required Software
Downloading and Installing JDeveloper 11g, Java JDK 1.6 & Maven 2.2.1
Import a Maven Application into JDeveloper
Create JDeveloper 11g Application Configured for Maven
Additional Maven Features Provided in JDeveloper
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.

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 (11.1.2.1.0)

Optional downloads

Apache Maven 2.2.1

Java JDK 1.6

Note that Apache Maven 2.2.1 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.2.1

Download JDeveloper 11g and run the installer. Select the options that best fits 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. Once you'vedownloaded and started JDeveloper 11g you're ready to begin as you no longer need to download and install the Maven extensions for JDeveloper as Maven is NOW part of the JDeveloper package.

Optional

Download Java JDK 1.6 and run the installer.

Download the appropriate Maven 2.2.1 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.
  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 to the path.
  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.6.0_25 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. I'll create a project directory, mvnProjects, to run this in.

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <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. Create a new application workspace to import your Maven Project into. Note that it doesn't have to be a Custom Application since when you import the Maven Project, it will be configured accordingly.
 Create Application Workspace

Create Application Workspace.

  1. Provide an Application Name nd Application Package Prefix.
 Create Workspace

Provide an Application Name and select Finish. Note a default project will be created for you.

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

Import.

  1. Select what you want to import. In this case "Maven Project".
Maven Project 

Select "Maven Project".

  1. Provide a the Root Directory for your Maven Projects, select the project(s) you want to import, and select the checkbox Copy project(s) in application. Select OK.
Browse for Maven Application 

Select Maven Project(s) to import.

  1. Review the imported Maven project..
Review project 

Review imported Maven project..

  1. Right select the pom.xml file and select "Manage Goals". Notice that you can also Manage Apache Maven Settings which you can also do from Project Properties as illustrated below.
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.
Maven Project Properties 

Maven Project Properties.

  • 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.
Run Goals 

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.

  1. Run the application.
Run the application 

Run the application.

This section has demonstrated importing a pom.xml from a Maven Project. You may also want to review the application code provided within App.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 sections.

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 Application Workspace appropriately and provide an Application Package Prefix.
Workspace Name 

Name the Application.

  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 your Maven settings as required.
Configure Maven settings 

Configure Maven settings.

  1. An Application Workspace is created configured for a Maven project with a pom.xml.
Review Application Workspace 

Review the Application Workspace.

  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="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>mavenProj</artifactId>
  <p
  <version>1.0-SNAPSHOT</version>
  <description>Project for mavenProj</description>
  <build>
    <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>
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>MavenApp</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
</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 Properties 

Configure Maven Project Properties.

  1. Right select on your pom.xml and select "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 11g configured for Apache Maven. 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.

Additional Maven Features Provided in JDeveloper 11g

Some new Maven features that have been added to JDeveloper 11g include Project and POM dependency syncing as well as better Repository Managment. First a quick look at Project and POM dependency syncing.

Project and POM dependency syncing

  1. You select project properties and add libraries to your POM file. In this example, I'll add ADF Faces Runtime 11 libraries. Once you select ok on the Project Properties dialog, you'll notice that the selected libraries are added to you Maven repository such that POM can manage the project.
Dependency Syncing 

Add a ADF Faces Runtime 11 libraries.

  1. Once you select ok on the Project Properties dialog, you'll notice that the selected libraries will be added to your Maven repository such that the POM can manage the project.
Add libraries to Maven repository 

Add libraries to the Maven repository.

  1. Select the Export checkbox and ok to add the dependency.
Add dependency 

Add the dependency.

  1. Review the POM file to see that the dependency has been added.
  <dependencies>
    <dependency>
      <groupId>oracle.jdeveloper.library</groupId>
      <artifactId>ADF-Faces-Runtime-11</artifactId>
      <version>11.1.2.0.0</version>
      <type>pom</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>oracle.jdeveloper.library</groupId>
      <artifactId>JSF</artifactId>
      <version>2.0</version>
      <type>pom</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>oracle.jdeveloper.library</groupId>
      <artifactId>JSTL</artifactId>
      <version>1.2</version>
      <type>pom</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>oracle.jdeveloper.library</groupId>
      <artifactId>JSP-Runtime</artifactId>
      <version>11.1.2.0.0</version>
      <type>pom</type>
      <scope>compile</scope>
    </dependency>
  </dependencies>

pom.xml.

  1. Another scenario could be that a dependency was added directly to the POM file. In that case if you were to look at the Project Properties and Maven Dependencies you would notice the message in red in this image.
Project and POM dependency syncing 

Notice the alert that the project POM has dependencies that are not listed..

  1. Select Add from POM to add the library.
Add POM dependency to Project 

Add POM dependency to Project and review the POM file again to see that the dependency has been added.

  1. One last point here before moving on to another feature is that during the creation of a POM for a project, the dependencies from the project jpr will be copied to the POM file as expected..

Repository Management

Oracle JDeveloper now allows for better management of Maven repositories.
  1. Right select on the Application workspace and select Application Properties.
Application Properties 

Application Properties.

  1. Notice you can add additional remote repositories, change the local repository, as well as use custom settings. You can also add Goals, additional Dependencies as well as work with the Maven settings.
Application Properties 

Application Properties.

  1. Likewise you can right select on the Project and select Project Properties and have access to additional configuration properties such as the ability to specify the options for the Maven process, including the Java version and environment variables..
Project Properties 

Project Properties.

For further information on Oracle JDeveloper & Apache Maven please visit the following sites.

Conclusion

Maven is a powerful build tool with many unique capabilities. Maven Integration within JDeveloper 11 g 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.
false ,,,,,,,,,,,,,,,,