Using Maven 3, how do I overwrite a file defined in one of the dependencies? - maven

I have a multi-module Java EE application including a module for the web archive.
The WAR declares a dependecy on another WAR which provides a general purpose CSS.
I'd like to overwrite the provided CSS in my local WAR to add some specific behaviour.
So I copied the CSS from the dependency and placed it in the very same directory in my local WAR (being src/main/webapp/css/file.css).
I assumed Maven to merge these files, so my changes eventually become visible.
However, after deploying the application I still end up with the general purpose version provided by the dependency.
Is there any solution / plugin to fix this?

What you probably need is Maven's overlay feature which can merge 2 war projects:
http://maven.apache.org/plugins/maven-war-plugin/overlays.html

Related

How can I exclude artifacts by packaging in maven-dependency-plugin:copy-dependencies

I'm working in a complex tomcat configuration where I'm using third party proprietary service that is distributed as WARs. In the servlet container I have 10 WARs deployed where only one is coded by us.
We are using maven to manage the project.
I'm declaring the third party WAR files in the POM with provided scope.
My issue comes when I try to use maven to deploy the system in a local testing server.
I'm using maven-dependency-plugin:copy-dependencies goal to copy the right artifacts in the right directories in the local serving tester.
I must copy JAR files in one directory and WAR files to a different directory. But maven is not differentiating the artifacts by packaging. So I end having the JARs mixed with the WARs in the destination directory. While I need to have two executions, one for WARs and one for JARs going to the right directory.
I have only being able to use a copy goal specifying every artifact to copy, but this is difficult to maintain if any developer adds a new dependency, the dependency must also be added to the right copy goal.
I will like to be able to use copy-dependencies goal but being able to indicate that I only want to copy a specific packaging.
Any idea on how I can manage to do that?
Thanks!
You can use -DexcludeTypes=war
https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#excludeTypes

Creating uber jar with maven

My project inherits it's compile dependencies from parent and I have no control over it - can't change them to provided. Additionally, I have added another dependency 'a:b:1.0.0' to my project's pom. I want to include only 'a:b:1.0.0' with it's own dependencies (recursively ) to my uber jar.
Seems like neither assembly nor shade plugin doesn't support such case.
How this could be done ?
Thanks
Shading recursively has some significant disadvantages. Especially, the problem of duplicate files from multiple dependencies being overwritten with only a single version of the file. This can cause some pretty annoying problems to troubleshoot at runtime. You'd be better off using something like spring boot to build a standalone jar where instead of shading files into a single hierarchy, will embed dependent libraries into itself as a subdirectory and include on the classpath for you.
http://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html

Merge i18n properties file in maven overlay plugin

I have two web application projects in place, Project A and Project B. Project B has war dependency on Project A. Both projects have messages.properties files to handle i18n. However, the location of properties is same for both the projects. I am using maven war overlay plugin to overlay files of Project A on to Project B. If files are in the same location for these two projects, maven will not override Project B's files and leave them as is. However, this leads to maintenance problems as new text for i18n has to be added in messages.properties of both the web applications.
Is there a way to tell maven war overlay plugin to merge the properties files at the time of packaging? The logic of not overriding files when already present serves us well otherwise.
You should be able to achieve this with the Maven Cargo plugin outputting an uberwar.
http://cargo.codehaus.org/Merging+WAR+files
It doesn't know how to merge properties files out of the box, but it's super easy to implement a custom merger, all you need to do is implement org.codehaus.cargo.module.merge.MergeProcessor, and have it available on the classpath.
You probably just want to write a simple merger that can concatenate files together, unless you have duplicate properties then you might need to do something a little more fancy.

What is the purpose of the pom.xml inside a jar's META-INF folder?

Typically, a maven built jar artifact will have it's pom included under META-INF. I recently noticed that the Spring jars don't have this. So, that causes me to wonder about the purpose of that pom.
It seems like maven retrieves the pom directly from the repository when it's doing things that require knowledge of the artifacts meta-data, e.g. when determining dependencies.
So, what's the embedded one for?
The Maven docs suggest two reasons for the pom in this location.
1) Merely for reference, as a convenience. As the docs say, it makes the artifact "self describing"
2) You can get at this information from within your application using Java. This enables the arfiact to auto-report it's version within the application.
http://maven.apache.org/guides/getting-started/index.html
The pom you will find in the repository is not necessarily the one used to build the artifact. It is aimed at the users of the artifact and can be customized when building your artifact.
The one included inside the artifact IS the one used to produce the artifact.
There are options to not have it included in the artifact.

2 WARs in maven EAR build

I am new to maven and as a matter of fact new to the build tools and process or should i say the whole web structure. I have a slight problem for which i need help.
I am going to make a web project which on compiling/deployment will give a war file. And i have a separate project for which the war(just the war file after bundling the project) file will be given to me.
Now my requirement is to make a EAR file comprising both my project bundle and also including the war of the other completed project. I need to use maven for this.
I know the multi-module projects can be created using maven. But i am not sure how it will handle isolated war file. I mean for my project it will have the whole structure , artifactId and groupId. What about the other war how will it be handled.
I know this may be a novice question. But can someone please help.
Thanks
The way this would typically be handled would be making the isolated WAR file a dependency. Install it in your local maven repo and just list it as a dependency in the POM for your EAR module. Also, making your WAR project a module under your EAR project would handle the WAR that you're actually building.

Resources