Packaging spring sub-projects into one jar - java

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.

Related

'Correct the classpath of your application' in a Springboot Configuration in IntelliJ

I have such an application with an IntelliJ-Idea IDE that has a Springboot configuration that errors out and gives the following recommendation:
The following method did not exist:
org.springframework.context.ConfigurableApplicationContext.setApplicationStartup(Lorg/springframework/core/metrics/ApplicationStartup;)V
The method's class, org.springframework.context.ConfigurableApplicationContext, is available from the following locations:
jar:file:/C:/Users/tlmitch/.m2/repository/org/springframework/spring-context/5.2.8.RELEASE/spring-context-5.2.8.RELEASE.jar!/org/springframework/context/ConfigurableApplicationContext.class
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.context.ConfigurableApplicationContext
I'm relatively new to IntelliJ-Idea and Springboot. If this were say Eclipse - I would probably just go to the Java Build Path and add the jar file that contains the class.
I'm less familiar with IntelliJ. So far if something is missing, I've just been running 'npm install' and that usually takes care of it.
Any suggestions?
Thanks much
If you've added Spring dependencies in your pom.xml file, they might be conflicting with the Spring dependencies in the lib folder in your project directory. Deleting the lib folder will fix this. (just make sure your project does not depend on other libraries in the lib folder before deleting).
As error suggests:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.context.ConfigurableApplicationContext
you need to make sure your classpath contains only one such class. You can use Navigate | Class action to see if there are multiple versions of this class exists in project and exclude the duplicated library dependency in Maven pom.xml file.

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.

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

package all external classes in my jar, with Eclipse

I am working on a Hadoop project in Eclipse that depends on another one of my projects; I've included the other project in my build path, but when I export the dependent project, it only contains the classes from that same project.
Ordinarily, this would not be a problem, as I could just link the other project with the -cp flag, but Hadoop requires you to pass the jar as an argument, meaning that all of my dependencies must be inside that jar.
Is there a way, in Eclipse, to automatically build and include classes from projects that you depend on?
Thanks.
You coud use Ant to automatically build, test and export. It needs some time learning it, but its worth.
There are possible tasks (fileset, zipgroupfileset, copy) to include files, jars (unzipped) or anything into the final jar. By this way you definitly know whats inside your distribution jar and you don't need an eclipe installation running.
I suggest you take a look at maven as a build tool. You define the dependencies and build steps for each of your projects in files called pom files. The maven plugins for Eclipse (the m2e plugins) can take the configuration in the pom file and setup your Eclipse build paths and project description so that you can access the classes in your other project in Eclipse. Maven can also create a jar for you that has the classes from both projects (a "jar-with-dependencies").
In maven terms, your two projects are called "artifacts" with one having a dependency on the other.
The one downside to maven (and the cause for many negative comments about maven) is an initially steep learning curve that can be frustrating. What you're trying to do, however, is very straightforward and I expect you can find a number of examples showing you exactly what you want to do.
The first step, and that's what my answer is about, is to take a look at maven. It may seem overly complex, but it can scale to handle just about any build configuration you need as your hadoop apps get more and more complex.
You can export a project as a Runnable jar, which can be useful if you want a single jar, with dependencies included.
Select the Project. File > Export. Select the Java section. Select Runnable JAR file.
See related answer:
Eclipse: How to build an executable jar with external jar?

Java classpath for dynamic web project

Hi all i have very rare problem which needs to be solved.
Problem/issue:
I have a dynamic web project which is already built and i have war file of that project.
I need to apply some customizations on top of the war file given to me.
Using maven or ant am able to compile the custom code written by me and able to add produced class files to the war file.
But the this is happening for final war file build.
when i want to test my code in eclipse. the war file build and deployed in jboss plugin contains only the class files produced out of java files written by me..........
Please help me how can i modify the .classpath file of my project so that a jboss publish can build a war file using the dependent war file which can run on eclipse-jboss to test my custom code....
Advance Thanks.....
Not a rare problem.
What you need to combine two web applications (wars) together. You have your customization war on which you need to overlay the existing web application.
It looks like you have already solved it from build perspective and looking for Eclipse support. To my knowledge, Eclipse lacks support for this. You probably need to manually do the necessary configuration to make this happen.
It looks strange to me to have two WAR files.
Perhaps you have to consider to package your customizations in a JAR and inserting that jar in the original WAR file.
Otherwise, another solution, and what I do often with open-source project to customize is to have three projects in your workspace.
PRJ-src (with your original sources/JAR/WAR)
PRJ-custom (which depends of the previous one); This project contains only the new classes or custom spring xml files (with injection of my own classes)
PRJ (the merge of the two previous projects)
I create an Ant task in the 3rd project which takes the 1st project (PRJ-src) and merge with the 2nd project (PRJ-custom). This is possible to do so with Maven as well.
Then this is the only project I deploy in my app server (tomcat / jboss).

Categories

Resources