Maven war project dependant on another Maven war project - java

I have two Maven projects (inside NetBeans IDE) that produce war files. Let us call them Project-A and Project-B.
Project-A is a big web application. It consist of many packages, classes and dependencies.
Project-B is some kind of sub-project of Project-A. It is an application that does a specific task that requires classes from Project-A.
My problem lies in my inability to configure Maven in Project-B to import classes from Project-A along with their dependencies. All my tries ended in either compilation errors or ClassNotFound exceptions.
I would want to kindly ask for help how can I configure Maven in Project-B, so I can use the classes from Project-A and the make the resulting WAR contain all the required dependencies.
Thank you for your time,
Adam

I assume the classes from Project-A get into WEB-INF\classes. In this case they are meant to be used only from that war.
Extract classes from Project-A in a jar.
Use the jar in both Project-A.war and Project-B.war

In my opinion, if it's possible, there will be a JAR containing all the business services and classes that project A and B are sharing.
Both projects have this Jar has dependency.
And may be, if it's your need, i will create an EAR not a WAR, with an EAR maven project with project A and B in dependencies.

Related

How to get folders, resources and pom file from a maven project to another maven project?

I am trying my luck in eclipse to achieve something that I am able to successfully achieve using Visual Studio(.net project). I have a core-framework maven project with additional files and folders apart from the normal folders that maven provides. I exported it as jar file and added it as an external library to another maven project. Is there a way that when I add it as a library to the second project, the second project gets all the folders, files from the first maven project overwriting the pom file in the second maven project too? Inshort I want to make sure whoever takes the framework jar as reference follows the same folder structure as framework with required files such as config file, pom.xml file to avoid errors on missing path/files. I read about dependency management but even for that I have to define all the dependencies in child pom file which I want to avoid. Any help would be appreciated, I didn't find much info around the query.
Copying JAR files to lib folders is old-school. Such you also set aside Maven's sophisticated dependency (and transitive dependency) resolution. The clean Maven way is to put your core-framework JAR to a Maven repo (remote or local at the users machine) and let the users of it declare it as dependency in their projects.
What do you exactly mean by "additional files and folders apart from the normal folders that maven provides"? What is there more than code and resources?
To make your users' life easier concerning dependencies you could a) use a BOM (Bill Of Material) dependency or b) use inheritance (i.e. <parent> ← child relationship).
Adding a dependency will not change your project in any way. You cannot add folders from a dependency to the project.
You can write a Maven archetype that is a kind of project template. Then people can use it to create new projects where the files are at the right places.

Creating a war file of the whole project and a jar file for a class with main method (along with the dependent classes and jar files) using maven

I have a CXF WS project from which I need to create a war file of the whole project and a jar file for a class with main method (along with the dependent classes and jar files) (for code re-usability) using maven. Please help me how to do so. I need to know what I need to give in the pom.xml file Please guide me.
The Maven way of doing things is that you create one artifact (e.g., either a war or a jar) per module. Since you want a jar and a war, you want to utilize at least two modules. Each module goes in its own directory with its own pom.xml file. Presumably, the war would have a dependency on the jar.
Maven has lots of support for multi-module projects to ease the maintenance and sharing of configuration between the multiple modules.

How are dependency binaries included in my final built and installed .JAR in Maven?

1) I included a Spring Context dependency in my pom.xml project in Eclipse with Maven.
2) I ran the 'Install' phase on the project and it built properly, and the project was installed to my local .m2 repository
3) When I unzipped my .JAR, I only saw my single class that I created custom.
This brings up two questions:
1) Are external, dependency classes only included in your final built jars if a class from it is physically instantiated within your class?
and
2) How come, when I imported the SpringContextAnnotationConfig class into my class, and instantiated an instance of it, and installed my project, I STILL only saw my custom class when I unzipped my .JAR. Is this unusual? Or is the SpringContextAnnotationConfig now just written into the .class binary so that when I deploy my .jar to another JVM, it hass all its dependencies within my custom built .class binary?
Thanks
SOLUTION:
The problem was that I was expecting maven to do the same for a JAR output as it would for a WAR. When using the webapp archetype to generate a WAR file, Maven automatically packaged the required dependency jars into the WEB-INF directory.
I was basically trying to understand how a container on a remote, brand new server would run my classes without having the dependency binaries. I was attempting to run a maven built to produce a JAR file, which did not end up including my dependencies. However, when I ran maven install to build a WAR file, my dependencies were all there ready for deployment.
No, they are never included (unless you use a special plugin which does that).
See 1.
If you add this artifact as a dependency to some other project, its dependencies (and their dependencies, etc.) will be automatically added (this is controllable, so you can e.g. exclude them or change the version). But they are taken from pom.xml, not from the .jar itself. This also allows not to download same libraries a huge number of times.
For building fat jars, see e.g. How can I create an executable JAR with dependencies using Maven?. Specifically for Spring you may want Spring Boot.

maven, fix compile order modules

I have a multi-module project.
Project contain some jar modules, one war module, one ear module. War depends on jars, pom.xml of ear module contain dependencies for all other modules - ear file deployed in container.
Every time I make new ear file I need first compile all other modules (and remember right order of compilation). I'm tired of this.
I know about parent module. http://www.sonatype.com/books/mvnex-book/reference/multimodule.html
But it seems not a good solution for me.
Does I have any other solution to fix compile order modules?
If the modules are dependent in a way that they must be built together, there are two ways you could handle this.
Use a parent pom (they were designed to address this situation)
Use an external build script to build the dependencies before building the main project

Project Build classes do not include external project imports

I have a project which imports another projects to its build path. When I clean the project to compile classes, the classes imported from external projects are not showing up as part of the compiled classes directory.
Do anyone know how to make sure these external import classes gets compile and included into my build classes?
Thanks
I don't think it's possible. But when you deploy your project, you'll certainly want to create a jar file to hold all your classes. And Eclipse has a wizard to export a jar file and choose to embed dependencies in the jar.
The usual way, however, is to have each project generate its own jar, and to use all the jar of the project + the jars of the dependencies as the classpath of the deployed project.
#Bitmap,
is this a WAR or EAR project?
In EAR project you have to specify clearly the "JAVA EE Module dependency" to include the referenced projects.
If you include a project as a build dependency, it will be for compile-time only.
If these are simple java projects you may want to look at "JAR JAR" enter link description here
to achieve this.
HTH

Categories

Resources