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

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.

Related

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

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

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.

How to add a xml a file as dependency using gradle?

I have a project build on gradle and it has a directory src/main/dist/deploy. In deploy folder there is a xml file. This xml file i want to add as a dependency in the jar file which my build.gradle is generating. This jar i am adding in the lib folder of another project that has a dependency on my gradle project. The other project is built using ant. When classes bundled in gradle jar are loaded from ant project they are unable to read that xml file from the gradle jar.
The standard java plugin does not look in src/main/dist/deploy for resources.
You should either move the xml file into java plugin's standard resource folder src/main/resources, or add the dist/deploy folder to your main sourceSet's resource list.

Build child jar using parent war using maven

I need to create a dependent maven project.
The child one should be a jar, that would be called by the parent project which should be a war file.
The steps should be like this. When I build the war, it should automatically build the jar file and include it and build the war and show output of the child jar (suppose a simple print statement).
Note: Build should be done only once and that is for building the final war.
Need to edit the pom.xml accordingly.
I am new to maven,so a bit elaborate solution would be very helpful.

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