maven, fix compile order modules - java

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

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.

Packaging spring sub-projects into one jar

I'm having Spring based web-app that I'm trying to bundle into a single jar.
My thoughts on laying out the project was to have a root project that contains multiple sub-projects, where there would be one sub-project that is a spring-boot application, and others sub-projects maybe my own-written code handle certain business logic let's say.
So the structure may look something like:
/root
build.gradle <- the problem is here, how I should write root project build script
/spring-backend-subproject
/other-business-subproject
Having all these sub-projects, I want to package them all into one single JAR, so that I can just do
java -jar build/libs/RootJar.jar
otherwise I need to run:
./gradlew bootRun
which will run the particular main class in the "spring-boot" sub-project
I've been searching online for how to bundle all subproject jars into one jar, such as this one: Can Gradle jar multiple projects into one jar?
but I often just end up having errors such as main-class not found(which does not happen if I run the jar specifically in the "spring sub-project" folder) or SpringApplication class not found (again does not happen if run jar in the sub-project folder)
How may I solve this problem?
Thanks!
I use this pattern extensively. Instead of trying to put the deployment classes into the root, add a module (I usually call it -launcher) that lists the other modules as dependencies and contains your main class and related application code, such as any application.properties you may have.
This module will produce an artifact my-project-launcher.jar, and you deploy this to whatever platform you're using.

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 war project dependant on another Maven war project

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.

Categories

Resources