I have two Java + Spring Web 4 (not Spring Boot) web project, the dependencies are managed with Maven and are packaged as WAR.
Since both are almost identical, I'd like to create a common WAR with all the common code and put in these two WARs only the differences.
Here a graphic representation:
Most of the differences are in the #Controller classes, for example, I have some endpoints with a custom annotation in one project and without on the other. Another difference is that there are some differences in the code inside the #Controller classes.
I have seen that often, the Maven Overlay is suggested in a situation like this but it doesn't seem a good solution in this case and is not good during the developments because I'd like to debug each project separately.
Do you have any suggestions?
Related
I am trying to move break up a small java spring boot microservices project into a multi-module Gradle project.
When I have worked on my own projects in the past I have leaned towards having a package for every type of component i.e controller, entity, repository, model, service, configuration, datasource, facade... etc.
Now I have realised 2 things...
1) I would prefer to have a organise my project by domain area NOT component types
2) I should use modules, not packages to organise my project.
Now most things seem to work like this except the fact that because I don't have all my spring repositories in one package, I cannot use #EnableJpaRepositories annotation. I know in a purely package based project I can use it in a package higher in the hierarchy and it would scan all packages below it and pick out any repositories.
I'm not sure how to get around this without having a single package that contains all the repositories.
I almost always organised my projects using only packages so I accept that I may just be misunderstanding the implications of using modules.
Any guidance would be appreciated.
I've faced with the same issue and I've found the answer here: https://github.com/spring-projects/spring-boot/issues/8134.
You need to add AutoConfigurationPackage annotation. One more thing - I use org.springframework.boot.autoconfigure.EnableAutoConfiguration configuration in spring.factories resource. It give me possiblity to autoload a module if it added just to runtime dependencies.
I'm trying to design the architecture of a medium-sized web application in Java and I would like to get some advice on how to do it.
The project consists on a base website plus a number of modules. For instance, one module would provide user registration, another module would offer a web service, and so on...
Whenever I need to deliver the application to a new customer, the ideal thing would be to pick up the modules he wants, do some theming (css, images, maybe jsp) and developing the custom modules he may need, if any.
I've taken a look to maven multi module projects, war overlays, but I find it difficult to partition the application, especially in regard to the modules' configuration (for example, merging a global spring configuration from the modules). Can somebody point me to an example of such a system? Thanks in advance!
merging spring configuration is easy. In each module, package up a spring context file in it's /WEB-INF/classes directory. When you overlay, all classes and resources in WEB-INF classes in the dependency will be put into WEB-INF/classes in your app. (ps, this also works if you package as .jar instead, but you won't be able to overlay .jsp files if you do)
Then it's just a matter of importing them. This is best done by using a set pattern to find the files. Here's an example:
<import resource="classpath*:/module/*-context.xml" />
This will import all the classpath resources that match this pattern.
Annotation based example:
#Configuration
#ImportResource(value={"classpath*:/module/*-context.xml"})
public class MyConfiguration { ... }
It's the web.xml configuration that will cause you more trouble than anything if you have the need to do any web.xml customizations in modules. You could use servlet 3.0 for this of course, but it requires the right server to deploy on.
fwiw, after some experience with plain Spring import we developed tiny framework for osgi-less modularity for Spring. The first problem with import is the bean name clashes you can not have same-named singleton in two contexts, and many other hassles.. tbc https://github.com/griddynamics/banshun
--
Mike
I have a large scale project I am working on at the moment using Eclipse. Normally, as a one man team, these problems would not be an issue, but as our team is not one person we need to be able to break up pieces of the project to be worked on by certain team members.
In simplicity, let's say I have two layers to be separated apart:
1. Each DAO is a separate Java project, to be worked upon individually
2. The web-tier service layer contains all of our service endpoints and must be able to reference all of the DAOs. This layer runs on Tomcat as a dynamic web project, and utilizes Adobe LiveCycle Data Services as the piece that handles creation and management of endpoints.
Now, the issue we are running into is that when we create a DAO and unit test it individually it runs great. But when we reference it into our service project and try to run it we begin to get all kinds of issues related to the fact that we have two different versions of certain jars referenced in and as such we begin to have errors when running the server.
As a result, we know we can solve the issue by pulling the problem jars and ensuring that this is not an issue again in the future, but as I said before this is a large scale project with multiple people working on it and we don't want to be spending our time weeding out dependency issues when under the gun.
We are looking for recommendations on where to proceed for alternative solutions? Our team is new to JavaEE and as such we don't have much of a bearing on what we can use to tie everything together in it, or if it is a viable solution. Should we be looking at turning our DAOs into EJBs and deploying them in an EAR library? If so, where would our service layer lie, and would the service layer be able to reference the DAO classes since the EJB maintains it's own classpath (from what we have read?) Are we looking down the wrong path, or are we completely wrong in our current understanding of JavaEE?
Any assistance is greatly appreciated. We are still in the framework stage of this project and we want to be sure that we will be able to maintain it in the long run.
I second the Maven recommendation. That can add all sorts of sanity to your project structure.
Maven can even generate Eclipse workspaces via mvn eclipse:eclipse
An important clarification on the EJBs note. As of ava EE 6 is you no longer need to separate EJBs from Servlets and can use them together in the very same jar in the war file.
So understand from that that using EJBs or not no longer has any impact on packaging or classloaders as it once did. These are now separate decisions. EARs and classloader separation should now be viewed as a feature you might want to use should you want classloader separation and the complexity it brings. Most applications simply do not need that and are more than fine with just a war file containing servlets, ejbs, jpa entities, cdi beans, jaxrs services and whatever else you need. You are free to decide how you want to separate them or if you want to bother separating them at all.
EJBs do make great DAOs due to transaction management, something you don't get from plain Tomcat but can be made available in Tomcat via TomEE and works fine in Eclipse. You should consider EJBs for that reason, not for dependency reasons.
Side note, as you're new to Java EE, you might find this helpful:
http://openejb.apache.org/examples-trunk/index.html
In order to have things organized when working with Java EE in teams of 1+ people I could suggest:
Use Maven to manage your build process and library dependencies.
Maven has a small learning curve, but once you grasp it you will be grateful. By using Maven you no longer depends on Eclipse to manage your classpath.
A thing about it that I think is really helpful when working in teams is the install feature. Suppose you are woking on the version 1.0 of an EJB module, say core-ejb-module-1.0, and you've got it to a stable state and want everyone working in the project to refer to it from now on.
You then run a maven command like this on it: mvn clean package install
Maven will clean this module, compile it, run tests, create the jar and then install it to a repository that you define. Could be any computer in your company.
Now you may tell the guys working on other projects to update this dependency version on their .pom file and in the next build they run, before compiling, maven will download this library and then use it. Really neat. No more classpath hell.
(There are other ways to always automatically refer to the latest library as stated in this post, but there are some caveats. Anyway it's just an example.)
Use JPA/EJB instead of DAO Pattern.
Some people say DAO meaning any sort of data access, others really mean that they use the DAO Pattern to access objects. If that is your case, you no longer need to use it when using JPA. (At least for most common scenarios).
In my case, I have a generic EntityService which is capable of doing CRUD operations on any Entity and has a centralized query management. Then every EJB's that should perform database related operations may inject this guy and do its job.
As a suggestion, with Maven, you project could be organized as such:
core project structure
core (The pom root)
core-ejb-module (Includes all generic EJB's, like the EntityService for instance.)
core-jpa-module (Includes all JPA generic definitions, like Interfaces, MappedSuperclasses and such.)
core-jsf-module (Includes all JSF generic definitions, like abstract controllers, generic converters and wrappers for FacesContext, etc..)
Now that you have a core generic module setup, you could create:
an application structure
app (The pom root)
app-ear-module (Includes all other modules in this application. Shared jars goes in the ear /lib folder, so all other modules could reference to them.)
app-ejb-module-a (Includes EJB's for the business layer of your application. It uses the core-ejb-module)
app-ejb-module-b (You may have lots of ejb modules. You may even have a project that contains only ejb modules. Other apps will declare their dependency on them via Maven.)
app-jpa-module (Contains definitions for JPA Entities that represents you database tables. Depends on the core-jpa-module)
app-web-module (Holds the pages, Controllers and Converters for this application.)
I think you got the idea. Things tend to be loosely coupled and you may organize your projects as you like.
This is just a simple example to illustrate. I didn't explained a lot about Maven but if you're interested I think it will help you indeed.
I hope it gives you some ideas and may help you in any way.
[]'s
If you can run all the sub-components using the same set of dependencies, you may find it helpful to migrate to a Maven build.
With Maven, you can define a top-level project that manages all the 3rd party dependency versions in one place, so all modules are built, tested and deployed against the same library versions. You are also likely to find Maven a good fit for the multi-module approach you have adopted, as it ensures that a project is rebuilt correctly if one of its dependencies changes.
You would still be able to use dynamic web projects as before; Eclipse will automatically deploy the DAOs as part of the service project (IIRC you need to characterise the DAOs as utility modules).
If you do go down the EJB root, you are correct that each EAR will get its own class-loader, and can therefore use a varying set of dependencies. However, in your position I would tend to look at improving your dependency management first - it'll probably be cheaper and easier.
We are building a small application using different architectural layers such as domain, interface, infrastructure and application. This follows the Onion DDD model. Now I am wondering if there is any benefit in splitting the application into a multimodule maven project. As far as I can see now it seems to make things more difficult than needed. The entire application will be deployed as a single WAR file into a Tomcat container.
Splitting your application makes sense for the following:
When a certain part of the project needs to have new functionality or bug fixes, you can simply focus on that module and run just the tests for it. Compiling a fraction of all the code and running just the related tests speeds up your work.
You can re-use the code from the modules across different projects. Let's assume your project contains some well-written generic-enough code for mail sending. If you later have another project that need mail sending functionality, you can simply re-use your existing module or build upon it (in another module by adding it as a dependency).
Easier maintainability on the long run. Maybe now it seems like a small project. In a few months things might look different and then you'll need to do more refactoring to split things into logical units (modules).
Conceptual clarity (as added by Adriaan Koster).
Concerning the WAR: You can have an assembly module which puts things together and produces a final WAR file from all the related modules.
Initially, this may seem as more work, but in the long-run, modularized projects are easier to work with and to maintain. Most sane developers would prefer this approach.
Using multiple modules forces you to have a hierarchy of dependencies. You have one module which is standalone and doesn't depend on any other of your modules. You have another which only depends on that. It might appear harder than allowing anything to depend on anything else but this approach results in a mess of dependencies which is hard to fix later.
If you are trying to follow a layered model I suggest you place each layer in a different module. This will ensure you are not tempted to break the model.
Short answer: today it is small, tomorrow it will bigger and more complicated to maintain, reuse, extend, integrate with other system and so on
As far as I know, Maven do little help for WAR dependencies. As you are talking about single WAR, this should never be a problem.
You can separate java classes into several "jar" submodules, but if you split the WAR project into several smaller WARs, using some kind of "overlapped" packaging things get complicated.
Just information, one of our projects, it contains too many web pages, so we decided to split it into several WAR submodules, however, the session is not shared between different WARs deployed, and we are not going to use Kerberos stuff. At last, we modified a lot sources of Glassfish, Jetty, MyFaces, etc. To make them resolve web.xml stuff inside JARs. And converted the whole project to Facelets 2.0 (to avoid the dependency of JDK tools.jar and custom resource handler), the only reason is to change the WAR submodules to JAR submodules, and move all webapp/pages into class resources. So the conclusion, Maven does great job for JAR dependencies, but no WAR or single WAR.
EDIT You can put applicationContext.xml in one of the base submodule, and import it by classpath:com/example/applicationContext.xml. Also Spring 3.0 do have annotation supports, you can make spring auto scan them instead of declaring them all in the xml.
Spliting your project into multiple maven projects is useful if you want to reuse your classes in another project or if your projects are deployed in different configurations.
Maybe think of a webservice - if you are hosting the server, you could build a project for your domain classes (models) and your endpoint interfaces that could be used by server and client. The server would be another project that is build to a WAR.
To develop further clients the first project could be used, too.
Use a parent project for dependency management on common projects (like logging) and different profiles and build configurations.
Is it possible to have a Seam project just for entities (src/main) and other projects access them ?
I have a situation that a organization has 6 projects accessing the same entities and i don't want to replicate them in each project. If it is possible, how can i do that ? where can i found documentation about it ?
thanks,
Cateno Viglio
Yes, it is very common way. Look at Richfaces photo album example where you have ejb module holding domain and used as dependency in web module. Of course you must remember adding empty file seam.properties to result jar file.
PS. This example is built by maven and uses ejb (entities and sessions beans). You can use seam-gen and POJO instead if you like.
Depends on what exactly you mean.
Yes, you can declare separate projects for your entity classes. I have about 50 projects roughly. Of those less than a handful are actually web applications packaged as WAR files. The rest are comprised up of utility projects, service projects, listeners, entities, and servlet filters.
Breaking the projects up this way has its advantages and disadvantages. Any time you make a change to a project, you have to propagate that change throughout the other projects. However, it isolates your code and allows you to write unit tests more easily as you can now focus on 5 classes instead of 500.
Another potential downside is, unfortunately PMD won't be able to detect if you duplicated code between your projects. At least I don't know how to do that yet.
I use this pattern (divide and conquer / refactor out replication) quite a bit to avoid duplicate code.