Skip to content

Instantly share code, notes, and snippets.

@hamid-rostami
Created March 25, 2022 17:23
Show Gist options
  • Save hamid-rostami/aa1021499cbb4a9c5899fdab11efd9dc to your computer and use it in GitHub Desktop.
Save hamid-rostami/aa1021499cbb4a9c5899fdab11efd9dc to your computer and use it in GitHub Desktop.
Deploy Maven project to Tomcat

By following this instruction, you will be able to deploy your Java web application to your Tomcat server from command-line just by a simple command:

mvn tomcat7:deploy

First of all, make sure you have installed tomcat server. Then you need to create a user with manager-gui and manager-script privilages. You can do this by editing tomcat-users.xml file. On a GNU/Linux machine, it must be located under /etc/tomcat directory. Open it by a text editor and add this line (Don't forget to edit your-username and your-password).

<user username=your-username password=your-password roles=manager-gui,manager-script/>

Then, you need to tell maven about the user you just created by editing settings.xml file. It's usually under ~/.m2/ directory, but if there isn't such a file, don't worry and create it and put below content in it. Again, please change your-username and your-password to actual user/password.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
        <servers>
    <server>
      <id>TomcatServer</id>
      <username>your-username</username>
      <password>your-password</password>
    </server>
  </servers>
</settings>

In the last step, you need to install tomcat plugin in your maven project. To do so, add these lines to your pom.xml file. Just remembert to edit path section based on your preferences. In this case it's /myapp, but you can change to whatever you want.

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <url>http://localhost:8080/manager/text</url>
        <server>TomcatServer</server>
        <path>/myapp</path>
    </configuration>
</plugin>

Now, it's time to deploy your project. Go to your peoject directory and type: mvn tomcat7:deploy

No need to mention that these commands are also available:

mvn tomcat7:redeploy
mvn tomcat7:undepoy

PS: Although I have installed Tomcat/10.0.16 in my machine, this instrunction worked for me.

@dinu-359
Copy link

k;l'kmknkm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment