idea 使用tomcat插件创建Java web项目

1、创建一个普通的maven项目

2、右击项目->Add Frameworks Supports

选中web Application  ,勾选 Create web.xml

生成的项目结构如下图:

3、配置tomcat插件与引入servlet与jsp依赖

<?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>com.plugin.web</groupId>
    <artifactId>spring_web</artifactId>
    <version>1.0-SNAPSHOT</version>
  <!--注意指定打包方式-->
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 配置Tomcat插件:
                    就是本地部署,将tomcat 内嵌到 web项目中,这样可以直接运行 webapp项目。
                    跟类似spring boot 项目一样,不需要再部署到额外的tomcat,直接就可以运行了。-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- 不需要上下文  http://localhost:8080/xxx.jsp -->
                    <path>/</path>
                    <port>8080</port>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

4、更改生成的目录结构

此时项目是如果打包,会报如下的错:

Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project spring_web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

需要把web改成webapp,并把webapp目录移动到main目录下:

此时就可以intall了。

D:programsjdk8injava.exe -Dmaven.multiModuleProjectDirectory=D:workspace_ideaspring_web -Dmaven.home=D:programsmaven -Dclassworlds.conf=D:programsmaveninm2.conf "-Dmaven.ext.class.path=D:programsIntelliJ IDEA 2020.3.3pluginsmavenlibmaven-event-listener.jar" "-javaagent:D:programsIntelliJ IDEA 2020.3.3libidea_rt.jar=62114:D:programsIntelliJ IDEA 2020.3.3in" -Dfile.encoding=UTF-8 -classpath D:programsmavenootplexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2020.3.3 -s C:Userscaopeng.m2settings.xml install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.plugin.web:spring_web:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.tomcat.maven:tomcat7-maven-plugin is missing. @ line 49, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ---------------------< com.plugin.web:spring_web >----------------------
[INFO] Building spring_web 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring_web ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ spring_web ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring_web ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:workspace_ideaspring_websrc	est
esources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ spring_web ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ spring_web ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ spring_web ---
[INFO] Packaging webapp
[INFO] Assembling webapp [spring_web] in [D:workspace_ideaspring_web	argetspring_web-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [D:workspace_ideaspring_websrcmainwebapp]
[INFO] Webapp assembled in [21 msecs]
[INFO] Building war: D:workspace_ideaspring_web	argetspring_web-1.0-SNAPSHOT.war
[INFO] WEB-INFweb.xml already added, skipping
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ spring_web ---
[INFO] Installing D:workspace_ideaspring_web	argetspring_web-1.0-SNAPSHOT.war to D:programsmaven
epositorycompluginwebspring_web1.0-SNAPSHOTspring_web-1.0-SNAPSHOT.war
[INFO] Installing D:workspace_ideaspring_webpom.xml to D:programsmaven
epositorycompluginwebspring_web1.0-SNAPSHOTspring_web-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.469 s
[INFO] Finished at: 2021-09-21T19:45:27+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

5、启动项目

直接点击tomcat7:run

或者添加add Configuration

在第二步点击maven

在 Command line输入maven 命令,点击确定,就可以直接通过绿色的三角型启动了

原文地址:https://www.cnblogs.com/cplinux/p/15317666.html