当前位置:首页 > 通信资讯 > 正文

idea使用maven创建web项目(idea创建mavenweb项目)

目录
  • 前言
  • 1. 项目搭建
  • 2. 配置项目
  • 添加web部署的插件
  • 3. 项目运行
  • 使用Tomact插件运行项目
  • 4. 注意事项

前言

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

Maven依赖仓库:

https://mvnrepository.com/

Tomcat7插件的命令:

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

1. 项目搭建

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

idea使用maven创建web项目(idea创建mavenweb项目)

设置maven相关配置

idea使用maven创建web项目(idea创建mavenweb项目)

确认自己的maven配置目录, 如果没有设置, 默认会使用.m2的maven配置

idea使用maven创建web项目(idea创建mavenweb项目)

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 标签

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>

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>

3. 项目运行

使用Jetty运行项目

点击右上角的 "Add Configurations ",打开 “Run/Debug Configurations” 窗口, 添加相关配置

idea使用maven创建web项目(idea创建mavenweb项目)

也可以指定端口运行

jetty:run -Djetty.port=9090 # 需要将插件配置中的port标签去掉

使用jetty运行项目

idea使用maven创建web项目(idea创建mavenweb项目)

在浏览器中显示

idea使用maven创建web项目(idea创建mavenweb项目)

启动成功

[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插件运行项目

配置tomact插件

idea使用maven创建web项目(idea创建mavenweb项目)

运行项目

[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:CodeWorkSpaceCodeAppspring-lesson-cloudspring-aoplesson-04-webapp arget omcat
[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"]

浏览器访问

idea使用maven创建web项目(idea创建mavenweb项目)

4. 注意事项

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>

到此这篇关于IDEA+Maven搭建JavaWeb项目的文章就介绍到这了,更多相关IDEA + Maven 搭建JavaWeb项目内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_40040107/article/details/121054206

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。