记录Eclipse将普通web项目转为Maven项目以及远程发布到本地tomcat遇到的问题

1、转Maven项目

鼠标右键项目,然后选择Configure>>Convert toMaven Project
在这里插入图片描述

2、转完之后需要作如下配置

(1)pom.xml文件
<build>
<!--首先必须要保证xml、properties文件编译时要包括在内-->
		<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
	<plugins>
	<!--这个compiler插件是自动生成的,版本和编译jre环境可以自己定,这里需要注意的一个就是你的项目中可能会有一些中文编码,那么encoding可配置成utf8-->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>utf8</encoding> 
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
      <!--配置远程发布的服务器地址,账号密码注意要在tomcat的conf路径下的tomcat-users.xml文件中配置localhost:8080可换成你要远程发布的地址,其他不要变-->
      <plugin>
    		<groupId>org.apache.tomcat.maven</groupId>
    		<artifactId>tomcat7-maven-plugin</artifactId>
    		<version>2.2</version>
    		<configuration>
    			<url>http://localhost:8080/manager/text</url>
    			<username>admin</username>
          		<password>123</password>
                <update>true</update>
    			<path>/${project.artifactId}</path>
    		</configuration>
    	</plugin>
    </plugins>
</build>
(2)tomcat-users.xml配置
<tomcat-users>
	<role rolename="manager-gui" />
    <role rolename="manager-script"/>
    <user username="admin" password="123" roles="manager-gui,manager-script"/>
</tomcat-users>

然后配置完之后需要update一下,鼠标右键项目>>Maven>>update project…
在这里插入图片描述
然后我们可以执行compile命令试一下有没有问题
在这里插入图片描述
在这里插入图片描述
这里Maven Build和Maven build …的区别就是前者会列举出你执行的历史Goal,后者可以自己输Goal

3、远程发布 tomcat7:deploy / mvn tomcat7:undeploy快速卸载

在这里插入图片描述
我们以debug模式发布,需要调试源码,所以可以在source选项卡点击Add加上源码
在这里插入图片描述

4、错误问题

(1)maven 编译时报错:编码 UTF-8 的不可映射字符

maven-compiler-plugin插件的encoding看看配了吗,配完了之后记得update project一下

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>utf8</encoding> 
        </configuration>
      </plugin>

项目鼠标右键>properties>Resources>改编码为UTF-8
在这里插入图片描述
还有一个终地方不知道需不需要改,找到pom.xml文件,鼠标右键>Run AS…>Run Configuration>Common栏中编码格式更改

(2)远程发布失败,可能报错java.net.SocketException

看看tomcat-users.xml加角色和用户了吗

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
    <role rolename="manager-gui" />
    <role rolename="manager-script"/>
    <user username="username" password="password" roles="manager-gui,manager-script"/>
</tomcat-users>

如果还是失败,报错java.net.SocketException,检查你的pom文件里的账号密码对不对
如果还是报错,但是报的错误是403,那么tomcat7及其以上版本需要在发布项目前确认是否配置IP限制
tomcat的webapps文件夹下的host-manager和manager,都有一个共同的文件夹META-INF,里面都有context.xml,

<Context antiResourceLocking="false" privileged="true" >
 <Valve className="org.apache.catalina.valves.RemoteAddrValve"
     allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />
</Context>

这段代码的作用是限制来 访IP的,127.d+.d+.d+|::1|0:0:0:0:0:0:0:1,是正则表达式,表示IPv4和IPv6的本机环回地址,同时这也解释了,为什么我们本机可以访问管理界面,但是其他机器确是403。

找那么修改一下这里的正则表达式即可,我们修改为所有人都可以访问,那么改成这样就可以:

<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="^.*$" />
</Context>
(4)有些包,例如jsp或者servlet 的包要注意,如果你项目中导入了,那么就会与发布的tomcat服务器中的jar包冲突,这时候你需要把jar包写成pom依赖,scope作用域为provided,这样仅仅编译时使用,而不会在打包的时候打进去
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值