Make a deployable Java EE project from a java project - eclipse

I have coded a small Java EE project that consists only of EJBs, helper classes and their tests.
The project is a maven project and creates a jar.
Right now, all tests are happening with JUnit and JMockit.
Now I want to deploy my application to a local glassfish 4.
What do I have to do, to deploy the project? I know that normaly a root Project with a root-Pom is created, that holds a EAR Project with the ear-Pom and the EJB Project with the ejb-jar.
I checked out the jee-simple-archetype, which was - for my kind of project - much to sophisticated and not simple at all. Could somebody explain how I package and deploy my app correctly?

I know that normaly a root Project with a root-Pom is created, that holds a EAR Project with the ear-Pom and the EJB Project with the ejb-jar
This is not necessarily the "normal" way. You can deploy a .jar containing EJBs on its own. Typically you would do this if the EJB beans are either called remotely (via RMI/#Remote or via SOAP/#WebService) or if the application contains some job-like logic (#Singleton/#Startup, or maybe JSR 352 Batching).
Could somebody explain how I package and deploy my app correctly?
In your pom.xml you should use <packaging>ejb</packaging>, that's basically it (assuming you have an otherwise valid pom.xml). The resulting jar can be deployed on its own. For GlassFish you would use the asadmin deploy command for this.

Related

Eclipse virtual application deploy Vs ear deploy

I have a Java EE web application written in Eclipse and deployed on WebLogic.
When I deploy it from Eclipse (as a virtual application) it works fine. When I try to create an EAR file and deploy it from the WebLogic console I get a ClassNotFoundException.
What could be the reason?
How can I create an EAR file that will match the structure of the virtual application so it will work?
Usually this means a dependent library is not being deployed. This tends to be where Eclipse has a dependency which it fulfills as part of the internal build. When building it yourself, work out which libraries you need, and which are part of your application, and which are part of WebLogic. For your own libraries (ie those in the lib folders, or those that SHOULD be in the lib folder) make sure they are on the classpath. For external libraries, ie those WebLogic fulfills, make sure you have the appropriate schema descriptors setup to tell weblogic which libraries it needs to deploy with your application (typically things like JSF).
If you want proper control over the build, take a look at things like Ant. You basically tell it via xml what is needed, put it together, and it generates the Ear/Jar file for you (the Ear file containing all the dependencies as well, and/or with your descriptors for weblogic built in libraries).
If you are planning many builds, I find Ant useful once setup (you can tie it in with other programs too, so it can push a build to your Source Control Server as a tag, build documentation, etc). Ant can be a pain though- you need build descriptors for each library you need to build, as well as the Ear file.

Autowired dependencies in a Maven multi-module project

I have a single Maven project that has the "core" and the "web" pieces in it. I'm attempting to convert it into multiple modules.
I've taken the "web" packages and put them into the new project. I've added the core project as a dependency and I can reference its classes within Eclipse. The "core" project correctly shows up in WEB-INF\lib in the "web" project when I build it.
The problem comes when classes from the "core" project are #Autowired in the "web" project.
In this particular case, I'm autowiring a service class from "core" into one of the web service classes in the "web" project. If I add the package that the core service is in to my context:component-scan in my "web" application config, it finds that service, but then the service references a repository, which references an entity, which is looking for an entity manager factory that I've got configured in my application-context in my "core" project.
So it seems that maybe the context:component-scan in the "core" project isn't happening maybe? Or maybe the classes that are picked up are not being made available to the "web" project?
I'm entirely new to multi-module projects, so if anyone can get me started in the right direction to resolve this, I'd greatly appreciate it.
I'm using m2e in Eclipse with the Run Jetty Run plugin if that matters; however, I seem to have the same issues when I do a mvn jetty:run from the command line.
You will need to load the core module application-context from your web module application context. Otherwise it will not be aware of beans defined in the core application context.
Take a look the answers for Application context from other maven module cannot be loaded and Spring import application context from another project for proper solutions.
You can also have a look at this post nicely describing how to create a multi-module Spring project using Maven.

Running servlet within Eclipse requires libs to be defined 3 times - am I doing something wrong?

Hullo - issue is this:
I wrote a servlet in Eclipse which requires mysql-connector-java-5.1.22-bin.jar
To compile I need to add the jar via the project's "Java Build Path"
To deploy I need to add the jar to the project's "Deployment Assembly"
To run the servlet within eclipse I need to add the jar to the servlet's Run Configuration -> Classpath
It's not the end of the world re-re-repeating myself like this, but it does seem odd.
Given that Eclipse gets a lot of other stuff correct I'm guessing / hoping that maybe I'm overlooking some feature to avoid this silliness (I cannot imagine a scenario where you'd benefit from entering this in 3 different spots ... but maybe I'm being uncreative here ...).
Insights appreciated :-)
The only thing you need to do is to drop the jar in WebContent/WEB-INF/lib.
You are developing a Java Web project, so the traditional place to put the required libs (JAR files etc) is under /WEB-INF/lib. And you do it only once.
In Eclipse, when you create Dynamic Web Project the appropriate project structure is generated for you (this is a development structure). In this case you place your JAR files in ProjectName/WebContent/WEB-INF/lib folder. And this folder is *automatically included in the project's build path.
Considering the fact that it is a Java Web project (you said you use servlets) you have to deploy your web app to some Application Server, like GlassFish, JBoss, WebLogic, WebSphere etc, or more simple Web Container like Apache Tomcat. If you do this thru Eclipse, then again your web project is automatically deployed.
NB!
There may be some additional details related to using libraries.
For instance, when it comes to using database drivers (MySql, PostgreSQL, Oracle etc) Tomcat advises the following while configuring JNDI Datasource (quote):
Before you proceed, don't forget to copy the JDBC Driver's jar into
$CATALINA_HOME/lib
In your case (MySQL) see the example here: MySQL DBCP Example
Also see my answer related to Webapp configuration file organization convention.
Hope this will help you.
P.S. Here is a step-by-step example: How do I access MySQL from a web application?

Setup a Eclipse Workspace With One Dynamic Web Project and Multiple Java Projects

Can someone guide me to setup a web app project in Eclipse. I want to have a Dynamic Web Project and multiple Pure Java Projects. My Web project will contain JSPs and Java projects will contain Struts Action classes, Spring Bean, Hibernate Entities etc. I want to use those Java Projects as dependency to the WAR. I have done work in such environments, but haven't setup a workspace from the scratch. Can you please guide me or share a link?
Note: I want to use Apache Tomcat or Glassfish as server
In every project you can set the JavaBuild Path properties.
So suppose you have project1 that is dependent on project 2 and some other libraries provided as jars. Go to that option for project1, choose tab Projects and add project2 (that of course is already a eclipse project), than go to tab Libraries and clic on Add external jars.. You are done. This is not dependant by the nature of the project.
Specifically, since you have a Dynamic Web Project, you'll need to go also to Deployment Assembly and add project2 and your jars to the list of files needed by the server.

EJBs 2.0 on OpenEJB - Where do I put the needed jars?

We've been using WAS 6.1 so far to deploy our web apps. Now we need to migrate to an economics-savvy Tomcat + OpenEJB solution. The OpenEJB 3.1.2 container is plugged into Tomcat 6.18, no standalone OpenEJB server here.
So here I am, trying to deploy my EJB 2.1 objects into OpenEJB...
My problem is that the EJBs code requires external .jar libraries, and I don't know where to put them so that they are actually taken into account into the container's classpath. It works fine into catalina.home/lib, so it does into openejb.home/lib. But still I'd rather find out a way to package the EJBs so that they are easy deployed with their linked .jar dropped right into place to be used by the OpenEJB container.
It can include building up an .ear or a .jar with the right descriptor files... Any solution that works is good enough for me.
Can possibly anyone help?
Ear Approach
You can just drop it into the Tomcat webapps/ directory and it will be picked up.
Example ear (valid):
myapplication.ear
lib/
lib/libraryOne.jar
lib/libraryTwo.jar
redEjbs.jar
blueEjbs.jar
Common mistake (invalid):
myapplication.ear
libraryOne.jar (err. not a javaee module)
libraryTwo.jar (err. not a javaee module)
redEjbs.jar
blueEjbs.jar
Only Java EE modules are allowed at the root. These are EJB jars, .war files, Connector .rar files and Application Client jars. Prior to Java EE 5, libraries had to be explicitly listed in an application.xml file. Java EE 5 and forward they can be added to a lib/ directory and be understood to be just plain jars as opposed to a Java EE module.
Collapsed EAR approach
In OpenEJB/Tomcat you can put all your libraries into the war file and be free of the ear concept. This is now part of Java EE 6.
mywebapp.war
WEB-INF/lib/libraryOne.jar
WEB-INF/lib/libraryTwo.jar
WEB-INF/lib/redEjbs.jar
WEB-INF/lib/blueEjbs.jar
Common mistake, including specs:
mywebapp.war
WEB-INF/lib/javax.ejb.jar (err. clashes with the related system library)
WEB-INF/lib/libraryOne.jar
WEB-INF/lib/libraryTwo.jar
WEB-INF/lib/redEjbs.jar
WEB-INF/lib/blueEjbs.jar
Doesn't sound like that is the issue, but adding for completeness.
Common mistake, broken dependencies:
tomcat/lib/libraryTwo.jar
mywebapp.war
WEB-INF/lib/libraryOne.jar
WEB-INF/lib/redEjbs.jar
WEB-INF/lib/blueEjbs.jar
The above is not invalid from a spec perspective and is impossible for the server to detect, but still can lead to apps not loading correctly. If libraryTwo.jar needs classes in libraryOne.jar then this app will never work as the Tomcat "lib" classloader cannot see classes from the "webapp" classloader, so classes from libraryTwo.jar will never successfully load. Unfortunately, the vm will almost never say the actual class that was missing and instead will report the first class in the chain of events that lead to needing a class that was missing. This is almost always a bean or servlet class.
Thanks David.
I tried all of the above, but still no luck.
The Collapsed EAR approach wouldn't work for me I guess, as I far as I know Tomcat 6.0.18 doesn't comply to the J2EE 6 specs. Maybe I'm wrong , but I tried and it didn't work anyway. So back to the standard EAR approach.
My EAR is organized exactly as described in your very first example. One Ejb jar, two library jars in /lib, and that's it. Tomcat still can't instanciate my EJB because the EJB class relates to an unreachable class from Library Jar Two.
I simplified my application.xml file so that it only declares one single EJB:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ...>
<application>
<display-name>ProxyaEAR</display-name>
<module id="EjbModule">
<ejb>ProxyaEJB.jar</ejb>
</module>
</application>
Any other thoughts??