• maven常见错误


    摘要: 1.Java-maven异常-cannot be cast to javax.servlet.Filter 报错  tomcat 启动后先将tomcat/lib目录下的jar包全部读入内存,如果webapps目录里的应用程序中WEB-INF/lib目录下有相同的包,将无法加载,报错的Filter...

    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.Maven缺少依赖包,强制更新

    mvn clean install -e -U

    -e详细异常,-U强制更新

    3.java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0

    解决办法比较多,在项目右键查看Configure中的Java BuildPath,设置为和默认JDK一致就可以。

    可以修改settings.xml,添加如下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <profile>
    <id>jdk-1.7</id>
    <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.7</jdk>
    </activation>
    <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
    </properties>
    </profile>

      

    4.编译失败 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:mavenweb_nanchang argetsurefire-reports for the individual test results.

    解决方法:

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
    <testFailureIgnore>true</testFailureIgnore> </configuration>
    </plugin>
    </plugins>
    </build>

    命令行:mvn test -Dmaven.test.failure.ignore=true

    5.在Eclipse中进行Maven项目断点调试等

    1、在安装了m2eclipse插件并配置好maven-tomcat-plugin后,单击Eclipse菜单“Run”中的“Run Configurations”。
    2、在弹出的对话框中的左侧树中找“到Maven Build”,在其上点击右键并单击“New”。
    3、在右侧的“Name”一栏填入自定义的名称。单击在“Main”选项卡的“Browse Workspace”按钮,选择目标项目,选择后在“Base directory”一栏中会出现形如“${workspace_loc:/project_a}”的内容(project_a是前文所述应用项目A,它会根据你所选的目标项目而改变)。
    4、在“Goals”一栏中填入“tomcat:run”。
    5、在“Maven Runtime”选择你需要的Maven环境(注意:必须是2.0.8以上的版本)。
    6、单击“Apply”,配置完成。
    通过以上配置,在eclipse自身的Run和Debug按钮下都会找到以你先前配置中“Name”值为名的条目(条目最前端也会有“m2”标识),单击之后即可启动基于Tomcat之上的相应项目的运行操作或调试操作。

    6.Maven编译 No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

    maven编译项目时出错,提示信息如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project springside-core: Compilation failure
     
    [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
     
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

    解决方案:

    在eclipse的菜单中,进入 Window > Preferences > Java > Installed JREs > Execution Environments,选择JavaSE-1.6, 在右侧选择jdk.

    然后在maven菜单中使用 “update project ...”.

     

    7.-Dmaven.multiModuleProjectDirectory system propery is not set

    eclipse中使用maven插件的时候,运行run as maven build的时候报错:

    -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.

    直接的解决方法:使用低版本的maven。

    或者可以执行下面的操作:

    设置环境变量M2_HOME指向你的maven安装目录

    M2_HOME=D:Appsapache-maven-3.3.1

    然后在Window->Preference->Java->Installed JREs->Edit

    在Default VM arguments中设置

    -Dmaven.multiModuleProjectDirectory=$M2_HOME


     

    8.pom文件提示"Missing artifact..."

    更新maven工程之后,发现pom.xml文件在如下依赖处报错:
    <dependency>
    <groupId>cms-dubbo</groupId>
    <artifactId>cms-dubbo</artifactId>
    <version>0.0.1</version>
    </dependency>
    报错信息为:Missing artifact cms-dubbo:cms-dubbo:jar:0.0.1

    解决方法:

    1.确保远程仓库是有对应jar包的,有可能有jar包,但是和你的版本号不对应,这种情况直接更新版本号就可以。
    2.确认本地repository相应目录中是否下载好了对应的包。
    同时检查发现目录下是否存在以如下结尾的文件:
    -not-available
    .lastUpdated
    该类型文件可以影响更新。将这两个文件删掉,重新build,如果问题解决,应该是之前未下载成功产生了这两个文件,影响了maven正常更新。

    3.如果上述办法无效,到eclipse-help-install new software-available software sites下,找之前安装m2eclipse插件的地址,如果是http://download.eclipse.org/technology/m2e/releases,将其卸载,按如下地址重新安装m2eclipse插件:http://m2eclipse.sonatype.org/sites/m2e

    4.如果上述方法仍无效,可尝试在eclipse中先用Close Project关掉出问题的工程,然后再Open Project打开;或用Project-Clean重新build该工程。

     

    9.Maven+eclipse工程中Maven Dependencies 文件夹丢失问题

    在项目构建路径中更改。

    10.Failure to transfer 依赖文件

    Failure to transfer org.codehaus.plexus:plexus-archiver:jar:1.2 from http://172.17.xx.xx:xxxx/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus-news has elapsed or updates are forced. Original error: Could not transfer artifact org.codehaus.plexus:plexus-archiver:jar:1.2 from/to nexus-news (http://172.17.xx.xx:xxxx/nexus/content/groups/public/): No response received after 60000

    错误原因已经说明:org.codehaus.plexus从nexus仓库传输失败,本地仓库已经存在对应的缓存文件,Maven不会重新下载文件,直到更新索引或者强制更新项目。

    解决办法也很简单,直接maven-->updates projects就可以。

     

    11.不能使用eclipse直接创建Maven Web工程

    使用Eclipse时,不能一次完成Maven Web项目的创建,需要分步骤才能创建一个完整的Maven Web项目。

    我的建议是通过复制粘贴等手工建立Maven的文件结构和pom文件,然后再在Eclipse中import已经存在的Maven项目。

    也可以通过下面的步骤,

    在Eclipse下新建项目时,选择新建Maven项目,默认点击下一步,在“select an archetype”这步时,

    选择org.apache.maven.archetypes maven-archetype-webapp

    maven archetype就是创建项目的脚手架,你可以通过命令行或者IDE集成简化项目创建的工作。

    下一步输入项目名称,起名为“testMavenWeb”项目,点击【finish】,完成项目创建。
    此时,一个Java Maven项目创建成功,但并不是Maven Web项目。

    Java Maven 创建成功后,再创建一个Web项目,在“select an archetype”这步时,选择“maven-archetype-webapp”,成功创建一个web项目,
    然后将该web项目下的webapp文件夹,拷贝到testMavenWeb项目下,至此,在Eclipse下的Maven Web项目创建成功。

     

    12.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 argetsurefire-reports for the individual test results.

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>            </configuration>
            </plugin>
        </plugins>
    </build>

      

    13.maven手动把安装jar到本地仓库

    每次添加单个jar到maven本地仓库的操作如下:

    1.建立一个新的文件夹,将jar文件存放在该文件夹下。 注意文件夹下最好只存放该文件。

    2.在该文件夹下建立一个pom.xml文件,在pom文件中定义其maven坐标。

    3.在cmd窗口中执行以下命令: mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> 
    例如:
    mvn install:install-file -Dfile=D:/jar/xxx.jar -DgroupId=xxx.xxx -DartifactId=xxx -Dversion=x.x -Dpackaging=jar

    14.xxx was cached in the local repository,resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced

    把maven仓库中xxx.lastUpdated文件全部删掉,重新运行maven,
    或者在执行maven操作时加 -U参数,就可以忽略xxx.lastUpdated。

    15.[ERROR] 找不到javax.servlet.ServletContext的类文件

    Maven项目下HttpServletRequest 或 HttpServletResponse需引用的依赖包:servlet-api.jar,并将scope设置为provided。

    1
    2
    3
    4
    5
    6
    7
    <!-- 使用HttpServletRequest或 HttpServletResponse需引用的依赖包 -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
    </dependency>
  • 相关阅读:
    Mysql 存储引擎中InnoDB与Myisam的主要区别
    SELECT FOR UPDATE(转)
    OAuth的机制原理讲解及开发流程 (转)
    缓存
    个人研究
    基础知识
    sql
    面试问题
    mysql的partition分区
    git安装配置和使用
  • 原文地址:https://www.cnblogs.com/Rambo635755402/p/5549356.html
Copyright © 2020-2023  润新知