How to create maven modulized project - maven

I want to create maven multi module project, but it should not like normal module project. My requirement is divide web project in to many parts and some module contents common pages, when building project it need to create many web war files.
Is there any maven plugin for this kind of requirement?
Thank You.

Depending on how many war files you need to create, you can use overlays to merge in common pages. This would work if it's ok to have one module per war.
Otherwise, I suggest using different profiles to create each different war. Then you would run the build once per war.

Related

building a jar library from a war project using maven?

We have a EAR project which has a WAR project. Maven is used, so pom.xml. As typical of any project, this project also contains a big feature (say Job Scheduling "JBS") among many other features. As it is planned to retire this whole project in the near future, it is discouraged heavily to spend much on working on this project (whether bugs or enhancements).
Therefore, for the sake of running the (JBS) feature as a separate application, the whole EAR project was duplicated (also to save time/cost). As a result, all the Java packages and classes (necessary for JBS project) were duplicated. In this situation, if we update one or more classes in the main project, this (JBS) feature project/application gets outdated (and needs update).
The fact is that this JBS feature project ONLY requires many packages of Java classes (from the main EAR-WAR project), and do not require 99% of the web modules and others. I am removing all the unnecessary things from JBS project. Then I would like to create a JAR library with all the java classes, so JBS project can have a dependency on this JAR.
I do not know if it is a good idea to separate these classes out of the main project (to create another Java project). I would like to continue to have these classes as part of the main project. Then, it will be good, as and when one or more of these classes are changed, a new version of the JAR will be generated (right away). And the JBS project would then make use of this updated JAR.
How can we accomplish this? I understand, through maven, we can do a build/package jar/war/ear on a project of that nature. I am not an expert with maven (and did not learn it systematically).
But, is there a way to create one or more JARs additionally from inside WAR pom.xml? In other words: I mean pom.xml of WAR will create a WAR. In addition to creating a WAR, can maven help create additional JAR? Or can maven create two packages out of one pom.xml?
Or should I create a separate module in the main project with all these packages/classes, and have its own pom.xml to generate the necessary JAR? For this, most probably I need to modify the structure of the main project. I would like to avoid this unless there is no way out.
Can you advice?
It seems like the best thing for you would be to create a multi-module project that both contains the JAR and the other project. This way, you can easily change/build them together, but you create separate artifacts.

Maven Best-Practices for Customized Builds with Plugins/Extensions?

I'm looking for advice on how to build artifacts that are composed of combinations of multiple modules without repeating a lot of boilerplate for all possibilities:
We have a software that is deployed as a .war into Tomcat and as an .amp into Alfresco running on the same Tomcat instance.
Everything related to Alfresco / .amp does not matter for the scope of the question. For simplicity just assume a single .war artifact in regards to Maven.
We use the open-core model and have a free version that consists of some code that ends up in an .amp and a .war file that contains the Angular-Frontend and several backend libraries.
We have at the moment two plugins in our software - each plugin provides an additional .amp file and adds a .jar / config files to the .war and we have lot's of extensions - each extensions overwrites/extends some Angular-Frontend files and also adds XML-configuration to the .war and/or .amp
Now I'm trying to migrate to Maven from an ancient ant-based build setup that basically just copies the plugins/extensions on deploy time over the base-install.
I need to be able to create configurations like: core + plugin-a + extension-b or core + plugin-a + plugin-b + extension-c - so that I have several .amp artifacts and a single .war artifact for each configuration.
It would be nice if it's also possible to aggregate extensions like core + plugin-a + plugin-b + extension-c + extension-d
At the moment I'm using the maven assembly plugin for the .war and the maven-frontend-plugin for angular and the assembly-plugin just copies the compiled artifacts into the war.
The .war itself is a maven module.
I could go on with this strategy and create modules for every extensions and every plugin but then I will need a module for every possible combination of the extensions and plugins.
To make it worse some extensions/plugins are commercial and live in different repositories - so I can't just add everything to the open-core POM.
I've looked into profiles but I'm not sure if that would solve my problem - as I need something like a central registry for all the submodules?
Somethink like mvn clean package -Pextension-a,extension-b,plugin-a that creates the artifacts would be great.
How to tackle this problem with Maven? Are there projects with these requirements where I can look how it's solved there?
This answer is bit speculative, as I do not know anything about Alfresco.
Have you thought about writing a Maven plugin that downloads an extension/plugin (maybe as zip file from your repository?), unpacks it and applies it to your project?
Then you could call the maven plugin with different lists of extensions/plugins.
In the end I've found Bazel with jvm_rules_external.
The concept of WORKSPACE files that allow dependencies using git/maven/http/etc.pp is perfect for this. Beeing able to also build the Angular frontend using Bazel and create lightweight Docker images as well as the cached incremental builds make it a perfect fit.
However transitioning from Maven to Bazel is not straight forward but after learning the concepts I won't look back!

Maven: Can I have parent and child modules in same Eclipse project?

I need to create a Spring MVC web application where each of the services should be created as a separate JAR file. Some of the docs suggest that multiple Maven modules can be used for this. But what I understood is, that each module also needs to be created as a separate Eclipse project.
Can I have all the controllers, service and DAOs in the same Eclipse project, while still creating separate jars for each service (and a war file for the whole application) with Maven?
Short answer:
Yes, you could create multiple build targets (jar files) out of single project by using maven-jar-plugin (profiles, executions).
https://maven.apache.org/plugins/maven-jar-plugin/
Should you do it?
I believe most people will agree with me that you should not do it, you should stick to one of the most important core concepts of Maven: modularity.
Now, Have you asked yourself the question, Do I really need to create multiple modules (jar Files) for my application? Can I manage all pieces of code in one single project?
Maybe you just need some guidance about how to organize your java packages in one single project.
https://dzone.com/articles/layered-architecture-is-good - Using layered architecture
http://www.javapractices.com/topic/TopicAction.do?Id=205 - Packages by feature vs Layers
NOTE: If you think that your project will grow fast and the number of classes will be a large number, it is probably a good candidate to be a multi module application.
If you still have the feeling that you have to create multiple modules, there are several interesting posts about how to address modularity in the design of applications, in these posts they provide very interesting reasons/criteria why you should split your application into modules.
How to decompose a system into modules?
Spring Java Maven Project + Module Design
Good Luck!
What is the problem of separate modules and hence separate Eclipse projects? If you declare a Maven dependency between the modules, you'll be able to have compile dependencies between the classes in the corresponding Eclipse projects.
So I suppose that it is possible to force multiple Maven modules into one Eclipse project, but I don't know any good reason to do so.

Merge i18n properties file in maven overlay plugin

I have two web application projects in place, Project A and Project B. Project B has war dependency on Project A. Both projects have messages.properties files to handle i18n. However, the location of properties is same for both the projects. I am using maven war overlay plugin to overlay files of Project A on to Project B. If files are in the same location for these two projects, maven will not override Project B's files and leave them as is. However, this leads to maintenance problems as new text for i18n has to be added in messages.properties of both the web applications.
Is there a way to tell maven war overlay plugin to merge the properties files at the time of packaging? The logic of not overriding files when already present serves us well otherwise.
You should be able to achieve this with the Maven Cargo plugin outputting an uberwar.
http://cargo.codehaus.org/Merging+WAR+files
It doesn't know how to merge properties files out of the box, but it's super easy to implement a custom merger, all you need to do is implement org.codehaus.cargo.module.merge.MergeProcessor, and have it available on the classpath.
You probably just want to write a simple merger that can concatenate files together, unless you have duplicate properties then you might need to do something a little more fancy.

Create a maven project to be used as a jar library in another project

I want that a maven project can be used as a black box jar. This is needed because second project was born its way, and I don't want to integrate its code by hand. So the best way is that this project is going to save it's data on db, but it should use a service offered by the "wrapper" project to save them.
The idea is simple, the secondary project can expose just a method to which I will pass the service that offers the save method as a parameter.
The secondary project should not have configuration files, but should rely on the father application's properties.
Any idea to do this fast and almsot good? Thanks for any suggestion.
EDIT 2013/03/07: The idea behind this is that the second project should generate a classic jar library that looks a properties file into the classpath of the host project. Something like Quartz/Spring/... you import jar and you provide the properties file.
I just defined some classes to load properties from the classpath, and some interfaces to make the two projects interact.
In pom.xml of the parent project I imported the child project excluding it's dependencies to avoid conflicts.
It was a pretty easy task at the end.

Resources