How can I share common components between Java web applications? - java

Environment
IDE: Netbeans 6.9
App Server: Glassfish 3
Frameworks: Spring, Hibernate, Struts 2
Problem
I have 2 web applications. I want to share resources between them both - i.e. authentication form jsp and other assets (js - yui,jquery/images/css). I will be adding more web applications that will also require access to these common components.
As a last resort I will create another web project with just the common assets - including them via http://common.mydomain.
How has anyone else achieved this? Can I create a web resource jar/war and include this in both applications?
I am experimenting so will update question with any progress.
Many thanks.

Place these 'common' components in a separate module (or in several separate modules) and make a dependency to this module from other parts of your app. (I'm not sure how is it called in NetBeans, in IntelliJ IDEA it's called module, in Eclipse - it's a separate project in the same workspace).

In eclipse you must create an utility project and an ear project containing the utility project and your current web applications. You can then move common code to the utility project, and deploy the ear to glassfish.
Common resources must be located through the classpath. This is rather easy with JSF 2

Netbeans has a feature to do this. Create a new project just for your shared components. List the project as a dependency of the projects that need it and make sure both projects are open in the IDE during the build process.
The IDE agnostic way(which I'm using in my own projects.)
would be to:
use maven to manage your projects
build your shared components into their own .jar
setup a local repository server(nexus, artifactory) server to host the shared .jar
(if you're the only dev, you might be able to get around this with mvn install)
list your new jar as a dependency on the projects(wars) that need it.

Related

Multi project gradle build deployed as 2 separate applications (one is a play app)

I am working on migrating a java multi project build based on net beans into a gradle build.
The motivation is not to be dependant on the IDE and having gradle manage all the dependencies.
we currently have one folder that contains all the projects and they have their inter dependencies.
These projects are deployed in two separate applications. One (lets call it App1) in a play framework application (v1.4) that depends on some other projects, and the other (App2) is a regular java application deployed on a separate machine.
Some projects are common to both applications.
I currently managed to build App2 using gradle, so I have a build.gradle & settings.gradle files in the top folder
now I want to add the build files for App1 (the play application).
Questions:
1. I couldn't find any explanations on combining play 1.x with gradle. Does anyone know how to do it?
2. How should I arrange my top level gradle files for building 2 separate applications
3. I want to be able to use the IDE for refactoring code that is common to both apps. how do I do that
Thanks
Answer to your questions
Suppose your projects structure looks like in below image
Please include all sub projects in top level project's settings.gradle like below image
Use the dependencies in sub projects like below image

How to split a web project into 2 separate projects/modules

I have a Maven web project (Java) that I created in Eclipse. Due to in house IDE restriction I had to move the project to JDeveloper 12c and disable the Maven nature. I had to make several tweaks to project's properties in JDeveloper to make it work.
We found that the back-end code (Service Impl, DAO and Entity classes) can be used on several other projects so we are evaluating/considering to separate the single large web project into 2 projects. One for the front end, which is specific for each project and the other for backend, which is common for all projects.
Here are few ways I thought it can done.
1) From the single large web project, create 2 projects; web UI project and web back-end project.
2) Keep the code as it is and use maven modules or maven overlays feature and generate 2 wars from the same code.
I have not dealt with the projects depending on others or multi module projects a lot. Do you see any issues with this type of architecture, good or bad!
Please let me know if you have any other suggestions or ran into similar situations before. Thanks in advance.
Splitting project into many subproject is a good idea. You could use maven multimodule project setup (docs). Every frontend project would have a separate maven project (module in parent pom) and you'll have one project (module) for the backend.
Depending on your requirements you could then create:
an EAR archive with backend in EJB jar and all frontends in WAR archives,
a WAR archive for every frontend project with EJB jar (or jar for non-plain-javaee setup) inside WEB-INF/lib.
Multimodule setup has few advantages and the main one is that you can build the whole application from scratch just by issuing single mvn command.

How to organize gae projects in eclipse

I try to find the way to organize a GAE with several projects within Eclipse using the Google plugin for GAE:
The Web App project (a WebApp project) containing the GAE web application.
A Java project with data access
A Java project with utility classes
My problem here is how to link things together. I want to add the two Java projects in both build and execution paths. Since a Web App project follows the JavaEE structure, only what is specified in the WEB-INF/lib directory is taken into account.
I would like to find out how to simulate a Jar file in this directory based on a Java project present in the Eclipse workspace based on what the Google Eclipse plugin for GAE provides.
I saw something that seems to be related in the WebApp project properties Google > Web Application, section "Suppress warnings about these build path entries being outside of WEB-INF/lib".
For the GAE web application to run then you'll need the classes or a jar from the projects you want to include in the WEB-INF/classes or WEB-INF/lib folders respectively.
One way would be to build your data and utility projects and put the resulting jars in the WEB-INF/lib folder. You can then then reference those jars as libraries from your web app and all should be fine. Of course that's a bit tiresome to do manually, so you should probably check out some dependency management tools. From personal experience Ivy and IvyDE were easy to get into and should cover your needs although Maven and others have their strengths.
Another way that is a easier (but less structured) is to used linked source folders in your build path (to the source folders for your data and utility projects). In such way Eclipse will build those sub projects to WEB-INF/classes and build and execution should work similarly.

How to use Maven to make a Java EE 6 project

I have used Eclipse to make a EJB project, Dynamic Web project and then I have combined these to in a EAR project. Now I am learning Maven and I wonder how you manage how you do this?
If I use an archetype that generates a javaee6-webapp then after what I understand you get a WAR archive....but that can only use the subset of the JavaEE6 specifiaction. I want to use the whole. So do I need to use one archetype for javaee6-ejb and one for javaee6-webapp, and then combine these in some way?
If this is the case or there are other better approaces I would be happy to learn about it.
Or are the EJBs in a webapp project generated by an archetype compiled and packed as a library and added to the WEB-INF/lib folder? If so, does it have access to the full JavaEE6 API if deployed in a full profile app server?
This article by Adam Bien might be interesting for you.
Also, if I understand it correctly, you can use all the goods of Java EE 6 specification using WAR deployment. You can use timers, remote interfaces, asynchronous invocation etc. in your WAR.
It just depends if you use Full vs Web Profile.
There are few differences between EAR and WAR packaging but, in my opinion, the biggest added value of EAR is the separation of concerns and modules (business logic from web).
To start quickly you can also use a maven archetype like this http://seam-archetype.sourceforge.net/jbosscc-seam-archetype/1.4/javaee.html
You have the right idea.
Create all of your maven projects such as ejb-javaee6, webapp-javaee6, etc.
Then IF you want, create an maven ear-javaee6 project to bundle them all together. This EAR would reference your ejb and webapp.
Updated link:
Eclipse has a plugin for maven that makes your life easier

How to share code between a Java Web app and a normal Java app

I have smallish personal project consisting of the following Eclipse workspace.
+
+-MyApp // this is just a vanilla Java Application
+-MyWebApp // this Dynamic Java Web Application (Tomcat)
+-MyCommonStuff // these are common classes
// Ex. Database access code & business classes
This is all well and good when I'm running on eclipse coz I can use Eclipse build properties to make the two apps reference the common project.
I am now thinking of how to deploy my app to a linux server and I'm wondering how to do it.
Can eclipse be used to build appropriate targets which can then reference the common stuff when running in a live environment?
Or do I have to learn how to use builders like Ant or Maven.
Thanks
You just need to add the common projects to the build path of the webapp project and to add them as web library dependency.
In the properties of your webapp project, go to Java Build Path > Projects and select the the common projects from your workspace to add them to the build path. Then, in the Java EE Module Dependencies select the projects which needs to be exposed in the WEB-INF/lib. That should be it.
You don't have to learn to use maven, but there are compelling reasons for doing so.
One of which is, maven can set up your Eclipse's build path up for you (And the next guy who has to work on your code, and the next guy...)
One simple 'mvn eclipse:eclipse' and all your build path setup is done!

Categories