Maven : Copy only selected jars inside WEB-INF/lib - java

In my Maven project , I have certain dependencies which should be present inside the WEB-INF/lib . I cannot put all the jars inside WEB-INF/lib , only the selected ones . How to go about doing this?
I cannot use the maven-resources plugin since then I would have to mention the entire jar's name inside <include> tag and I need to keep it dynamic.
I tried using <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> but this not give me an option to insert only selected jars inside the lib folder.
I also tried using <scope>provided</scope> for some of the jars but due to this the name of the jar doesn't get added to the classpath field inside manifest.mf file.
Please suggest some solution. Thanks in advance!

You can use Assembly plugin and customized assembly descriptor. In descriptor you can set which jars you want to copy with their artifact id, group id, version and scope. it also supports regex. https://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html

This may not be the best way to package the content for the WEB-INF/lib/ nested directory in a WAR file, but the implementation presented in the question for How to put all required jars in a lib folder inside the final jar with maven (using the maven-dependency-plugin plugin) solved my JAR needs for bundling all dependent jars into the lib/ directory of my final jar. Oddly it's a commonly asked question, with many different solutions. This implementation actually bundled the jars inside the nested lib/ directory, while others, for example, extracted the contents of my dependent jars and laid their classes beside my classes. So this might get you a little closer but probably not the official way of handling a WAR files' library deps.

Related

Java export jar including libraries

I have been looking around for some time now, but didn't a find way how to export a JAR (not runnable jar) that contains in it's build path the referenced libraries.
Using Eclipse, I have included the lib folder which contains the jars of the referenced libraries in the export process.
Importing that JAR to another project and calling some method results in a ClassNotFoundException.
Looking at the MANIFEST, I didn't see any reference to those jars in the classpath, though the jars are indeed included in the jar.
So my questions are:
1. Is there any way to accomplish the packaging of the non-executable JAR so it will include libraries?
2. Is there any best practice for building and deploying a jar that include other jars libraries?
I tried it too but it doesn't work for me. I added the final .jar file but it doesn't work.
So, I did a workaround.
Extract the .jar file that you want as a dependency.
Copy that content and put it all inside your .jar file.
Add your .jar file as dependency inside an eclipse project.
Run it and see if everything is ok.

Order of resource loading in WAR

I have a Java webapp (WAR) that has several dependencies. Two of them contain resources to be exposed in the war (with the same name).
So my dependency tree looks like
The my issue is that the custom.css from the core.jar dependency is used if I access index.html
Is there a way to force the usage of the custom.css? I cannot modify core.jar
Thanks,
michael
Environment: Maven 3, Java 1.8, Tomcat 8
There's no guarantee as to the relative ordering of JAR files in WEB-INF/lib, but it is guaranteed that WEB-INF/classes will be ahead of all the WEB-INF/lib/*.jar files in the classpath. So if you can get the right custom.css into WEB-INF/classes/META-INF/resources in your final WAR file it will be used in preference to the ones in the lib JARs by ClassLoader.getResource.
You can extract and put necessary resources into myWAR itself.
As long as it's loaded by classloader (which is likely in your case) you're guarantied to not be able to force ordering.
You might be able to unpack the core.jar dependency and exclude the custom.css file as for example specified in Building a WAR project with unzipped JAR dependency?
In such case, you would exclude the core.jar dependency in maven-war-plugin (see https://maven.apache.org/plugins/maven-war-plugin/examples/including-excluding-files-from-war.html) and configure maven-dependency-plugin to unpack core.jar to target/classes while excluding META-INF/resources/custom.css.

How to include jars that load other jars in maven?

I am developing a Java application in NetBeans and using maven for the dependencies. I have a bunch of jars located in a folder. These jars load other jars located in a path known to them. It seems to me that, when i load a local jar in maven, it is actually copying the jar to another location. The problem is that the moved jar can't find the other jars anymore because now the relative path to them jars is broken.
Is it possible to use maven in such way that the included jars are not moved from their original place so they can find other jars? Or if this is not possible, is there a way to give maven an entire folder that contains subfolders, and if maven moves the entire folder, it would also move all the subfolders with the jars inside them?
I am not sure if I was clear enough. I am also new to maven. It seems to me that ant is more flexible in this regard.
Edit: After reading the comments it seems I was not very clear. Basically the company I work for has two applications that share some common jars that load other jars with URLClassLoader. I don't want to distribute these common jars again, i want my second application to find and load those jars from the location where my first application has put them. I found a solution by using maven to import one jar that uses URLClassLoader with a hard-coded path to load the other jars.
I have a module which pom file contains these lines in the beginning:
<groupId>com.domain.project</groupId>
<artifactId>MyFirstProgect</artifactId>
<packaging>jar</packaging>
<version>1.2.3-snapshot</version>
After I compile this project maven will put it into the local repository (mine is situated in the C:\Users\MyUser.m2\repository, in linux it should be somewhere in /home/myUser/.m2/repository).
Afterwards I can add a dependency in the second project like this:
<dependency>
<groupId>com.domain.project</groupId>
<artifactId>MyFirstProgect</artifactId>
<version>1.2.3-snapshot</version>
</dependency>
In my case this helped me.
P.S. But this will lead the whole first project to be added as a dependency. So maybe this is not the perfect solution for you.

What's the correct procedure to add JARs into WEB-INF/lib directory in IntelliJ

Say I have a Java web project and I need to place a couple of JARs into the WEB-INF/lib directory. In Eclipse, if you simply place those JARs there, Eclipse picks them up as on the classpath. However, IntelliJ doesn't seem to.
What is the correct way of adding those JARs into the project such that they are in the standard WEB-INF/lib directory?
In the Project Structure window, after adding the JAR in the section "Libraries", you can go to the section "Artifacts" and you'll see in the register "Output Layout" how the WEB-INF folder will look like.
On the right side you see "Available Elements", which you can drag&drop to the left site into the lib folder.
The only way i could do this is by adding the required jars manually to the WEB-INF/lib folder on the file system and add that to project library explicitly (apart from the way to add via module library as described in the linked SO question).
Hope that helps.
Create the WEB-INF folder manually in your project’s file system.
If you want the added jar(s) to be part your web app, part of your WAR file, then you may need to manually add the WEB-INF folder and nested lib folder to your file system if you do not already see them. Ditto if you want WEB-INF/classes to store anything such as Flyway database migration SQL scripts.
Once created, IntelliJ should auto-detect them and show in the project structure.
Even if you did not see these folders in your project structure, IntelliJ was likely creating them dynamically in packaging your web app at build-time. Anything you place in your manually created folders will be merged in alongside the other generated items being placed in the auto-generated WEB-INF folder. Not obvious at all, but an effective process.
Had exactly the same issue.
In Intellij IDEA right click on WEB-INF/lib directory and choose "Add as Library..." Worked for me. Hope it helps!

Query Regarding Copying local folder jar in Lib folder using Maven

As we do in Ant build, we can specify the Jars that we need to copy in build.xml in case of Ant(We just need to specify the folder name from which we need to pick the jar files). Is there any facility of same kind is available in Maven as well.
If yes, Then do we need to add the dependency tags equal to the number of jars in folder or one dependency tag is sufficient. I hope you get my point.
I think your missing the point of dependency management. All the JAR's required by your project should be defined as dependencies in your POM. If you have any custom JAR files (not available in a public repo) then you will want to install those in a local repository, and access them that way.

Categories

Resources