项目构建工具Maven:(5)Maven创建Web工程

作者:陆金龙    发表时间:2017-01-14 22:01   


目录

1.创建Web项目

2. 配置pom.xml实现继承和聚合

3. Web项目的生成和部署

4. Tomcat环境配置

1.创建Web项目

1.1创建项目

步骤与Java工程一样,不同的是选择maven-archetype-webapp模板

1.2 问题处理

(1)An internal error occurred during: "Retrieving archetypes:"

(2)org.jboss.tools.vpe.xulrunner.XulRunnerBundleNotFoundException: Bundle org.mozilla.xulrunner.win32.win32.x86_64 is not found.

Eclipse > Help > Install New Software

Add URL below to Work with

http://download.jboss.org/jbosstools/updates/integration/luna/core/xulrunner/xulrunner-1.9.2_win64-2014-08-22_09-55-58-B4/

2.配置pom.xml实现继承和聚合

Web项目目录结构如下:

同样修改pom.xml继承Parent工程,并在Parent工程中配置pom.xml实现聚合。

3.Web项目的生成和部署

在Parent工程pom.xml上运行maven install命令,生成web项目的war包。

将war包拷贝到tomcat的webapps目录下,重命名为FirstWeb.war

在tomcat目录的bin下,找到startup.bat执行,会将FirstWeb.war解压。

输入localhost:8080/FirstWeb/或者http://localhost:8080/FirstWeb/index.jsp,浏览站点,结果如下(注意网址是大小写敏感的):

更高级的部署方式如下;

3.1 部署的配置

(1)在C:\Users\kl\.m2的settings.xml的 节点下添加:

 org.codehaus.cargo

(2)在FirstWeb项目下pom.xml的project节点下添加如下配置:

<build>

    <finalName>FirstWeb</finalName>

    <plugins>

        <plugin>

        <groupId>org.codehaus.cargo</groupId>

        <artifactId>cargo-maven2-plugin</artifactId>

        <version>1.2.3</version>

        <configuration>

            <container>

                <containerId>tomcat9</containerId>

                <home>C:\Apache\apache-tomcat-9.0.0.M11</home>

             </container>

            <configuration>

                <type>existing</type>

                <home>C:\Apache\apache-tomcat-9.0.0.M11</home>

             </configuration>

          </configuration>

          <executions>

            <execution>

                 <id>cargo-run</id>

                 <phase>install</phase>

                 <goals>

                      <goal>run</goal>

                 </goals>

                 </execution>

              </executions>

         </plugin>

    </plugins>

</build>

(3)在pom.xml上右键-run as-maven build...,deploy生成web项目的war包。

3.2 错误处理

Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.2.3:run (cargo-run) on project FirstWeb: Execution cargo-run of goal org.codehaus.cargo:cargo-maven2-plugin:1.2.3:run failed: Cannot create configuration. There's no registered configuration for the parameters (container [id = [tomcat9], type = [installed]], configuration type [existing]). Actually there are no valid types registered for this configuration. Maybe you've made a mistake spelling it? -> [Help 1]

Failed to execute goal

org.codehaus.cargo:cargo-maven2-plugin:1.4.9:run (cargo-run) on project FirstWeb: Execution cargo-run of goal org.codehaus.cargo:cargo-maven2-plugin:1.4.9:run failed: Cannot create configuration. There's no registered configuration for the parameters (container [id = [tomcat9], type = [installed]], configuration type [existing]). Actually there are no valid types registered for this configuration. Maybe you've made a mistake spelling it? -> [Help 1]

该错误目前没有解决。

4. Tomcat环境配置

4.1 下载安装包

进入官网http://tomcat.apache.org/,下载Tomcat,64位windows系统版本选择64-bit Windows zip ,如下所示:

4.2 安装步骤

解压安装文件

下载完之后直接解压,解压到C:\Apache\apache-tomcat-9.0.0.M11

配置环境变量

需确保Java的环境变量已配置好,然后按如下步骤配置tomcat环境。

右击我的电脑—属性—高级系统设置—环境变量,在系统变量中添加以下变量

(1)TOMCAT_HOME,该变量指向解压文件的路径,该目录下有lib、bin等文件夹。添加方法如下:

     点击环境变量下的“新建”,在“变量名”中填写“TOMCAT_HOME”,在“变量值”中填写解压文件的路径,

C:\Apache\apache-tomcat-9.0.0.M11\apache-tomcat-9.0.0.M1

CATALINA_HOME,该变量的值与TOMCAT_HOME相同

(3)在“系统变量”中找到Path变量,双击打开Path变量,在“变量值”的最后面添加 %CATALINA_HOME%\bin

(4)在“系统变量”中找到CLASSPath变量,双击打开CLASSPath变量,在“变量值”的最后面添加%CATALINA_HOME%\lib\servlet-api.jar

​3.安装服务

单击“开始”—“运行”,键入"cmd",在控制台输入service install Tomcat9,在系统中安装Tomcat9服务项。

然后进入控制面板—系统和安全—管理工具—服务,找到Apache Tomcat Tomcat9服务项,右击该项,点“启动”,启动该服务,如图

4.打开浏览器,地址栏输入http://localhost:8080或 http://127.0.0.1:8080

 如果出现tomcat示例主页,则表示服务器安装成功。

4.3 启动服务失败处理

FindClass org/apache/catalina/startup/Bootstrap failed

java.lang.UnsupportedClassVersionError: org/apache/catalina/startup/Bootstrap : Unsupported major.minor version 52.0

J2SE 8 =52

1 J2SE 7 = 51 (0x33 hex),

2 J2SE 6.0 = 50 (0x32 hex),

3 J2SE 5.0 = 49 (0x31 hex),

4 JDK 1.4 = 48 (0x30 hex),

5 JDK 1.3 = 47 (0x2F hex),

6 JDK 1.2 = 46 (0x2E hex),

7 JDK 1.1 = 45 (0x2D hex).

需要52的版本,目前为51版本1.7.0_55,尝试将环境变量jdk改为jdk1.8.0_92。

1.cmd中执行service uninstall Tomcat9,卸载服务。

2.将环境变量JAVA_HOME值改为jdk1.8.0_92目录。

3.重启电脑,让修改生效。然后重新执行service install Tomcat9。

4.在服务中启动Apache Tomcat9.0 tomcat9 ,成功。

访问localhost:8080,结果如下: