I try to make a JavaEE application with 2 and more Spring MVC modules. Before I had WebSuite module, that have Web and DB modules
<modules>
<module>../UBDB</module>
<module>../UBWeb</module>
</modules>
In the DB module are all classes that work with database; in the web module - Spring MVC for views.
But now I need to re-organize my project. I need Maven managed modules, that will consist of different business logic. For example: I need one module that will have controllers and views to manage accounts, another module to create orders, another module for blog etc. (something like CMS). All of these modules need to be on Spring MVC.
But how do I need organize my pom.xml files to make it?
For a webapp, we always loose our time with maven module to think we have a nice separation... that's useless.
Create an account-parent with
Account model
used by Account dao
used by Account service
used by Account controller
Do the same for all the group you wanna do.
Then create a single module app : MyWebApp. It contains just config, properties and add all the controllers in runtime.
You will loose your time while releasing modules, updating dependencies, ...
I worked this way untill last year.
So, sorry for late answer, but I found solution.
I have one module suite - that I use to get all modules in my project
<modules>
<module>../news</module>
<module>../blog</module>
<module>../suite</module>
</modules>
Also it have <packaging>pom</packaging> property
Description news and blog are quite similar
We can package it like jar files.
So, as I sad, I whanted to have some CMS system. How it works. I copy all nessesary modules to some directory. For example I will create site with news and blog module. So I copy there suite module, news and blog module(news and blog module are Spring MVC apps. Suite just pom.xml file). Then I create new directory (site module). In pom.xml of it I set
<packaging>war</packaging>
then in suite module I ad line for it.
<module>../site</module>
So I have 3 SpringMVc applications. 2 of them (news, blog are my modules, that I can set to different projects. Site - new module. It will be deploeed like Jar file This module different for different projects) and suite just to get all modules together.
And then mvn clean package.
Hope It will help for someone
Related
Here is the simple situation breakdown and I'd like to know if I'm doing this optimally or if there is a better convention. I have created a dummy project just for learning purposes.
I have created a multi module Maven project. Simply the parent POM, with two sibling child POMS, one being a service layer, and the other being a web layer.
The end result goal is to have a fully functioning WAR in the Web project's target folder, that I can simply deploy into a Tomcat.
Here is where I am not clear:
- Both the Service project, and the Web project need to use Spring. The Service project needs to use Spring simply for it's dependency injection purpose. I need to take a simple Dog class, and auto-inject it into the DogService object. That's all working fine.
- Then I need to auto-inject a DogService object into a Dog controller. The Dog controller exists within the Web project in the multi module structure. This is also working fine, because I have declared a dependency in the Web project for the Service project, therefore all Service JARs are included in the final built WAR, from the web project.
1) Is there a way to simply declare a Spring dependency for both child projects without having to declare the dependencies in each child POM.xml? I just want to make sure I'm not duplicating resources. I believe the way to do this is just to declare the dependency in the Parent POM.xml.
2) If I do #1 above ^, is this the optimal way of creating the project? In essence, the WEB module is the one that contains all the final jars, and in essence it's almost as if the SERVICE project doesn't even exist in Tomcat. As far as Tomcat 'knows', all there is, is a bunch of JAR files containing classes, some of them having been written in my WEB module, and some of them having been written in the SERVICE module, all of which is irrelevant to the production/Tomcat environment. True or false?
Thanks!
Is there a way to simply declare a Spring dependency for both child projects without having to declare the dependencies in each child POM.xml? I just want to make sure I'm not duplicating resources. I believe the way to do this is just to declare the dependency in the Parent POM.xml.
Maven is quite intelligent about dependency management and will not "duplicate" resources--it caches each dependency once* and manages the classpath so that all of the projects that you work with share the same jars. In general, declare dependencies in the modules where they're needed; don't clutter up modules or especially parents with random pieces just to avoid occasionally re-specifying a dependency. This is like hauling your boat trailer on your daily commute because you occasionally go to the lake.
Keep in mind that dependencies are transitive, so that if service-module depends on spring-web (does it really, or are you spamming dependencies?), if web-module depends on service-module it will pull in the dependency as well without having to repeat yourself.
If I do #1 above ^, is this the optimal way of creating the project?
No, it isn't. Be minimalist about your dependencies: If you need it, include it, but don't add dependencies "defensively". This will just bloat your deployment and slow down builds, along with adding opportunities for problems like version mismatches.
As far as Tomcat 'knows', all there is, is a bunch of JAR files containing classes, some of them having been written in my WEB module, and some of them having been written in the SERVICE module, all of which is irrelevant to the production/Tomcat environment. True or false?
Mostly false. In a war, your top-level project (web-module) has its classes directly in the archive, and dependencies are embedded as jar file inside it. Tomcat does not distinguish between service-module and your Spring and other dependencies, however.
Better still would be using Spring Boot's standalone jar and embedded container features--Boot will take care of packaging up the jars you need into a single runnable file that doesn't need external support.
*Release dependencies only, but snapshots aren't relevant here.
Welcome,
I'm begginer in Spring (also in Spring Boot). I create four maven projects in Eclipse:
my-persistance
my-presentation
my-webservices
my-main
I have assumed every part of program will be store in one of these special projects. For example: html, jsp files will be store in "my-presentation", also like Spring #Controller files. Projects that aren't "my-main" should be build earlier and store in local maven repository [via 'mvn clean install']to add them to "my-main" war package.
I just read one Spring Boot guide:
http://spring.io/guides/gs/serving-web-content/
It works when all elements are in one project. I want move Web Controller to "my-presentation" and class Application to "my-main". Of course it isn't working. Could it be possible to do this?
You can check how to work with Maven and multiple module.
Basically, you create parent pom.xml, and in each submodule (my-persistance, my-presentation, etc.) you create submodule pom.xml, that refer to parent pom.xml.
I have a similar configuration in my example project, you can check pom.xml files
If we have a multiple module project,
Parent Project
Model Module
Services Module
Web Module
And the parent project pom.xml, lists the 3 modules in <modules>...
And then Services pom.xml has a dependency on Model, and Web Module pom.xml has a dependency on Services...what does this mean?
Does it mean I can't reference in my Web Module, any of the Model classes?
i.e. does the dependency chain in Maven impact this type of scoping?
So I cant in appContext.xml in my web module control any of the injection of beans outside of Services/Web Module?
Many thanks (sorry for noob question)
i
Your question is not very clear but what I could gather is that you want to ask whether in your web module you can access your model class. Definitely you can because of transitive nature of your dependencies. Since Service module depends on Model and Web model depends on Service, you can definitely access Model classes in Web module. You can understand this better if you run the mvn dependency:tree command on your Web module. The Model would show up in the dependency tree or if you are using Eclipse Maven plugin you can view the Effective dependecy in a tree structure which is visually better to understand.
I am looking to set up a multi module maven project (described below) that would scale well. I have some questions about the approach which is largely drawn from the Sonatype example.
I have done a certain amount of reading on maven multi module projects but couldn't find an example going beyond the basic level.
Questions:
Is this (below) a good project structure to start with? Or does it smell of disaster right from the start - i.e. would lead to heavy restructuring when setting up builds? In short, I am looking to avoid setting up something that goes against the grain with Maven.
I am expecting some modules to be quite independent, while most will be interrelated. Is it alright to start with each module as a Git repo and later refactor in together modules that are tightly linked?
Objectives:
Good project structure for a modular Spring, JSF2, Maven based project, that would allow for builds involving a selection of modules and their dependencies.
It should be possible to deploy an individual web module on a lightweight container like Tomcat/Jetty through Maven configuration (like jetty-maven-plugin). This should be able to pull in the necessary dependencies through Maven. That makes it easy during development to focus on the module being worked on (not having to run a full build and deployment) and deploy the full application only in a complete build.
The setup should allow for multiple distributions based on a selection of modules to be included in the build. I take it this can be achieved through the use of build modules that will pull and package the corresponding modules.
Project structure
Core domain classes.
somapp.core (maven project)
|- someapp.core (maven module)
|- someapp.core.tests
Account Management Domain classes
someapp.accountmgmt
|- someapp.accountmgmt
|- someapp.accountmgmt.tests
component1 domain classes
someapp.component1
|- someapp.component1
|- someapp.component1.tests
Service 1 - # account management (User login)
someapp.accountmgmt
|- someapp.accountmgmt.api
|- someapp.accountmgmt.impl
|- someapp.accountmgmt.mocks
|- someapp.accountmgmt.tests
someapp.service2
|- someapp.service2.api
|- someapp.service2.impl
|- someapp.service2.mocks
|- someapp.service2.tests
|- someapp.service2.cli # CLI access for service2
someapp.service3
|- like above
someapp.accountmgmt.web
|- someapp.accountmgmt.web
someapp.service2.web
|- someapp.service2.web
someapp.service3.web
|- someapp.service3.web
someapp.build1 # bundle accountmgmt and service2 into 1 war file
someapp.build2 # bundle accountmgmt and service3 into 1 war file
somapp.build3 # bundle accountmgmt, service2 and service3 into 1 war file
(i.e. someapp.accountmgmt.web.war, someapp.accountmgmt.jar, someapp.service2.web.war, someapp.service2.jar, someapp.service3.web.war, someapp.service3.jar, someapp.core.jar)
I understand project structures are not set in stone. I would like to set up one that is a good starting point. Suggestions / Links to examples are welcome.
Well for the Spring part it's already discussed and an answer accepted at Spring Configuration in a multi-module project. As far as the general layout I've only seen one WAR per project and services only bundled together if they are related (e.g. UserLoginService would not go together with DomainObjectsService).
I would suggest breaking up the structure into several different projects, with the dependencies (business objects, etc) deployed as JAR projects to a local repo and listed as normal Maven dependencies in the (now different) projects that need them. Then in your app-server you can deploy the apps to different paths (e.g. yourdomain.com/app1, yourdomain.com/service2).
My compliments to your ambition though!
EDIT: There is a way to have multiple WARs if you wish, see this SpringSource blog post about Using a shared parent application context in a multi-war Spring application.
A hierarchy starting with Spring IO through to your artifacts can be done as a single build multi-module project.
Spring-IO (dependencies)
- Your parent pom (custom and further dependency management, plugins etc)
- someapp-parent (really just a container for each -independent- sub-module)
someapp-api (deploy as jar into Nexus
someapp-remote (Implements API and makes REST calls to your web app - also an independent jar)
someapp-web ('war' Exposes REST - JSON - representations of your API domain objects)
someapp-dashboar (Admin console working with the API/web app via remote so you can manage everything, also a 'war')
Spring IO is BTW really good as a blessed set of dependencies that work well together and avoid classloader issues. Well worth migrating your project to use it as the latest version looks pretty up-to-date. I'd also recommend using Spring Boot for your web app(s).
As outlined I think its worth having all your app-related modules build as one so versioning is easier and you can test everything in a single build command. I've recently worked on a project where we keep all these modules separate, and it just means 4x the effort to merge changes, build artifacts, deploy artifacts etc.
I am still fairly new to Maven, I finally have it how I want but now I need to break it all over again.
Here is my scenario:
I need to write two different server applications, which use identical core functionality; just, what is done with that framework is very different. One server application is very easy/simple - it's already done - whereas the other is a lot more complicated.
The code is written in a dependency injection style (using Guice, if it matters), so it should be extremely easy to break apart.
My question is this: how would you structure the projects in Eclipse, using Maven? Would you set up three different projects, something like:
server-core
server-appEasy
server-appComplicated
where each server would have it's own pom. Or, would you keep it all in one project? I need to be able to easily recompile appEasy in, say, a month from now, while I work on appComplicated. The classes for appEasy are already in a subpackage. Note: core would not work by itself without at least a mock dependency injection. It doesn't have a main class.
All thoughts appreciated, even on things I haven't thought of.
I would have a structure like this:
/server
/server-core
pom.xml
/server-appeasy
pom.xml
/server-appcomplicated
pom.xml
pom.xml
So each project has its own pom.xml that allows you to build that project in isolation.
However the parent folder also has a pom.xml, which will build all the projects if run. You can do this by including the projects as modules in the parent pom.
E.g. In the parent pom.xml
<modules>
<module>server-core</module>
<module>server-appeasy</module>
<module>server-appcomplicated</module>
</modules>
You can also use managed dependencies in the parent pom tio allow you to centralise external dependency and plugin version numbers.
I would suggest to structure all as a Maven Multi Module project.
The parent project, would have the three projects as modules, the 3th party dependency versions, and the version of your project as a property.
Then, in the server-appComplicated and server-appEasy I would add a dependecy to the server-core.
In this way you will gain:
1- A root project to compile (the parent), that it will generate the two servers and the core-lib.
2- A point where to handle the version numbers and the common dependencies.
I hope it helps
Im not a maven expert but here is my 2 cents.
Each project needs its own pom.
Do you need to build all the projects together? In that case it might make sense to have a parent pom , which has all the common dependencies.
EDIT: In that case, I feel just have three separate 3 pom files for each project.
There are multiple ways to do this, depending on how you need it when it comes to deployment. Assuming that 'server-core' is a shared artifacts among your 'server-appEasy' and 'server-appComplicated' artifacts, I would suggest something as below
Create a Maven Project 'server-core'
Add two module projects
by name 'server-appEasy' and 'server-appComplicated'
Make sure the module projects have their parent set as the 'server-core'
In the end you should have three projects (each has separate pom.xml), where
a. Building 'server-core' will also build the modules
b. Building either of the 'easy' and 'complicated' modules independently on need basis will also build the server-core.
Hope this helps!