maven web项目的创建

2021/12/23 23:07:18

本文主要是介绍maven web项目的创建,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

方式一

  1.先创建一个maven jar项目 再添加web

  1.1添加web 选择project structure->moudules,选择点击项目 ->点击加号-> 选择web

 

 

   1.2 配置deployment descriptors 和 web resources directories   选择 点击修改 如下图

     

 

 

   1.3修改成功

  

 

 

 方式二 

  2.直接创建maven web项目 缺点:创建完成后会缺少包

  

 

 

 

  2.1创建开始,勾选下图选项 填写选项 并配置maven引用

 

 

    

 

 

 

 

 

  2.2如图 缺少 main下的java包和resources包  src下的test包以及test包下的java包

  

 

 

 

 

 

   2.3 添加缺少的包

  右键点击main创建java包和resources包 右键点击java包选择 mark directory as ->sources root  右键点击java包选择 mark directory as ->resources root 

  

 

 

 

 

  右键点击src 创建 test包 再右键点击test创建 java包 

  再右键点击test包下的java包 选择 marking directory as -> test sources root

 

   

 

  2.4 创建成功如下

  

 

 

   2.5 配置web.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  </web-app>

  2.6 配置pom.xml
<?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.javasm</groupId>
    <artifactId>maven-web-demo6</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--默认jar项目 可以不写-->
    <!--<packaging>jar</packaging>-->
    <!--聚合项目-->
    <!--<packaging>pom</packaging>-->
    <!--web项目-->
    <packaging>war</packaging>

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

    <!--环境变量的配置-->
    <!--方式一 解决编译 1.7 设置成 1.8-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <!--&lt;!&ndash; 方式二:解决编译 1.5 设置成 1.8 &ndash;&gt;-->
    <!--<build>-->
        <!--<plugins>-->
            <!--<plugin>-->
                <!--<groupId>org.apache.maven.plugins</groupId>-->
                <!--<artifactId>maven-compiler-plugin</artifactId>-->
                <!--<configuration>-->
                    <!--<source>1.8</source>-->
                    <!--<target>1.8</target>-->
                    <!--<encoding>UTF-8</encoding>-->
                <!--</configuration>-->
            <!--</plugin>-->
        <!--</plugins>-->
    <!--</build>-->
    <!--&lt;!&ndash;  配置tomcat7插件&ndash;&gt;-->
    <!--<build>-->
        <!--<plugins>-->
            <!--&lt;!&ndash;在pom文件中 添加comcat7插件 &ndash;&gt;-->
            <!--<plugin>-->
                <!--<groupId>org.apache.tomcat.maven</groupId>-->
                <!--<artifactId>tomcat7-maven-plugin</artifactId>-->
                <!--<version>2.2</version>-->
                <!--<configuration>-->
                    <!--&lt;!&ndash; 项目访问路径 本例:localhost:9090, 如果配置的aa,则访问路径为localhost:9090/aa &ndash;&gt;-->
                    <!--<path>/</path>-->
                    <!--<port>9090</port>-->
                    <!--<uriEncoding>UTF-8</uriEncoding>&lt;!&ndash; 非必需项 &ndash;&gt;-->
                <!--</configuration>-->
            <!--</plugin>-->
        <!--</plugins>-->
    <!--</build>-->
    <!--&lt;!&ndash;jetty 插件&ndash;&gt;-->
    <!--<build>-->
        <!--<plugins>-->
            <!--<plugin>-->
                <!--<groupId>org.eclipse.jetty</groupId>-->
                <!--<artifactId>jetty-maven-plugin</artifactId>-->
                <!--&lt;!&ndash;<version>9.3.0.M2</version>&ndash;&gt;-->
                <!--<configuration>-->
                    <!--<webAppConfig>-->
                        <!--&lt;!&ndash;配置根路径&ndash;&gt;-->
                        <!--<contextPath>/</contextPath>-->
                    <!--</webAppConfig>-->
                    <!--<httpConnector>-->
                        <!--&lt;!&ndash;配置端口&ndash;&gt;-->
                        <!--<port>10000</port>-->
                    <!--</httpConnector>-->
                <!--</configuration>-->
            <!--</plugin>-->
        <!--</plugins>-->
    <!--</build>-->
    <!--配置 Junit版本 配置spring 依赖-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

    <!--不使用Tomcat的情况下 使用 jetty 插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.0.M2</version>
                <configuration>
                    <webAppConfig>
                        <!--配置根路径-->
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <httpConnector>
                        <!--配置端口-->
                        <port>10000</port>
                    </httpConnector>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
 

 

  

 



这篇关于maven web项目的创建的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程