Maven项目错误解决

1.Java-maven异常-cannot be cast to javax.servlet.Filter 报错

tomcat 启动后先将tomcat/lib目录下的jar包全部读入内存,如果webapps目录里的应用程序中WEB-INF/lib目录下有相同的包,将无法加载,报错的Filter实现了javax.servlet.Filter接口,Filter是在servlet-api.jar里。

解决这个问题的方法就是对于servlet-ap.jar 使用 <scope>标签,编译的时候用到servlet-api和jsp-api,但在打包的时候不用这两个依赖。

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

<dependency>中<scope>,它主要管理依赖的部署。目前<scope>可以使用5个值:

  • compile,缺省值,适用于所有阶段,会随着项目一起发布。
  • provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
  • runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
  • test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
    *system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
  • 2.编译失败 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project web_nanchang: There are test failures.

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project web_nanchang: There are test failures.

    [ERROR] Please refer to E:\maven\web_nanchang\target\surefire-reports for the individual test results.

    解决方法:

    这是因为测试代码时遇到错误,它会停止编译。只需要在pom.xml的<project>里添加以下配置,使得测试出错不影响项目的编译。

    <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
            <testFailureIgnore>true</testFailureIgnore> </configuration>
        </plugin>
     </plugins>
    </build>
    

    3.Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin...There are test failures.

    Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project myproject: There are test failures.
    Please refer to E:\myproject\target\surefire-reports for the individual test results.

    解决方法:
    测试代码时遇到错误停止编译。可以在pom.xml的忽略错误,使得测试出错不影响项目的编译。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                <testFailureIgnore>true</testFailureIgnore>            </configuration>
            </plugin>
        </plugins>
    </build>
    

    4.编译时也有可能出现Java文件jdk1.8,但maven编译级别1.6,从而无法通过编译

    解决办法:

    <!-- 设定maven编译Java文件的jdk级别,一定要和项目,java install中的一致 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>