Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy to tomcat using Maven and Jenkins in test and prod environment

I am writing the build scripts for my application and am wondering what is the best practice to deploy/release to test/prod environment.

Currently what I have is written is

  1. Stop the tomcat server using ssh.
  2. Build the war using Maven
  3. Remove the war from tomcat webapps dir using ssh
  4. Transfer the built file using ssh to the webapps dir on tomcat
  5. Start the server

I know its not the best way to deploy an app and would like your feedback. What I would ideally like to do is

  1. Perform a maven release for prod env. Upto a revision number if possible.
  2. Tag the code.
  3. Upload the released war to a S3 folder that contains all the wars and their history.
  4. Write a script on the servers/Jenkins that will download the file from S3 (version number if version is provided else latest) and deploy to tomcat.

I am confused whether this step 4 should be in Jenkins or on the servers. I am using EC2 instances for hosting the web application so my images need to have the script.

like image 657
NEHAVERMA8 Avatar asked Feb 03 '26 12:02

NEHAVERMA8


2 Answers

The first thing is that you are mixing build and deployment. I would suggest to do building via Maven. Deployment is a different story.

I would suggest to change your build in that way to produce with a single run in Jenkins or on command line artifacts which can be deployed in test and production.

I mean with producing artifacts making a release via Maven release plugin, cause you are delivering artifacts to test and/or prod which must be reproducible.

These are the thing producing the artifacts afterwards you have them within your repository manager (Artifactory, Archiva, Nexus) and the next step is to deploy them to the appropriate areas like test or production. This can be achieved by Maven via maven cargo plugin or tomcat plugin etc. or other plugins. But i would suggest to do this via different things like puppet, chef etc. or may be by home brewn scipts/tools. The scripts can be running via Jenkins without a problem (shouldn't be a problem). The deployment itself shouldn't be part of the building it should be made separately. If you are writing your own tools you could take a look at Aether library to make access to Maven repositories simpler.

like image 100
khmarbaise Avatar answered Feb 05 '26 00:02

khmarbaise


In term of organization, you are mixing building and deploying, copying, ...

  • jenkins should be used to checkout the source. There are various plugins to checkout a certain version from the scm (even if CI is meant to always run against the latest version by default): http://wiki.hudson-ci.org/display/HUDSON/Subversion+Release+Manager
  • maven should be used to pre process, filter resources, compile and package the war
  • the post process tasks from jenkins can be used to copy / archive the generated war
  • a jenkins plugin such as https://wiki.jenkins-ci.org/display/JENKINS/Deploy+Plugin (based on cargo) can be used to deploy the archive

maven is a tool for building the archive when jenkins is an integration tool.

You should :

  • keep a clear border between the compilation and the integration
  • use the tools for what they have been designed.

HIH M.

like image 34
poussma Avatar answered Feb 05 '26 01:02

poussma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!