maven发布项目到本地tomcat

添加plugin:

<plugin>
				<!-- 使用maven的tomcat插件打包并部署war文件到tomcat -->
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.0</version>
				<configuration>
				    <path>/${project.build.finalName}</path>  
					<url>http://localhost:8080/manager/text</url>
					<server>tomcat</server>
					<username>tomcat</username>
					<password>password</password>
				</configuration>
			</plugin>

			<plugin>
				<!-- 这里版本为1.7 请注意编译版本 否则会报错 -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
其中用户名和密码的配置可以理解为maven为了和本地tomcat打交道而由tomcat的配置的口令,首先要在tomcat安装目录下conf文件夹下的tomcat-users.xml文件中配置:
<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
    <!-- maven发布到tomcat -->
   <role rolename="manager-gui"/>    
   <role rolename="manager-script"/>    
   <user username="tomcat" password="password" roles="manager-gui, manager-script"/>
  
</tomcat-users>

这里配置好后,启动tomcat,在浏览器输入localhost:8080/manage会让你输入用户名密码,输入这里配置的用户名/密码,若验证通过会跳到tomcat web application Manager页面,就说明该配置是成功的,就可以往下进行;若验证不通过,会跳到一个说明页面,里面有对各role的说明(想了解上面role的童鞋请自行查看),此时再修改配置,直至验证成功。

<servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the id attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
    <server>  
        <id>tomcat</id>  
        <username>tomcat</username>  
        <password>password</password>  
    </server> 

  </servers>
配置maven的settings.xml文件,至于settings.xml文件在你机器的哪里,请自行查找。这里的用户名和密码都与tomcat-users.xml中验证通过的用户名密码一致,这里的‘id’要与pom文件的<server> tomcat</server>一致,当然pom文件的用户名和密码也要对应。

共有三处进行了用户名密码配置。

选择eclipse的绿色启动按钮的下拉列表的run configurations...,新增一个maven指令“tomcat7:deploy”,运行指令前先启动tomcat,

不出意外就发布成功。

经验分享 程序员 微信小程序 职场和发展