微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>
 

 

  

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐