-
Notifications
You must be signed in to change notification settings - Fork 3
Creating a new Java EE application project
The instructions below illustrates how to create a Java EE application from scratch in the Eclipse IDE. The idea is to obtain an application similar to the [advanced tweet application example]((https://github.com/selabhvl/dat250public/wiki/Running-an-advanced-Java-EE-Aplication).
-
Go to File > New > Maven project.
-
Go to Next in the first window and choose the
org.apache.maven.archetypes
with Artifact Idmaven-archetype-webapp
and press Next. -
Use the information provided in the figure below, then press Finish:
We need to change some parts of the configuration properties to get an up-to-date application:
-
Right click in the project, and go to Properties.
-
Go to Java Compiler and deselect the "Enable project specific settings". Click in Configure Workspace Settings... and verify that the Compiler compliance level is set to 1.8.
-
Apply and close the changes, and press Yes if you are asked to build the project.
-
The
web.xml
file created needs to be updated. Go tosrc/main/webapp/WEB-INF
, open theweb.xml
and substitute the content with
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Servlet 3.1 Web Application</display-name>
</web-app>
- We need to include dependencies in the
pom.xml
file. We include all the dependencies also used in the advanced tweet application example. The dependencies that must be included between the<dependencies> HERE </dependencies>
tags are listed below
<!-- Needed to build web applications with request/response workflow -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- Dependency for JUnit Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Derby dependency for the database -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.12.1.1</version>
<scope>test</scope>
</dependency>
<!-- Contains all Java EE 7 APIs (from EJB, JTA, JCA over CDI to JPA) -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<!-- Dependency for RESTful APIs -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m09</version>
<scope>test</scope>
</dependency>
<!-- RESTful Web Services using Jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0-m05</version>
<scope>test</scope>
</dependency>
<!-- For the management for persistence and object/relational mapping -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<!-- For using JSON to send to Dweet -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
- In the
pom.xml
file again, just below the<finalName>...</finalName>
tag, and inside the<build></build>
tag, we need to add the maven compiler plugin configuration. The final result should be
<build>
<finalName>fromscratch</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
- We need to update the
org.eclipse.wst.common.project.facet.core.xml
file inside the.settings
folder in the project folder where our application has been created. Navigate to it with the file explorer. In this example
C:\Users\Alex\Documents\Eclipses\EclipseDat250\eclipse\workspace\fromscratch\ .settings
If you do not know how to see where your project is located, right click in the project, Properties and in the Resource tab the Location is shown as illustrated in the figure below.
- Open the file, and modify the
jst.web
property from version2.3
to3.1
andjava
from1.5
to1.8
as shown in the figure below
-
Save the changes, Refresh the project and go to Project > Clean... and clean the project.
-
Right click in the project, Properties, Project Facets and verify that Dynamic Web Module and Java are configured as shown in the figure below
-
Right click in the project, Maven > Update Project... and press OK. There should not be any error marked now.
-
Check that the project compiles without errors. Right click in the project, Run as > Maven build..., in the Goals input text type
compile
, and Run it. Check the BUILD SUCCESS in the Console. -
Add the project to the server and run it. Try to deploy it with the Add and Remove... option of the server, wait for the server to be
[Started, Synchronized]
and check if the Hello World is shown in the browser when navigating to http://localhost:8080/fromscratch/.
From this point, the base of the project is established.
Now try to build the advanced version of the tweet application example yourself. Things to take into account
-
The
persistence.xml
and theglassfish-resources.xml
files need to be properly configured and located (use the projects provided as reference). -
Remember to create the JMS resources in the GlassFish control panel to be able to use the JMS features.