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)
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:
- 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.
- Navigate to “target/generated-sources/archetype” and execute mvn install to install the created template into your local repository.
- Navigate into an empty folder and execute mvn archetype:generate -DarchetypeCatalog=local -DgroupId=net.javabeat.eclipselink –DartifactId=EclipseLink-Installation -Dversion=1.0
- A list of local archetype will be listed if you have different archetypes that have been created.
- Enter the number beside of the archetype named “quickstart-java-app-archetype”.
- 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.
- 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.
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.
- Open EclipseIDE.
- Right-Click at the “Project Explorer”.
- Choose import option and from the right menu click import.
- Expand the maven tree.
- Choose Existing Maven Projects. And click next.
- Navigate into your created project by using the browse and click ok. In our case the path is C:\JavaBeat\archetype.
- The Eclipse IDE should read the pom.xml and tell you with the name of the projects found there.
- In our case the Eclipse IDE will mention /EclipseLink-Installation/pom.xml. See Figure 1.3 as the only project there.
- Select the project & click finish.
The project created is imported successfully. See Figure 1.4
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.
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:
- Create a folder under src/main/java called META-INF
- Create a file named persistence.xml.
- 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.
Figure 1.6