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

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

Related

Maven build using embedded JAR in project pom

I am working on a Maven project where a build is done through Jenkins, and 1 particular JAR has been removed from corporate repository recently.
So my build is failing as parent pom.xml is referring to a JAR not available in the repo.
But I have the old certified copy downloaded in my local repo, and I want to use the same JAR during build copying in the project folder and want to use the local copy of dependent JAR in pom.xml instead of downloading from corporate repository embedded in the project structure.
How can I do this?
Frankly, this does not sound like a good idea.
While it is possible to reference jars with the <systemPath> entry, it is generally considered bad practise.
By "corporate repository", do you mean a repository of your own company or of some other company? In the first case, you should request that the jar is put back in. In the second case, it would be better not to use the external corporate repository directly, but to set up your own Nexus/Artifactory through which you use (different) external repositories. This Nexus/Artifactory can then host additional artifacts like the one you need (you can e.g. upload them through the UI).

Deploy zip artifact from another build action to Nexus

Is it possible to deploy arbitrary zip archive artifacts to Nexus through Maven as snapshots?
We have a build step that is not supported through any application-specific Maven plugin. Instead, our full build and deployment process is as follows:
1) Maven POM compiles the Java component of the build, using Jenkins.
2) Shell script calls create a deployable artifact shell scripts were wrapped around calling a code generation application, which are then zipped up into an archive by the application itself. I need these artifacts deployed to Nexus as both snapshots, and as releases as appropriate.
I tried using the maven-assembly-plugin however this assumes that the plugin itself is creating the zip archive, not simply deploying an archive that was produced by some other method.
I would prefer to do this within Maven since our Nexus settings and credentials are already within the environment and do not need to be passed manually on the command line. Using the Nexus UI for this is not a viable option since this needs to be part of a standard build-deploy-test process, which may happen many times per day, for a couple dozen applications.
For completeness, I'm answering my own question (oh bother...)
I resolved this issue by using the maven-assembly-plugin, which allows you to define arbitrary artifacts, and deploy them (snapshots or releases) to Nexus. The assembly plugin uses a bill of materials (src.xml) that defines the exact contents of the artifact (either including or excluding files, directories, changing file permissions, etc). This can also be used for creating Java uber jars, but it appears that using the Maven Shade Plugin is the preferred method for creating uber jars.
Maven Assembly Plugin main webpage

Multiple Maven modules with dependency on a JAR

In my multi-module Maven project, suppose I have two modules, car and horse. They both depend on a JAR file, transport.jar, a file not available in any online Maven repositories. As such, I need to find a way to make these modules depend on a file found somewhere in the project folder structure.
From what I understand, the default Maven solution would be to manually register the JAR file in the local repository. While this would work on a development machine, it breaks on the build server, which clears its local repository before each build.
I've been searching online on how to do this on and off for a while and found some helpful things, but nothing that completely works.
For instance, a common answer is to add a dependency to the file using <scope>system</scope>. However, not only do others claim that it's extremely bad practice to do so, it also doesn't work on the build server. (On a side note, I would also like to point out that using absolute paths to the JAR is also out of the question due to, again, it being built on several different machines.)
A more useful method I found was to define a local repository in the POM file, pointing towards the path file:${project.basedir}/lib. (Such as in this article) Unfortunately, if I place the JAR and repository definition in the car POM, I cannot successfully add a dependency to the JAR in horse. I've tried both with and without an additional reference to car in horse, as well as defining a second repository in horse, pointing to file:${project.basedir}/../car/lib. This problem would also remain if I tried to make a third module, transport-lib, specifically for wrapping the JAR dependency.
I could most likely add the JAR file to both modules and define two separate module-local repositories, but I really don't want to unless I have to due to the need to keep the two (often updated) JARs in sync etc.
So, my question is as follows: Can someone give me a confirmed-to-work method to have two modules depend on the same JAR file inside the project, given the parameters and restrictions mentioned?
Best solution is to use a repository manager like Archiva, Artifactory or Nexus and install that artifact into the repository manager. Afterwards you can use this artifact directly in your pom files without any issue.
Don't use the scope system, cause it will cause other problem after a release for other etc.

Maven: Checkout a SVN Repository, compile, package into a jar and add as a Dependency

We have a web application in which we are using Ant as a build tool. There is a urgent requirement to create web services (API) and this will be a separate project. For now, to make it available to our customers we have decided to use our web application and remove all unnecessary files (like velocity files, properties, xml etc) and make a jar of it. This jar will be used in our web service project.
In Maven, I want to checkout my web application svn branch, compile it, make a single jar of it and add as a dependency in my project. Is this possible? If yes, then please show me the way.
I'm new to Maven please explain your answers with more detail.
Thanks.
Short Answer
Get your .war deployed into a Maven Repository (local or remote) from your ant build
Child Projects will embed your .war as a dependency, creating a war with your custom services + your original .war file
It is advised that those Child Projects turn into an Archetype, so creating custom services gets easier
Long Answer
From your SCM, you could modify your build.xml file and use Maven Tasks for Ant.
In particular, the install and deploy examples are helpful in order to guide you on deployment your .war into a Maven repository (local or remote)
Then, a .war artifact (when accessible from a Repository) is able to be consumed from other .war applications.
Look into the maven-war-plugin Overlays Feature. In particular, this answer offers you more advice:
combine different maven web-projects into a single project
Other than that, I suggest you could also combine with Maven Archetypes (they're now easier than ever), so you could create skeleton projects for your webservices, already depending on this .war dependency.

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