IDEA + Maven 搭建JavaWeb项目

前言

本章节主要内容是描述如何使用maven构建javaweb项目

  • Maven依赖仓库:

    ​ https://mvnrepository.com/

  • Tomcat7插件的命令:

    ​ https://tomcat.apache.org/maven-plugin-trunk/tomcat7-maven-plugin/plugin-info.html

1. 项目搭建

选择maven模板的 maven-app 模板创建项目 , 如下图所示

在这里插入图片描述
设置maven相关配置

在这里插入图片描述
确认自己的maven配置目录, 如果没有设置, 默认会使用.m2的maven配置

在这里插入图片描述

2. 配置项目

修改 JDK 的版本
<!-- JDN的版本修改为1.8 -->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
设置单元测试的版本
<!-- junit的版本修改为4.12 -->
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
删除pluginManagement标签
<!-- 将这个标签及标签中的内容全部删除 -->
<pluginManagement>
...
</pluginManagement>
添加web部署的插件

​ 在 build 标签中添加 plugins 标签

  1. Jetty插件

    <!-- 设置在plugins标签中 -->
    <plugin>
       <groupId>org.mortbay.jetty</groupId>
       <artifactId>maven-jetty-plugin</artifactId>
       <version>6.1.25</version>
       <configuration>
          <!-- 热部署,每10秒扫描一次 -->
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <!-- 可指定当前项目的站点名 -->
          <contextPath>/test</contextPath>                 
          <connectors>
              <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                  <port>9090</port> <!-- 设置启动的端口号 -->
              </connector>
          </connectors>
       </configuration>
    </plugin>  
    
  2. Tomcat插件

    <!-- 设置在plugins标签中 -->
    <plugin>
    	<groupId>org.apache.tomcat.maven</groupId>
    	<artifactId>tomcat7-maven-plugin</artifactId>
    	<version>2.1</version>
    	<configuration>
    		<port>8081</port> <!-- 启动端口 默认:8080 -->
    		<path>/test</path> <!-- 项目的站点名,即对外访问路径 -->
    		<uriEncoding>UTF-8</uriEncoding> <!-- 字符集编码 默认:ISO-8859-1 -->
    		<server>tomcat7</server> <!-- 服务器名称 -->
    	</configuration>
    </plugin>
    
    
    
完整POM文件参考
<?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>cn.knightzz</groupId>
  <artifactId>lesson-04-webapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>lesson-04-webapp Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>lesson-04-webapp</finalName>

    <plugins>
      <!-- 设置在plugins标签中 -->
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.25</version>
        <configuration>
          <!-- 热部署,每10秒扫描一次 -->
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <!-- 可指定当前项目的站点名 -->
          <contextPath>/test</contextPath>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>9090</port> <!-- 设置启动的端口号 -->
            </connector>
          </connectors>
        </configuration>
      </plugin>

      <!-- 设置在plugins标签中 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>8081</port> <!-- 启动端口 默认:8080 -->
          <path>/lesson-04-webapp</path> <!-- 项目的站点名,即对外访问路径 -->
          <uriEncoding>UTF-8</uriEncoding> <!-- 字符集编码 默认:ISO-8859-1 -->
          <server>tomcat7</server> <!-- 服务器名称 -->
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

4. 项目运行

使用Jetty运行项目
  1. 点击右上角的 "Add Configurations ",打开 “Run/Debug Configurations” 窗口, 添加相关配置

在这里插入图片描述
2. 也可以指定端口运行

jetty:run -Djetty.port=9090  # 需要将插件配置中的port标签去掉
  1. 使用jetty运行项目

在这里插入图片描述
4. 在浏览器中显示

在这里插入图片描述

  1. 启动成功
[INFO] Starting jetty 6.1.25 ...
[INFO] jetty-6.1.25
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] Started SelectChannelConnector@0.0.0.0:9090
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 10 seconds.
使用Tomact插件运行项目
  1. 配置tomact插件

在这里插入图片描述
2. 运行项目


[INFO] --- tomcat7-maven-plugin:2.1:run (default-cli) @ lesson-04-webapp ---
[INFO] Running war on http://localhost:8081/lesson-04-webapp
[INFO] Creating Tomcat server configuration at K:\CodeWorkSpace\CodeApp\spring-lesson-cloud\spring-aop\lesson-04-webapp\target\tomcat
[INFO] create webapp with contextPath: /lesson-04-webapp
十月 31, 2021 3:10:03 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8081"]
十月 31, 2021 3:10:03 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
十月 31, 2021 3:10:03 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.37
十月 31, 2021 3:10:04 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8081"]

  1. 浏览器访问

在这里插入图片描述

5. 注意事项

  • jetty 和 tomact 的插件配置都在pom文件里配置

 <plugins>
      <!-- 设置在plugins标签中 -->
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.25</version>
        <configuration>
          <!-- 热部署,每10秒扫描一次 -->
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <!-- 可指定当前项目的站点名 -->
          <contextPath>/test</contextPath>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>9090</port> <!-- 设置启动的端口号 -->
            </connector>
          </connectors>
        </configuration>
      </plugin>

      <!-- 设置在plugins标签中 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>8081</port> <!-- 启动端口 默认:8080 -->
          <path>/lesson-04-webapp</path> <!-- 项目的站点名,即对外访问路径 -->
          <uriEncoding>UTF-8</uriEncoding> <!-- 字符集编码 默认:ISO-8859-1 -->
          <server>tomcat7</server> <!-- 服务器名称 -->
        </configuration>
      </plugin>
    </plugins>
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兀坐晴窗独饮茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值