How To Create EclipseLink Project Using Maven Archetype

This article should discuss the installation and configuration of EclipseLink-JPA using Maven dependencies and Maven archetype project creation principle. This article assumes that the machine that used for installation is already have installed a Maven. So the “mvn” command should works. This article will use an Eclipse Kepler release 1 as an Integrated Development Environment, so be sure that you have installed one. To install the Eclipse Kepler refer to the following Eclipse Kepler IDE.

Installation of Maven

Check if the maven has installed or not by using the maven command, “mvn -version”. The proper reply should be as shown at the figure 1.1. (Maven Windows and Maven Ubuntu)

Maven-Check

Figure 1.1

Create Project Using Maven

To create a Java project using maven, you should use the maven archetype. The Archetype is a template toolkit that used for creating a standard Java projects. Those templates are used to create several types of Java projects starting from a standard simple Java into very complicated Enterprise application.

Creating a maven Java project should follow these steps mentioned below:

  • Download the latest maven-archetypes-templates from an official “Github” site Maven Archetypes Zip File using the “Download Zip” link that’s appended at the right panel of the page.
  • Unzip the file inside an empty folder and navigate into quickstart-java-app folder and execute the following maven commands to install the template that will be used to create a simple standard Java project:
  1. Execute mvn clean archetype:create-from-project. The result of execution should be a new template that must be installed on your local repository to be used later.
  2. Navigate to “target/generated-sources/archetype” and execute mvn install to install the created template into your local repository.
  3. Navigate into an empty folder and execute mvn archetype:generate -DarchetypeCatalog=local -DgroupId=net.javabeat.eclipselink –DartifactId=EclipseLink-Installation -Dversion=1.0
  4. A list of local archetype will be listed if you have different archetypes that have been created.
  5. Enter the number beside of the archetype named “quickstart-java-app-archetype”.
  6. List all folders that created in a step 3 by using dir or ls based on your Os. You should see the artifact EclipseLink-Installation.
  7. The EclipseLink-Installation is a Java standard project that created by the maven. The created project contains your own named package that passed in the -DgroupId.

See the Figure 1.2 that shows you the created project and its structure.

MavenProjectStructure

Figure 1.2

Import Maven Project Into Eclipse IDE

Eclipse IDE provides the developer capability of importing a maven project through an eclipse driven wizard. Follow the following steps to import the project created previously.

  1. Open EclipseIDE.
  2. Right-Click at the “Project Explorer”.
  3. Choose import option and from the right menu click import.
  4. Expand the maven tree.
  5. Choose Existing Maven Projects. And click next.
  6. Navigate into your created project by using the browse and click ok. In our case the path is C:\JavaBeat\archetype.
  7. The Eclipse IDE should read the pom.xml and tell you with the name of the projects found there.
  8. In our case the Eclipse IDE will mention /EclipseLink-Installation/pom.xml. See Figure 1.3 as the only project there.
  9. Select the project & click finish.

The project created is imported successfully. See Figure 1.4

EclipseIDEImportMavenProjectWizardResult

Figure 1.4

Compile & Execute the Project

To compile the project, click on the project from the project menu and choose clean. The project must be compiled successfully. See Figure 1.5. Try to run using CTRL+Shift+x & j or by using the run from the menu.

ProjectCompilation

Figure 1.5

If you have noticed that the project execution doesn’t print out “Hello” followed by the entered name. Make sure the log4j.properties uses the correct rootLogger. If the logger uses “debug” while the rootLogger configured to use “info”; the logger will not behave as you expect and thereby it will not print any message. Let they use the same type of message. As you have noticed the logger in the Figure 1.5 uses the info method, so the rootLogger must also use the info.

EclipseLink Artifacts For Maven

Adding external libraries have never been easier than now. To add the EclipseLink-JPA library, you should add the following lines at your project’s pom.xml file.

<repositories>
   <repository>
     <id>oss.sonatype.org</id>
     <name>OSS Sonatype Staging</name>
     <url>https://oss.sonatype.org/content/groups/staging</url>
  </repository>
</repositories>

Also, you should add the required dependency within dependencies tag

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>eclipselink</artifactId>
   <version>2.5.0-RC1</version>
    <exclusions>
      <exclusion>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>commonj.sdo</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Configure persistence.xml

The persistence API requires a persistence.xml that is located under META-INF folder.

To be able to use the persistence API, you should follow the following steps:

  1. Create a folder under src/main/java called META-INF
  2. Create a file named persistence.xml.
  3. Copy the persistence.xml file that produced at the article Eclipse Link – JPA Installation and Configuration Using Eclipse into META-INF.

Testing Application

Add at your default generated HelloService Java class the code that uses the persistence API.

System.out.println(Persistence.createEntityManagerFactory("EclipseLink-Installation").isOpen());

See the Figure 1.6 that shows you the HelloService that uses Persistence API successfully.

TestingApplication

Figure 1.6

Comments

comments

About Amr Mohammed

Speak Your Mind

*