eclipse将javaweb项目改成maven项目

步骤

  1. 项目右键–>configure–>Convert to Maven Project
  2. 将项目中原有的src下的东西复制出来,然后删除src包
  3. 项目右键–>New–>Source Folder
    分别新建 src/main/java、src/main/resources和src/main/webapp(webapp目录有人说是自动生成的,我的没有自动生成)
  4. 将项目原src下的java放在src/main/java中
    将项目原src下的配置文件放在src/main/resources中
    将项目原WebRoot下的文件复制到src/main/webapp中
  5. 将项目的lib文件夹复制出来一份,然后删掉
  6. 根据复制出来的lib,去http://mvnrepository.com/中查找对应的maven版本,写在pom.xml的dependencies中
<dependencies>
	<dependency>
		<groupId>freemarker</groupId>
		<artifactId>freemarker</artifactId>
		<version>2.3.8</version>
	</dependency>
	<dependency>
		<groupId>it.sauronsoftware</groupId>
		<artifactId>ftp4j</artifactId>
		<version>1.6</version>
	</dependency>
	……
</dependencies>
  1. 部署web.xml
    修改文件/projectname/.settings/org.eclipse.wst.common.component,该文件规定了项目怎么组装成一个webapp
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
    <wb-module deploy-name="projectname">
        <wb-resource deploy-path="/WEB-INF/web.xml" source-path="/src/main/webapp/WEB-INF/web.xml"/>
        <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
        <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
    	<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> 
        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
        <wb-resource deploy-path="/" source-path="/src/main"/>
        <property name="java-output-path" value="/projectname/WebRoot/WEB-INF/classes"/>
        <property name="context-root" value="projectname"/>
    </wb-module>
</project-modules>

或者使用项目右键–>properties修改
在这里插入图片描述

我在启动项目的时候,还遇到了个报错:“No bean named ‘shiroFilter’ is defined”,解决办法就是修改上面的文件。

  1. 修改jdk和servlet版本号
    本项目使用jdk1.7,在pom.xml中添加:
<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			
	</build>

(原文链接:https://blog.csdn.net/mafan121/article/details/51944346)
错误说明:eclipse+maven下java Resoures资源文件夹出现小红叉。
错误提示内容:One or more constraints have not been satisfied.
错误原因: Deployment Assembly跟java版本不匹配,即maven默认编译版本与使用的jdk不匹配。
该错误解决办法即为上述在pom.xml中修改jdk版本号,然后重新maven->update Project就好了。

本项目使用servlet2.5,在pom.xml中添加依赖:

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
</dependency>

web.xml中,版本号是2.5:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 

项目右键–>maven–>update project后,/projectname/.settings/org.eclipse.wst.common.project.facet.core.xml查看当前项目使用的jdk和servlet版本号:

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <fixed facet="wst.jsdt.web"/>
  <installed facet="wst.jsdt.web" version="1.0"/>
  <installed facet="java" version="1.7"/>
  <installed facet="jst.web" version="2.5"/>
</faceted-project>

查看/projectname/.settings/org.eclipse.jdt.core.prefs中jdk版本号:

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.source=1.7

Maven项目在Project Facets里面修改Dynamic web module为2.5的时候出现Cannot change version of project facet Dynamic web module to 2.5,解决办法即为修改上面的文件。

  1. web.xml中,加载不到ApplicationContext.xml和ApplicationContext-mvc.xml了。(
    报错:class path resource [spring/ApplicationContext.xml.xml] cannot be opened because it does not exist
    原因:IDE认为spring文件夹是在source下的,所以找不到
    解决:在classpath后面加个*
<init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring/ApplicationContext-mvc.xml</param-value>
    </init-param>
  1. Maven Web Projest经Update Project报错:Cannot nest ‘myApp/src/main/resource’ inside ‘myApp/src’. To enable the nesting exclude ‘main/’ from ‘myApp/src’:
    解决办法,根据报错的具体原因使用(1)或(2),或根据实际情况进行修改:
    (1)修改pom.xml,去掉该行:
<sourceDirectory>src/</sourceDirectory>

(2)修改pom.xml:

		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<outputDirectory>${project.build.directory}/classes</outputDirectory>
  1. resources目录下的配置文件不编译
    https://blog.csdn.net/qq_36350477/article/details/79722517
  2. maven install报错:编码 UTF-8 的不可映射字符
    (1)修改pom.xml,增加UTF-8编码格式:
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
					<compilerArgs>
						<arg>-XDignore.symbol.file</arg>
					</compilerArgs>
					<fork>true</fork>
				</configuration>
			</plugin>

(2)maven install报错:非法字符: \65279
重新保存文件格式,修改编码格式为utf-8,且不加bom标识。

(3)maven install报错:程序包com.sun.image.codec.jpeg不存在
原文链接:https://blog.csdn.net/eleanoryss/article/details/80015293
由于jdk1.6以后jre中删除rt.jar,jce.jar,所以会出现以上问题
解决办法:在pom文件中build -> plugins 中加入以下配置就可以了

				<compilerArgs>
						<arg>-XDignore.symbol.file</arg>
					</compilerArgs>
					<fork>true</fork>
  1. jar包依赖不存在,使用本地的jar包:
    方法:
    (1)将jar包放在src/main/webapp/WEB-INF/lib目录下。
    (2)pom.xml中:
		<dependency>
			<groupId>fcexporter</groupId>
			<artifactId>fcexporter</artifactId>
			<version>1.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/fcexporter.jar</systemPath>
		</dependency>
  1. 附上pom.xml完整代码:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.companyname</groupId>
	<artifactId>projectname</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>projectname</name>

	<dependencies>
		……
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
					<compilerArgs>
						<arg>-XDignore.symbol.file</arg>
					</compilerArgs>
					<fork>true</fork>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
					<resourceEncoding>UTF-8</resourceEncoding>
				</configuration>
			</plugin>
		</plugins>

		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<outputDirectory>${project.build.directory}/classes</outputDirectory>
	</build>

</project>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值