- [Cameron] Hi, I'm Cameron McKenzie
I'm the Editor-in-chief over at TheServerSide.com
and I want to talk to you about how easy it is
to deploy WAR file to Tomcat using Apache Maven.
Now, in order to run this application,
we need three things.
First, we need an application
to run packages of WAR file and deploy.
We need Maven installed,
which you can see I've already got installed here.
There's a tutorial on The Server Side
that talks about how to install that.
And also I've got Tomcat nine here,
also another tutorial on The Server Side
to show you how to install that.
But those are the key three things that we need.
We need an application to install Tomcat and Maven.
I'm also gonna look at Tomcat.
Notice the web apps folder doesn't have any WAR files
in there or anything other than the default applications.
The goal is to deploy a rock-paper-scissors web application
named rps.war in here and get Tomcat to deploy it
having done all that through Maven.
As I said, I need an application to install.
I've actually got an application up on GitHub.
My GitHub account is github.com/cameronmcnz
And the repository is rock-paper-scissors.
If you can find Cameronmcnz up there,
you can just click on repositories
and look for rock-paper-scissors.
There it is there.
And I wanna clone that application,
bring that application local.
Don't we just have to click clone or download.
You can just clone it like this
if you've got Git installed locally,
if not you can just click download zip file.
Although I will say I actually want to switch
to a special branch named patch-1
If you're just on the master branch here,
you're not going to have the right settings
in the pom file.
So I'm probably doing this with
Git is your best bet
So you are gonna open up the Git batch,
again, make sure I've got this URL cloned.
(keyboard clacking)
And clone that GitHub repository.
And so that's great.
I've got my application cloned.
As I said, I did want
to check out the patch-1 branch.
And so now that we've got that in the patch-1 branch
of that repository, I'm good to go.
The other branch packages things as a JAR,
not a WAR file.
So that would mess things up for this example.
So I have an application, I want to package this
as a WAR file and deploy it to Tomcat.
In order to do that, I have to have a user
with the appropriate rights set up in Tomcat.
So that means going into Tomcat,
making sure that you have a user.
So you go into Tomcat, conf, tomcatusers.xml
It looks like I don't have any good users in there
so I need to add one.
I'm gonna add this user,
the username is gonna be war-deployer
The password is maven-tomcat
And the role that this user needs to be in is manager-gui
I've also added manager-script and I think
that's using Jenkins and also manager-jmx
They say that this can open your site up
to cross-site forgeries,
if you've got all of these, a user in all of these roles.
So they see the best practices is just habit in manager-gui
and not the other ones.
But I'm gonna live dangerously here.
So I've saved that,
I've added a new user war-deployer, maven-tomcat.
I know I need to go in and just start that Tomcat server,
if you already had the servers started,
you might wanna bounce it.
(keyboard clacking)
Because I think that is only read at startups.
So you may need to bounce the server
if you've done that already.
Okay, life is good there.
So I've now added my user.
The next thing I need to do is I need
to go tell Maven about that username and password.
So I'm gonna dig into my Maven installation,
into the conf folder of Maven,
go into my settings, that XML file.
And I need to configure Maven
and let Maven know about that server.
So if you go into Maven,
you'll see into the settings.xml file in Maven,
and this isn't the pom.
This is the settings.xml file.
You'll see a section to configure servers.
And what I want to do is I wanna configure a single server.
I'm gonna call it the maven-tomcat-war-deployment-server.
Can you use that name inside the pom files?
It's very important.
Make sure you get the spelling of that right
and everything correct.
And from here, I've specified the username war-deployer
and the password maven-tomcat.
And again, that is right out of the Tomcat users.xml file
that I just configured.
So war-deployer, maven-tomcat .
Now in the Maven settings, war-deployer, maven-tomcat.
Okay, I'm gonna save that file
and now I'm gonna go into the pom of the application.
And so here we have the java-web-app, rock-paper-scissors.
There is the pom.xml file.
I'm gonna edit this with notepad,
make sure the packaging is WAR.
If it's JAR, this won't work.
And then I'm gonna need to configure a plugin here.
And let me say,
I got a bunch of different plugins already configured.
I'm gonna add in the Maven Tomcat plugin.
That's a lot of different plugins,
I wonder if I need all of those.
Well, regardless, I'm just gonna throw my new plugin
into the top.
So I'm just going to throw my new plugin into the top here
(keyboard clacking)
And here you can see in this server,
I'm referencing the tomcat-maven-war-deployment-server
The URL is localhost:8080/manager/text.
The path is gonna be /rps
That's gonna be the path for my application.
I notice maven-tomcat-war-deployment-server
that matches what's in the settings.xml file of Maven,
maven-tomcat-war-deployment-server.
So I'm gonna save that, by the way,
if you want to go and take a look at that URL,
you can always go to localhost:8080
and go take a look at the manager application,
we don't want text, we just want manager.
And then there's the manager application.
I've already logged in so it didn't challenge me.
But if you want to test your credentials,
just go to that URL and login.
But now I've got everything configured.
I've got my plugin.
I've validated the manager URL and the /text
is added in there.
I'm referencing the Maven Tomcat plugin.
I should be able to run this application,
actually have this deployed very easily by Maven
to my server.
And so I'm just gonna go into my web app.
Find the pom.xml file.
Make sure it's there ,open up a batch shell from here.
And the command is simply mvn install tomcat7:deploy
So I'll click enter.
It may download a few plugins. It does this.
So it may take a moment.
After it downloads all the required plugins
and builds the application,
it then gives me a little message here.
It says that we've deployed to the /rps path.
We gonna double-check Tomcat,
look under web apps.
Indeed that file is there.
And if I open up a browser,
I should be able to go to localhost:8080/rps
And all of a sudden my rock-paper-scissors game is deployed
and all functioning on the web.
And how did I do it?
I did it all simply by using Tomcat
and the Maven Tomcat plugin.
And that's how easy it is to employ a WAR file
using Maven to Tomcat.