How to include libraries during war file built in spring project? - java

I have a spring project and it is running well. The problem is, whenever I perform a new deployment using the war file in tomcat, I need to manually copy the libraries in the lib folder. I am wondering if there is any way to add those libraries inside the war file so that every time I don't need to waste my time by copying the libraries in the server. It needs to be automatically included from the generated war file.

This answer depends on how you are building your WAR but I assume you are using one of the popular build tools for Java. Given that your war does not have a populated lib folder I'm assuming you have either not configured the right plugins for these build tools or you are compiling and managing your classpath from the command line like a madman. If the later is the case then I highly suggest you start using a build tool for your projects (All IDE's do this by default), if the former is the case I refer you to the individual pages for the plugins:
Maven:
https://maven.apache.org/plugins/maven-war-plugin/index.html
The war plugin does this by default.
Gradle: https://docs.gradle.org/current/userguide/war_plugin.html
The War plugin for Gradle also does this by default.
Ant: https://ant.apache.org/manual/Tasks/war.html
A glossary reading for the Ant tasks did not reveal if it did so by default but I'm assuming it does.

Related

Automate custom war files using maven

I know about maven war overlays, but they assume that the original war file is a maven project.
What if I only have access to the packaged war file and I need to modify it by adding new resources or updating few values in properties file using maven and push the new overplayed packaged war file to tomcat on a server?
Is it too complex to do this in maven?
If I install the war file in my repository does the maven build process automatically use it?
It doesn't assume that the WAR is a Maven project, whatever that means, just that the WAR is a Maven artifact. You can install any WAR as an artifact, it does not matter how it was built. If you then declare a dependency to this artifact, you'll be able to overlay it. While it is easy to add and replace files (just create a normal web project structure containing the new files and the WAR plugin will do the rest), it is not easy to modify them. If you really wanted that, you'd have to run something like the replacer plugin after the original WAR is extracted but before the package phase of your project. It is also possible to download a (WAR) file instead of getting it from a repo by using some exotic plugins, but this is probably not something you should be doing as it blows away half the benefits of Maven.
As for deploying the newly-built application to a remote Tomcat, while doable using the Tomcat plugin, it is not something I would be doing from Maven. It is usually a job for a build server, like e.g. Jenkins, while the Tomcat plugin is more for local development and testing.

Eclipse Dynamic Web Application putting jar files in WEB-INF/lib and Java Build Path

I have a very "basic" question regarding Java web application development (servlet). What is the difference between putting a jar file in WEB-INF/lib and putting it in Java Build Path/Library/External Jar?
Java Build Path/Library/External Jar is only managed by eclipse so if you build war and if your builder (or if you build externally) doesn't copy over these jars to WEB-INF/lib then you would loose them at runtime
also when you share that code with other developers, or if you use other IDE it will not help
better to use some standard and mature dependency manager / build tool like maven than copying over library to WEB-INF/lib manually

Separating external jars from plugin lib direcory

I have a plugin that depends on external jar files.
I've made an update site to install the plugin into any eclipse.
These external jar files are shipped with the plugin.
I want to separate these external dependencies so that the plugin needs them during the installation and get them at the installation time. or search for them in a specific location away from the plugin jar file
Is that possible?
If so, How can I achieve that ?
Create separate plug-in's for each of the external jar's?
Like the way Eclipse handles things like Apache components.

Maven Jar signing and target rearrangement

I'm converting an Ant based project to be buildable by Maven. Standard build is working for now. I'm trying to migrate the additional build targets which are specified in the original Ant build descriptor. Our project can be deployed as a desktop application or as a Web Start launchable client. You can invoke the original Ant file naturally with dist, which simply builds the project but doesn't do post processing for web start, and also there's a dedicated target for JWS which calls dist, then do jar signing and rearranging the distribution files to be be easily deployable to the web server.
I've found out that Maven has a Jarsigner plugin for doing code signing. My project currently builds the core jar, copies all artifact dependencies to target, creates the correct manifest file for the core jar, and also unpacks the configuration artifact to target/ (this contains various things, like XMLs and property files). However i don't know how to fit into the Maven descriptor the following additional steps:
Sign all jar files (also external dependencies), removing existing signatures as well.
Rearrange the resulting jar files to a different directory layout. I also need to edit XML files for this configuration and pack them to a configuration jar which needs to get signed also.
After modification the project have to be buildable the standard way. So Web Start build have to be optional. I should note that we are using NetBeans to build/debug/profile the application.
I'm a little bit lost how to achive these with Maven. Could someone please give some suggestion how should i move forward?
I've solved my problem this way:
I've created a new jar module for the web start client configuration. During build Maven unpacks only those parts of the original config artifact to the build directory which are required for Web Start (you can specify filter rules for the dependency-unpack plugin). Then i'm using the Maven XML plugin to modify XML files using XSLT templates. Finally Maven takes care of packaging all these to a web start client configuration artifact.
I've also created a pom only module for the actual web start client building. This project has no classical artifact output so only the POM gets installed to the artifact repository. This module has dependencies for the source code (excluding original configuration) and for the web start client artifact. I'm using the dependency copying capability of Maven to arrange jar files in the correct structure in target directory. Finally i'm using the Maven jarsigner to sign all jars.

Apache Ant + Ivy build of a web application which is deployed to a local Tomcat

I will use Apache Ant and Apache Ivy to build a web application which is deployed to a local Tomcat instance (during development). I have some questions:
I want to grab most of my dependencies from the Maven2 repositories which works fine, but for the servlet JAR I would like to use the one Tomcat provides. Is there a way to do that using Ivy? And what do you think about doing so?
I download the Ivy JAR using Ant to "auto install" it into ~/.ivy2/jar/ivy.jar and I have the Ivy cache at its default location ~/.ivy2/cache . I keep both of these locations outside the project directory on purpose. Good idea?
Do you have an example of how to use Ant for the build file, Ivy to resolve dependencies, compile a WAR file and deploy it to a local (at the moment) Tomcat installation? I'm looking for something to have as a best practice which I then can modify further.
I've used some examples on the Ivy web site and modified them. Is there anything I should change? The build file can be seen at http://pastebin.com/f7b34abc2 , as I had problems pasting XML code in here.
(Please notice that I'm not looking for the suggestion that I should use Maven2, even if the suggestion is well intended.)
At runtime tomcat will use it's own away as it's classloader will exclude any jar with javax.servlet.Servlet in it. If you really really must compile against it you will have to do it in the ant script, and either copy it or reference it in the compile classpath.
On the other stuff, downloading ivy on demand is a good idea as it will prevent checking the ivy jar into the project scm repo as I have done in the past, whether you download it to the project directory or the user home is a matter of personal preference. The ivy cache on the other hand should be shared with other projects so the user home directory is a good location.
Building the war file isn't any different once you have done ivy:retrieve as the jar files required will be local to the project, just use the ant war task to create the war as normal. This is one advantage to ivy, that once the jars are downloaded it has no more to do in the build and you can just use ant to compile and package your project.
Had a quick look at your build file, looks perfectly sane.
Hope this helps.

Categories