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:\programs\jdk8\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\workspace_idea\spring_web -Dmaven.home=D:\programs\maven -Dclassworlds.conf=D:\programs\maven\bin\m2.conf "-Dmaven.ext.class.path=D:\programs\IntelliJ IDEA 2020.3.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\programs\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=62114:D:\programs\IntelliJ IDEA 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\programs\maven\boot\plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2020.3.3 -s C:\Users\caopeng\.m2\settings.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_idea\spring_web\src\test\resources
[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_idea\spring_web\target\spring_web-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\workspace_idea\spring_web\src\main\webapp]
[INFO] Webapp assembled in [21 msecs]
[INFO] Building war: D:\workspace_idea\spring_web\target\spring_web-1.0-SNAPSHOT.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ spring_web ---
[INFO] Installing D:\workspace_idea\spring_web\target\spring_web-1.0-SNAPSHOT.war to D:\programs\maven\repository\com\plugin\web\spring_web\1.0-SNAPSHOT\spring_web-1.0-SNAPSHOT.war
[INFO] Installing D:\workspace_idea\spring_web\pom.xml to D:\programs\maven\repository\com\plugin\web\spring_web\1.0-SNAPSHOT\spring_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 命令,点击确定,就可以直接通过绿色的三角型启动了

 

posted @ 2021-09-21 20:04  阿瞒123  阅读(474)  评论(0编辑  收藏  举报