步骤
- 项目右键–>configure–>Convert to Maven Project
- 将项目中原有的src下的东西复制出来,然后删除src包
- 项目右键–>New–>Source Folder
分别新建 src/main/java、src/main/resources和src/main/webapp(webapp目录有人说是自动生成的,我的没有自动生成) - 将项目原src下的java放在src/main/java中
将项目原src下的配置文件放在src/main/resources中
将项目原WebRoot下的文件复制到src/main/webapp中 - 将项目的lib文件夹复制出来一份,然后删掉
- 根据复制出来的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>
- 部署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”,解决办法就是修改上面的文件。
- 修改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>
(原文链接:)
错误说明: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,解决办法即为修改上面的文件。
- 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>
- 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>
- resources目录下的配置文件不编译
- 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不存在
原文链接:
由于jdk1.6以后jre中删除rt.jar,jce.jar,所以会出现以上问题
解决办法:在pom文件中build -> plugins 中加入以下配置就可以了
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
<fork>true</fork>
- 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>
- 附上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>