can we build controller and model classes into seperate jar file and add that jar file as a dependency to another project - spring

Normally in our spring MVC projects, we have atleast config, model, controller packages.my problem is can we build these model and controller directories separately as a jar file and add the resulted jar file to any another project.so that we can get rid of some coding and simplify it
Can we use #ComponantScan annotation to make it work

For this you can use Maven .(
If you are not familiar wtih maven read this , its quite simple- https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html )
Follow below steps
Create a Maven main project with packaging pom in pom.xml file
Then create a maven module for this main project with packaging
JAR where your model is placed.
Create another module with packaging war and add dependency of
module we have created in second step to this project.
Now you build the war project module and jar of first module will automatically added into war
For the component scan question , while creating the different maven modules keep a common structure for packages
For example , You can use com.app as base package name and scan this com.app for beans using componentscan. packages in maven modules could be like this com.app.controller, com.app.model etc

Yes you can add the jar files to another project. You just need to add the jar files in the classpath. First create the jar file of the required package with help of eclipse export option:
Right click on the package and select export.
Go to Java and select JAR file
Give a name to your jar file and continue to click next until the jar is created.
Then make a new folder under the other project in eclipse. Copy and paste that jar file in this folder. Right click on that jar file -> Build Path -> Add to build path. Now you should be able to use those controllers. Just give the correct full package name that you have exported in the #ComponantScan annotation. Let me know if any issue.

Related

After creating JAR using "maven-jar-plugin" along with "*.Properties"(files), methods from JAR are failing as "*.properties" path is changed

create JAR from maven project, along with all dependable properties files to reuse methods/class from another maven project.
properties files are located into src/main/resource under folder-> ConfigFolder
When JAR is created the path of ConfigFolder changed and below line of code is failing System.getProperty("user.dir")+/src/test/resource/ConfigFolder/Configfilename.Properties
In maven project - *.Properties is being read by below line of code
new FileInputStream(System.getProperty("user.dir")+/src/test/resource/ConfigFolder/Configfilename.Properties")
When Project is converted into Jar using maven-jar-plugin, the hierarchy of Configfilename.Properties changes hence all my methods are failing.
Before converting JAR Folder Hierarchy
ProjectName(maven project)
|-src/main/java
|-src/test/java
|-src/test/resource
|-ConfigFolder
|-Configfilename.Properties
|-testSuites
|-testng.xml
After converting the into JAR
JARNAME-test.jar
|-ConfigFolder
|-Configfilename.Properties
|-testSuties
|-testng.xml
As you see after converting the jar the hierarchy got changed and new **FileInputStream(System.getProperty("user.dir")+/src/test/resource/ConfigFolder/Config.Properties") **returns null
requirement is to convert first maven project into JAR and use it into second maven project as first maven project contains n number of reusable methods/class project.

Sping Boot to generate jar with only class files and place dependency in a separate location

I would like to create jar's with only class files for my project and to copy all it's dependency into a separate folder and put these in docker.
You can copy all dependency into a separate folder using maven's copy-dependencies
Using maven configuration profile and exec tag's you can create jar's only with class files.
Use maven manifest tag in your pom.xml, So that you can add entries in your manifest files to locate where is the jar placed. So that when application boot's the jar will take dependency from that location.
While creating docker files, make sure to copy all the dependencies. You can use docker volumes

Include the bytecode of a JAR file in a gradle library vs. just adding it as a dependency

If I add a JAR file to a gradle project's depenencies, the code compiles just fine, but after publishing to maven (publishToMavenLocal), the classes from the JAR are not found.
Obviously, this is because the jar is added as a "dependency" and not part of the project itself. Is there a way to get the contents of the JAR file to merge into the library? Do I need to make a separate maven repo for each JAR?
You can always try to create a fat jar which includes dependencies. You can follow the instructions provided here https://www.baeldung.com/gradle-fat-jar

Add non-osgi jars to RCP4 project

I am building an RCP4 application.
I have two non-osgi jars called a.jar and b.jar. Both jars have tons of non-osgi dependencies. One of the dependencies of a.jar is b.jar. So the hierarchy looks like this:
My application
|--a.jar
|----aDependency1.jar
|----aDependency2.jar
|----aDependencyN.jar
|----b.jar
|------bDependency1.jar
|------bDependency2.jar
|------bDependencyN.jar
Some of the bDependencyN.jars are different versions of the aDependencyN.jars
(An example is commons-logging-1.0.4.jar vs commons-logging-1.1.2.jar)
I need to directly reference a.jar and b.jar from my RCP4 application. In other words, when I write code, I will import packages from a.jar and b.jar)
Which is the best approach:
Use bnd 2.4 via command-line to turn all non-osgi jars into osgi ones. I then add every jar to my project via target file
Create a new project "Plug-in from existing JAR archives", and select a.jar and all of its dependencies and export it as a "deployable plugin and fragment" called a.with.libs.jar. I do the same with b.jar and create b.with.libs.jar. I then add those 2 new jars to my project via target file
Create a new project "Plug-in from existing JAR archives", and select a.jar and all of its dependencies, and b.jar and all of its dependencies and export it as a "deployable plugin and fragment" called ab.with.libs.jar. I then add the new jar to my project via target file
Is there a better approach than the suggestions above?
One option is to use bnd-platform (I am also the author) to manage third party dependencies and create OSGi bundles from them. You can use it with both dependencies retrieved from Maven repositories and local Jars (see the README). When you configure a Maven dependency it will also include the transitive dependencies. Under the hood it uses bnd. If needed you can also customize how the Jars are wrapped. bnd-platform is a plugin for Gradle, you can easily start with this template - just add your dependencies and provide the configuration as described in the project README (bundle symbolic names, versions) and run gradlew bundles. The created bundles can then be added to the target platform. You can also use bnd-platform to build a p2 repository / update site.

how to make maven war:explode do not pack a jar file

I'm new to maven.
I'm now working on a multi module maven project, when i use maven war:exploded ,it will packaging classes files to WEB-INF/lib/***SNAPSHOT.jar.
Is there any way to leave the classes files under WEB-INF/classes , for when java file changes, there is no need to replace the whole jar file?
Thanks

Resources