Automate custom war files using maven - java

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.

Related

Prevent intermediate Maven artifacts from being uploaded

I have a project creating some FAT jar to be used via webstart loaded from an ear. This leads to the following Maven projects in a multi-module project
jar project creating the FAT jar
war project creating the webstart war
ear file containing the server application and the war file for webstart
The jar and war artifacts are only used as intermediate artifacts to be able to create the ear artifact. However, Maven handles them as artifacts and so they are always uploaded to Nexus.
Since the jar is getting real fat, I would like to avoid uploading it 3 times to nexus for each build. Is there a way to avoid this? I can think of two ways:
Moving that three projects to one pom file
Some flag / option / whatever telling Maven to handle artifacts as intermediate artifacts not being deployed or installed
Does something like this or any other solution to my problem exist?
If this is a multi-module project, you can set <maven.deploy.skip> to true in the projects where artifacts should not be deployed.

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

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.

Is there a way to point jar libraries in a different location from a war in tomcat 8 to reduce war size?

I have my web service made in Grails/Gradle which contains a lot of dependencies jars making the war size heavy which is deployed in tomcat8. Is there any way that my war would use the dependencies from separate location so that my war would be free from these bulk dependency libraries.
If you are using maven for instance you could do something. In all dependecies you dont want to include into your WAR file add the tag.
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
<scope>provided</scope>
</dependency>
Then when maven build your WAR file, a folder lib-provided will generated, you can get rid of this folder and reduce the size of your war.
All those dependecies you dont want to include in your WAR, you should place them into your TOMCAT_HOME/lib and Tomcat will check the libraries of your project and also the jars located in this folder.
IMPORTANT: I would never do this option, because you are doing your WAR dependent to some specific application server, if you try to deploy your WAR in another Server, wont work, if you update your maven dependencies you have to manually update them in your application server.
So I highly would go for another option rather than this, and I would keep your WAR independant to any application server.
Check your project into Git (of course without jars) and create a script on your server to check it out, build it (with Maven or Gradle) and deploy it to Tomcat.
Have a look at a Continous Integration system like Bamboo or Jenkins, to do that automatically.

How to Move Maven Target Archive files into Server Deploy Folder?

I have a Maven, Java project with Eclipse and Jboss 4. Currently, after doing clean, install goals I am moving EAR and WAR files into Jboss Deploy folder manually. But every time this process hard to do. Are there any plug-ins or tools to do this job? I tried the "org.codehaus.mojo" plug-in but it did't work. Are there any other goals in Maven to deploy automatically to Server from Eclipse?
For such purposes you can use cargo which is intended for functional tests but supports the deployment of artifacts like ear, war into appropriate container.
may maven-war-plugin can help you

Correctly Deploy maven project

I have a project with maven, this a multimodule project
Mainproject
project1 - jar
project2 - jar
project3 - web
Now that I finished the project I want to test it on the server but don't know how to upload them, on my computer I have added a plug for tomcat which deploys the war file to tomcat automatically, but the server doesn't have maven.
What is the way for moving to production with this kind of projects?
Should I just upload the target directories for each module?
Thanks
You're asking a few questions here. There is the "how do I test on a server" one, and there is "what is the way for moving to production" one. And they can be quite different.
I have assumed that the JAR files in the project are used by the WAR file and packaged within its WEB-INF/lib directory. If I'm wrong, that's cool. Just this sort of information is handy to know.
Maven is a build tool. It is not a deployment tool. What you have at the end of this, is a WAR file. If you run mvn install (or mvn deploy) you have a SNAPSHOT version of the WAR file. This would be suitable for quick, ad-hoc testing to other machines. But you would most likely use methods approriate for the hosting container for making them available. Note: a Maven DEPLOY is a different thing from a DEPLOY a war file to tomcat.
To my mind, if you're putting anything into production, or in front of a customer, or in front of a testing group, you should use Maven to make a Release of the product. That is, using the release plugin (via the release:prepare and release:plugin goals) to create a non-SNAPSHOT release of your artifact (in this case, a official version of the WAR file). Once you have that WAR file, you can migrate it to your production server in whatever way is easiest (copy, deploy into tomcat in the best way). You haven't mentioned if there are database requirements for this web application, and that would need to be considered before you change any production application.
But, once you have official versions, you have tags of source code, and you can accurately know what code is being run.
What I don't think you're going to get is being able to automatically copy the WAR file into a production server from your development environment. Here be dragons.

Categories