I have a Maven based J2EE project structure
Customer (root pom)
|
|------ WebTier (JSF 2/Primefaces)
|
|------ BusinessTier (EJB 3.*, interfaces, interface impl)
|
|------ PersistenceTier (JPA 2)
|
|------ CustomerEar
Now I have found that I'd need a new Java class or classes for handling basic routines
I guess its not good idea to place this Utility class under WebTier, because Utilities might be used also from BusinessTier.
Is it the "best practise" to create an independent Java Project, implement necessary classes and include this jar to the ear??
based on my experience , i recommend to have separate project folder for utility and include this as jar where ever is required
advantages are
easy maintenance, since you need to change code once .
plugin & unplug when ever you need.
you can have this as common and utilize for
other projects.
for instance
apache string util
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
bean util
http://commons.apache.org/proper/commons-beanutils/
It would be hard to try and justify a "best practice" in this kind of situation as it is a very project-specific question.
There would be nothing inherently wrong with adding a "CommonLibrary" project or such-like that is a dependency of both the WebTier and BusinessTier. In fact the project I am working on now is almost exactly that structure (with a common library).
As this is a maven project, just make sure the "CommonLibrary" project dependecy scope is set to "provided" in the Business/Web/Persistence tiers, and "compile" in the ear project.
e.g. In WebTier, the dependency would be:
<dependency>
<groupID>group.name</groupID>
<artifactID>common-library</artifactID>
<scope>provided</scope>
</dependency>
EDIT: The scope would be 'provided' in the Web Tier as it would be a .war. The other tiers would be jars so the scope can be 'compile' for the persistence and business tiers.
Related
Suppose we are working on mortgage sub-module, and we are directly using the Google Guava classes in module code, but the dependcy for the guava is defined in other sub-module under the same parent and we have access to Guava classes only by transitive dependency on "investment" module:
banking-system (parent pom.xml)
|
|-- investment (pom.xml defines <dependency>guava</dependency>)
|
|-- mortgage (pom.xml defiens <dependency>investment</dependency>)
Should we still put a <dependency> to Guava in the mortgage pom.xml?
The cons looks like duplication in our pom.xml, the pros are: if someone developing "investment" will drop guava, then it will not stop our mortgage sub-module from being successfuly build.
If yes, then what <version> shoudle we specify? (none + <dependencyManagement> in parent pom?)
If yes, should we use a <provided> scope in some module then?
Note: Keep in mind, that I am asking in specific situation, when modules have common parent pom (e.g. being an application as whole).
Maybe this structure was not the best example, imagine:
banking-app
banking-core (dep.on: guava, commons, spring)
investment (dep.on: banking-core)
mortgage (dep.on: banking-core)
Should still Investment explicitly declare Spring when it use #Component, and declare Guava if it uses Guava's LoadedCache?
we are directly using the Google Guava classes in module code, but the
dependcy for the guava is defined in other sub-module under the same
parent and we have access to Guava classes only by transitive
dependency on "investment" module [...] Should we still put a to Guava in the mortgage pom.xml?
Yes, you should declare Google Guava dependency in your module and not expect it to be available as transitive-dependency. Even if it works with the current version, it may not be the case anymore in later versions of direct dependencies.
If your code depends on a module, your code should depends only directly on classes of this module, not a transitive-dependency of this module. As you mentioned, there is no guarantee that the investment module will continue to depend on Guava in the future. You need to specify this dependency either in the parent's pom.xml or in the module itself to ensure it will be available without relying on transitive dependencies. It's not duplication as such, how else can you tell Maven your module depends on Guava?
I do not see any situation in which minimal best practices are respected where you would need to do otherwise.
If yes, then what <version> shoudle we specify? (none + <dependencyManagement> in parent pom?)
Yes, using <dependencyManagement> in parent and using a <dependency> in your child module without version is best: you will make sure all your modules uses the same version of your dependency. As your modules are an application as a whole, it is probably better as it will avoid various issues such as having different versions of the same dependency being present on the classpath causing havoc.
Even if for some reason one of your module using the same parent requires a different version of our dependency, it will still be possible to override the version for this specific module using <version>.
If yes, should we use a scope in some module then?
Probably not, having the dependency with a compile scope is the best wat to go with most packaging methods.
However you may have situations where you need or prefer to do this, for example if said modules requires to use a runtime environment specific version, or if your deployment or packaging model is designed in a way that demands it. Given the situation you expose, both are possible, though most of the time it should not be necessary.
Yes, declare the dep. It's not a duplication!!! That compile dependencies are transitive is not intended by the maven developer, it's forced by the java-language. Because features like class-inheritance forces this behavior. Your already mentioned "pro" is the important fact.
See the (*) note in the transitive-scope-table
Yes, always declare needed third party lib-versions in your reactor parent with dependencyManagement. It's a pain to find errors from different lib-versions at runtime. Avoid declaring versions of third-party libs in sub-modules of large reactors, always use a depMngs in parent.
No, i would use "provided" only for dependencies provided from the runtime, in your example tomcat/jboss/wildfly/.. for things like servlet-api/cdi-api/. But not for third party libraries.
Declare the "provided" scope as late as possible (i.e. your deployment(s) module war/ear) not in your business modules. This makes it easier to write tests.
For example:
investment (depends on guava scope:=provided)
mortgage (depends on investment, but don't need guava himself)
--> mortgage classpath doesn't contain guava.
If you write a unit-test for mortgage where classes involved from investment it will not work -> you need to declare at least guava with scope=test/runtime to run these tests...
When a module uses a 3rd party library, the module should explicitly depend on that library in its pom.xml too. Imagine if another project should use the 'mortgage' module, and doesn't depend on Guava already, it will fail e.g. when a unit test comes upon a code path that involves Guava. An explicit dependency also covers you against the scenario where you refactor the 'investment' module so that it doesn't use Guava anymore. Your 'investment' module should be agnostic to such changes in its dependencies.
It's always correct to explicitly list your direct dependencies. When it comes to version, it's best to keep that in the dependencyManagement section of your parent pom so all child projects inherit that (same) version.
I read a lot of posts regarding the ways to use spring-boot-starter-parent in a spring boot project.
Essentially, I read posts (Spring documentation also talks about this) describing two ways to do this
To use spring-boot-starter-parent as the project parent directly. It gives us the benefits of having the dependency management as well as the plugin management.
The other way is to import the spring-boot-starter parent in the project pom (we may need this in case we already have a parent pom for the project).
It allows us to get the benefits of dependency management but not the plugin management)
I am creating a new Maven multi module project. Ideally I would like to have my own custom parent and also get all the benefits of using the Spring-boot-starter-parent.
I was wondering if it made sense to create a custom parent for my maven projects. This parent would in turn be a child of the spring-boot-starter-parent.
If I am not missing anything, this way I could get the benefits of having the dependency management and plugin management from spring-boot-starter-parent and at the
same time have a custom parent for all my projects where I could define some other common dependencies or if needed override the dependencies defined in the
spring-boot-starter-parent which would then be inherited by all my projects.
Does this design make sense or am I missing something.
What are the drawbacks of this approach?
There are no drawbacks -- this is exactly what you're meant to do if you want a multimodule spring-boot project. However, consider this: typically multi-module projects have all modules versioned together, released together, and dependant on each other. This rarely makes sense in a group of spring-boot modules, which are typically of the micro-service style and which require independent evolution. So, you should question your need for a multi-module project at all.
I'm creating a new java web project. This web application will have many modules such as core/commons, business service, repository, security, integration, ldap, user management,.... I wonder if I should separate each module into each maven project (jar file) or create project that include all java packages of all modules into single one maven project.
Structure of Multi modules maven project
mycompany-core
mycompany-repository
mycompany-api
mycompany-usermanagement
mycompany-business
mycompany-web
Structure of Single module project:
mycompany-web
|___ src
|____ main
|____ java
|____ com.mycompany.core
|____ com.mycompany.repository
|____ com.mycompany.business
|____ com.mycompany.controller
When should we apply multi-modules or single-module project> Please give me some advice.
All products I've worked on in Maven were multi-module. This is because they tended to be big. However, when I create my own pet projects, they are normally single-module ones.
As a rule, as products grow, they will need to be organized into multi-modules. Some projects start as a single module and are split as they grow. Others, created by developers with more experience, are already divided up, because the developers already know how the code will grow and how it needs to be organized.
Specifically, from your list, "core/commons, business service, repository, security, integration, ldap, user management", I would separate "commons" into its own module, because it smells like it could be reused on other projects. The other parts could all fit into one module, but I'd need more insight into the project.
It is depend on your requirement. If you want to run this using .sh or .bat you should have single target(single jar with other libraries).
If your project build as an API it is better to have your build as multi module one.
Here is the context : one application, let's name it "clientdata" is bundle into an ear to be deploy on WebSphere. We need to generate several jar for this application :
api-contract (interfaces)
ejb
ejb-client
impl
The ear contains ejb, impl and api-contract jar + the application dependencies
Let's call "checkclient" another application which need to call "clientdata" by RMI.
It will need to use api-contract and ejb-client jar.
For now the application is divided into 3 sub-modules (API, IMPL and EJB).
API module generate api-contract jar
IMPL module generate impl jar and use api-contract jar
EJB module generate ejb and ejb-client and use impl and api-contract jar
So here is my question : one member of the team thinks that we should use maven classifier plugin to generate all those jar so that we need but to maintain only one pom (one maven project).
I'm not so familiar with maven classifier plugin so i wanted to know, is this a good idea ?
Some of my readings make me think it is not, mostly because it violate a "core convention" and "Tools that have been designed to work with Maven won’t help you"
Here is the source of the citations : http://blog.sonatype.com/2010/01/how-to-create-two-jars-from-one-project-and-why-you-shouldnt/#.U7UhNCgucvQ
I would greatly appreciate your expertise on the subject.
Regards, GBE.
First the member of your team is simply wrong, cause that's the wrong way to deal with such default scenario in Maven. I assume (hope) you have created an appropriate folder structure
+-- root (pom.xml)
+-- mod-api (pom.xml)
+-- mod-ejb (pom.xml)
+-- mod-impl (pom.xml)
+-- mod-ear (pom.xml)
Based on the above folder structure you can simply define dependencies between the modules and let maven do the rest. Apart from that you can create the ejb-client automatically by maven-ejb-plugin and use this for furthere enhancements.
Now you can create an ear from it (mod-ear just add the dependencies to it) and just deploy it to WebSphere. That the way it should go. Creating a single pom to create that number of artifacts is by definition against the desing of Maven cause the convention in Maven is to have a single pom for an artifact. So you will start to fight against Maven and you will loose that combat.
Better got with the idea of Maven and make your life easier. Apart from that it's a separation of concern which means an single pom for a single part of the project which makes it clear where you can find anything in your project. Here is a simple example of a Java EE application.
My question has been asked before, which I know, but I think that I am trying to do something slightly different, where existing answers are not appropriate.
Essentially, I do want to have multiple projects in Eclipse that will be built (preferably) into one final WAR file. Ideally like this:
root - pom.xml
|___ java-app
|___ web-service-v1
|___ web-service-v2
|___ web-service-v3
|___ rest-service
|___ batch-service
Imagine the Java App as the actual application, and each additional component runs as a decoupled view layer for the Java App itself. Ultimately, the Java application will be running in a tomcat instance, with the different modules providing their services. I would like all the different modules also to run in the same Spring container.
I don't know that running in Maven modules is the best way of doing this, but I would really prefer to have each component in a separate Eclipse project that ultimately get built together.
Can anyone provide any suggestions as to how I would use Maven to build this?
Just make a separate war module:
root - pom.xml (packaging: pom!!!)
|___ java-app
|___ web-service-v1
..
+--- mod-war (pom.xml)
and put the dependencies of the modules you would like to have added to the war file into the pom and that's it.
The main Maven idea is that each module must produce a single build artifact (e.g. a jar or a war file). The parent pom is usually responsible for global configuration and dependency management and also for a proper module orchestration. If your final result need to be a WAR file, then last module in the list will be the web application. The other modules could supply classes that war file depend on.
There are more complicated build structures, but above one should be sufficient for you.
This is somewhat dated, but hope this additional info helps someone.
#Mouscellaneous An example of keeping Spring configs in each module and referencing them from the web.xml can be found here in the Sonatype book. I guess this is what you are looking for.