How to deploy war file to Tomcat from Jenkins without using plugin - java

I want to deploy from my Jenkins to Tomcat without using this "Deploy to container" plugin .
So I am thinking of a Tomcat REST API to deploy a war file. The Tomcat and the Jenkins are on different servers.
Maybe it is enough to just copy the file to the webapps folder ? But then I still have to deploy it meaning to start it . Right ?
Honestly I have not found any REST API to deploy to Tomcat so far. The problem is to make the .war file available .

From Jenkins, you can use a bash script to transfer the .war file to your Tomcat webapps directory (the code is the same if you work with a pipeline).
First, stop the Tomcat
ssh tomcat#tomcat '${TOMCAT_BIN_PATH}shutdown.sh'
Then, copy the war to the webapps directory of your Tomcat
scp -r -p jenkins#jenkins:path/to/war/file.war tomcat#tomcat:path/to/webapp/
After that, you have to start the Tomcat and the .war will be deployed automatically
ssh tomcat#tomcat '${TOMCAT_BIN_PATH}startup.sh'
If you are using Maven, I suggest you to take a look at tomcat-maven-plugin, you can find it here. You can deploy the war file with the following command:
mvn -B -P deploy-profile-from-pom-xml tomcat7:deploy-only
An example of implementation can be found here
UPDATE
If you want Tomcat to delay the webapps directory checking, you can add backgroundProcessorDelay to your server.xml configuration
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" backgroundProcessorDelay="15">
Tomcat will only check for a new .war every 15 seconds with this code for instance.

Related

How to deploy a WAR to Jetty by copying to $JETTY_HOME/webapps?

Having installed Jetty, it's certainly running as I get a welcome page.
How do I manually deploy a WAR to Jetty?
If you have a standard web application, you can hot deploy it into
Jetty by copying it into the webapps directory.
Where is the webapps directory? Or, how do I find the directory?
See also:
Deploying by Copying WAR
The easiest way to deploy a web application to Jetty server is
probably by copying the WAR file into the $JETTY_HOME/webapps
directory.
output from dpkg -L jetty9 as a gist shows:
/usr/share/jetty9/logs
/usr/share/jetty9/start.d
/usr/share/jetty9/start.ini
/usr/share/jetty9/start.jar
/usr/share/jetty9/webapps
and:
/var/cache/jetty9
/var/lib
/var/lib/jetty9
/var/lib/jetty9/webapps
/var/lib/jetty9/webapps/README.TXT
/var/log
/var/log/jetty9
/usr/share/doc/jetty9/README.Debian
On Ubuntu Jetty by default webapps directory is /var/lib/jetty/webapps.
To be sure, please check list of installed files (and to which directories they have been copied):
dpkg -L jetty

Server tomcat not deploying latest WAR changes

I'm new to deployment process. Kindly advice me.
I have created a WAR file in my development machine. Then in the server machine, I have installed Tomcat which is clean and new. Then I copied the WAR file and placed it under webapps. Then I started the server, it worked.
Later I made few changes in the code and created WAR file and deployed the same in webapps folder. But it is not taking the latest changes.
I did in the following way
1. Stopped tomcat instance by shutdown.bat
2. Placed the new WAR file in webapps.
3. Started the tomcat instance by startup.bat
Still no changes are reflecting? What could be the reason?
Manual deploy:
To deploy or redeploy a war file is simply dropping your war file into the $CATALINA_HOME/webapps directory of a Tomcat instance. If the server is running, the deployment will start instantly as Tomcat unpacks the archive.
If Tomcat is not running, then the server will deploy the project the next time it is started.
Sometimes you need to stop Tomcat and delete everything from $CATALINA_HOME/temp, $CATALINA_HOME/logs directories and delete the war and the related unpacked folder from $CATALINA_HOME/webapps directory. After that just copy your war to the right place and start Tomcat.
If the deployment is finished without error then you can see the following message in the tomcat log:
OK - Deployed application at context path /myapp
Otherwise an error message will appear which describes what was the error.
We need to see the Tomcat logfile if you need more help.
you should clear the log and temp files of tomcat
Try checking the autoDeploy flag in server.xml. Below is the description for this flag.
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
autoDeploy
This flag value indicates if Tomcat should check periodically for new or updated web applications while Tomcat is running. If true, Tomcat periodically checks the appBase and xmlBase directories and deploys any new web applications or context XML descriptors found. Updated web applications or context XML descriptors will trigger a reload of the web application. The flag's value defaults to true.
1)Please check your war file size. Then update it in the =apache-tomcat-9.0.53\webapps\manager\WEB-INF\web.xml file.
2)Remove dependency "javax.validation" from POM file
3)Add dependency org.glassfish.web
4)Check your log files. and modify dependencies accordingly.

WAR file can't be deployed on Tomcat 7 without wtpwebapps folder

I am facing a problem by deploying a War file on a Tomcat Server.
My Tomcat Server configuration as follows:
My steps to deploy:
I created a WAR file in Eclipse Kepler (Windows 7 64 bit, no Maven used).
Uploaded it with a Tomcat Web Application Manager.
Clicked deploy.
Tomcat unpacks WAR file in tomcat/webapps and throws me NullPointerException. It is missing some files that should be imported to project to work it right.
When I was working in Eclipse, as a source for these files I gave a workspace path, given in the Server settings and adding the wtpwebapps folder, which was offered by Eclipse by default. So my complete path looks like this:
C:\Users\myUserName\kepler\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myProjectName\WEB-INF\classes\project\fdsProject\FDS.prj
Now, when I deploy the project, it locates in a webapps folder, not in wtpwebapps and if I try to start in a browser, it shows me NullPointerException. But as soon as I copy the wtpwebapps folder from my workspace to the tomcat installation folder, near webapps, my project is starting successfully.
It means that I cannot start my project on another server, just deploying a standalone WAR file. I must add manually the wtpwebapps with my project in into the tomcat server installation folder. How can I optimize it and start project just with a WAR file deployed?
Thank you!
EDIT
My project structure is looking like this:
Stop the Tomcat Server
In the settings view Tomcat Server settings in Eclipse -> Server
Locations, check Use Tomcat Installation(...)
Now, you can change the Deploy Path, if you want webapps, then input it.
EDIT
The values below control how tomcat deal with war file
autoDeploy="true"
unpackWARs="true"
Just search these values in your server.xml file and edit values as you need.
If you want to be able to deploy your app as a WAR file, you need to refer to everything within it relative to the app itself, not with absolute file paths, not even as file paths if you can. If you have code that requires a java.io.File, rewrite it. The point is that you don't care where things are, you just care about how to get to their contents. For a Servlet, you can read anything on the web app's classpath by calling getResourceAsStream() on the correct class or classloader.
See getResourceAsStream() vs FileInputStream .

How to Dockerize a tomcat app

I am trying to dockerize some Tomcat application but I never touch Java application before so the lack of understand it makes it really hard to understand what should I do.
So far I have this but it doesn't work and I don't if it's the correct path as well
FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk
COPY . $APP_ROOT/
WORKDIR $APP_ROOT
RUN jar -cvf app_name.war *
# this fail for some reason, when I do `ls` the file is there but if fail to copy it
COPY app_name.war $CATALINA_BASE/webapps/app_name.war
I am just going on loop on this because I don't understand and Google Search do not help me that much (I don't know how to ask).
Should I use the jar command in the build? If not, I guess I have to build it locally and just make sure that the .war is there right?!
How the building of the Java with Tomcat app works? and How to integrate with Docker?
Sorry for being too generic but I don't understand anything about Java
Looking at your code this is what I could gleam:
You have some java files stored in current directory (.)
When you call COPY you copy all these contents to /app_name
You create a .war on the file
There are some things to note, first is that the app_name.war is not on the host disk, it is currently inside of the docker file system. What this means is that you cannot COPY the .war.
What you are really after is this: RUN cp app_name.war $CATALINA_BASE/webapps/app_name.war
This would look like the following:
Dockerfile
FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk
COPY . $APP_ROOT/
WORKDIR $APP_ROOT
RUN jar -cvf app_name.war *
RUN cp app_name.war $CATALINA_BASE/webapps/app_name.war
Adding the docker COPY reference here as it explains the command in detail. It might also be helpful for you to make a script called provision.sh, then do something like:
COPY provision.sh /tmp/provision.sh
RUN sh /tmp/provision.sh
That way you can put all your building, configuring and other in a single script that you can test locally (again if it helps)
EDIT: Adding mention about building locally and copying into dockerfile
You can build the .war on your machine, use COPY to put is on the machine.
Dockerfile
FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk
COPY app_name.war $CATALINA_BASE/webapps/app_name.war
WORKDIR $APP_ROOT
The above copies the file app_name.war then add it to the filesystem of the container at the path $CATALINA_BASE/webapps/app_name.war. So for that you do this:
Build the .war on your machine with java
Put .war in directory with Dockerfile
COPY app_name.war into the container's filesystem
You can try to do this "by hand" before trying to automate it, it should help to understand the process. You don't need to extend a tomcat official image to be able to deploy a war on a dockerized tomcat, you can use the image directly if you don't need to customize permissions and users (in production, you need).
If you need Tomcat 6.x because your webapp implements servlet API < 3, do this :
sudo docker run --name tomcat --detach --port 8080:8080 tomcat:6
Now, your Tomcat is running in background (--detach), waiting for a deployment. You've exported port 8080 from the container and mapped it to port 8080 from you host, so the app will be available at http://localhost:8080/ on your host.
From now if you copy your .war in /usr/local/tomcat/webapps into the container, the app will be deployed :
sudo docker cp app_name.war tomcat:/usr/local/tomcat/webapps/
I don't use docker, I use a similar AWS product called codedeploy for provisioning instances, so I tell you what I do for Tomcat setup in my provisioning scripts. Should be easy to port to docker as just bash comands.
1) Build the WAR
Most java applications these days are built using Maven but Gradle is catching up. Maven and the WAR plugin are used to turn java code into a WAR file which you can deploy on Tomcat. But it looks like you already have the WAR built by someone else? Either way, you dont run the war directly, you put it in Tomcat, unless you've bundled Tomcat into the app, in which case it would be a JAR but lets not talk about that....The simple solution is build the war from java code using a build tool like Maven or Gradle. By build, I mean turn it from source code to binary.
2) Install Tomcat
yum install tomcat6,7,8 etc etc (Whichever version you need)
Then turn it on
service start tomcat8
3) Deploy the war
To run the war place it in the webapps folder of the Tomcat installation. I generally like to shut tomcat off when I do this but you can do it while its running. After a few seconds the WAR, which is really just a zip file, will be exploded/unzipped to create a directory.
4) Accessing the application/site
If you rename your war to ROOT.war then you can access the applicaition at http://localhost:8080 if your configuration is to have it listen on 8080. If you war is named pets.war then your webapp URL would be http://localhost:8080/pets. You configure which port for Tomcat to listen on in the server.xml file in its conf folder.
Most Important
Tomcat documentation is very good once you know what to look for. The primary configuration files are web.xml, context.xml, and server.xml. The central tomcat guides explain each component https://tomcat.apache.org/tomcat-7.0-doc/setup.html you just need to find the doc that corresponds to your version of Tomcat.

How to deploy Java/Spring application on my own system in Apache tomcat server.?

Hello all master minds,
I have created a java spring application in eclipse with mysql db.
Now I can run this application using >Run on server in eclipse,but I want to know how to deploy this application on my own laptop(windows 7).
I have already configured server,by localhost://8080 I can see Apache tomcat is configured.
Give me simple steps so that i can just run that software using browser via its link like
http://localhost:8080/PMS
PMS is my project name.
Thanks in advance.
You can install tomcat-manager in order to deploy your war using a web interface: Tomcat 7 manager
Another option is to copy your war file into tomcat webapps folder. Your container will auto deploy your war: http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html#Deployment_With_Tomcat.
The easiest way, since you are using eclipse is to just export your project as war and then put that war (naming it PMS) in webapps directory in your tomcat.
Once started, Tomcat will deploy that war on http://localhost:8080/PMS
You have to do the following steps:
Right click on project>> Export (Export as WAR file).
or if you are using a maven project then you can give a maven build.
Copy that WAR file, (you will get that war file inside the project folder in your workspace) to the tomcat_Installed_Folder/ webapps
Inorder to deploy the app from outside eclipse,
goto tomcat_Installed_Folder/bin
and double click on startup.bat
then you could see a console.
For detailed logs of deployment, goto tomcat_Installed_Folder/logs
ALL THE BEST :)

Categories