Eclipse/Spring Tool Suite配置Maven

默认情况下使用Eclipse或者Spring Tool Suite创建maven-archetype-webapp项目后,会发现如下几个问题:

  • jdk版本为1.5。
  • 默认仓库镜像地址下载慢。
  • web.xml版本为2.3。
  • pom.xml里面的junit为3.1.8版本。
  • 无Web运行环境。

下面针对这几个问题逐一说明解决办法。

 

一、解决jdk版本问题

此问题比较容易解决,将 maven 安装目录下的 conf 文件夹下settings.xml文件复制到 C:\Users\{当前用户名}\.m2\ 目录下。然后找到profiles配置项,在里面添加以下profile配置即可(以下是将jdk设置成1.8版本)。

<profile>
    <id>jdk-1.8</id>
    <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
    </activation>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
</profile>

 

二、解决默认仓库镜像地址下载慢的问题

接着第一步,在C:\Users\{当前用户名}\.m2\ 目录下的settings里面的mirrors配置项下,添加国内仓库镜像地址的配置即可,比如添加阿里的仓库镜像地址:

<mirror>
  <id>aliyunmaven</id>
  <mirrorOf>*</mirrorOf>
  <name>阿里云公共仓库</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>

 

三、解决web.xml版本问题(此步骤与步骤三同时修改

1、进入 C:\Users\{当前用户名}\.m2\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0\ 路径,复制 maven-archetype-webapp-1.0.jar 文件到别处,然后将其解压出来。

2、找到解压出来的文件里面的 archetype-resources\src\main\webapp\WEB-INF\ 路径下的web.xml文件,使用文本编辑器打开,对其进行编辑然后保存。下面是一个编辑好的web.xml内容(版本修改到4.0的版本)。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    id="WebApp_ID" version="4.0">
    <display-name>Archetype Created Web Application</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

四、解决pom.xml中的junit版本问题

与修改web.xml类似,用文本编辑器打开解压出来的文件里面的 archetype-resources\pom.xml 文件进行编辑,然后保存。下面是编辑后的内容(将junit的版本修改为当前最新版本4.12)。

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <packaging>war</packaging>
    <version>${version}</version>
    <name>${artifactId} Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${artifactId}</finalName>
    </build>
</project>

 修改完成后,重新将文件打包成 maven-archetype-webapp-1.0.jar 文件,替换掉 C:\Users\{当前用户名}\.m2\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0\ 目录下原来的 maven-archetype-webapp-1.0.jar 即可。

 

五、解决无web运行环境的问题。

1、右键点击创建的项目,选择 Build Path-->Configure build path… 打开配置窗口,在窗口邮编选择“Libraries”选项卡,然后点击 “Add Libarary…” 按钮。

2、选择“Server Runtime”,然后点击 “Next”按钮。

3、选择已配置好的服务器软件/容器,然后依次保存即可。

 

posted on 2018-12-26 12:37  QingXiaxu  阅读(823)  评论(0编辑  收藏  举报

导航