1 package com.haifei.demo; 2 3 public class HelloWorld { 4 public static void main(String[] args) { 5 System.out.println("hello maven"); 6 } 7 }
1 package com.haifei.test; 2 3 import org.junit.Test; 4 5 public class TestHello { 6 7 @Test 8 public void testHelloWorld(){ 9 System.out.println("=====>>>hello maven test"); 10 } 11 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <!--创建模块时自动生成当前项目的坐标--> 8 <groupId>com.haifei</groupId> 9 <artifactId>maven_java_1</artifactId> 10 <version>1.0-SNAPSHOT</version> 14 <!-- 15 packaging打包方式 16 取值: 17 1. jar:java项目。默认值 18 2. war:web项目。 19 3. pom 20 --> 21 <packaging>pom</packaging> <!--此标签不写默认即为jar--> 22 23 24 <!--导入依赖--> 25 <!--需要单元测试的jar包 26 手动导入 27 百度搜junit maven 28 打开站点https://mvnrepository.com/artifact/junit/junit 29 选择版本,比如4.11 30 复制网站提供的maven-dependency配置代码 31 粘贴到dependencies下 32 --> 33 <dependencies> 34 <!-- https://mvnrepository.com/artifact/junit/junit --> 35 <dependency> 36 <groupId>junit</groupId> 37 <artifactId>junit</artifactId> 38 <version>4.11</version> 39 <scope>test</scope> 40 </dependency> 41 <!--tips:快捷键alt+insert 42 选择Dependency 43 在弹出的搜索框中输入目标jar包名称进行模糊查询(前提是本地仓库中得有这个jar包) 44 选好后回车,即可直接在此生成配置信息--> 45 <dependency> 46 <groupId>mysql</groupId> 47 <artifactId>mysql-connector-java</artifactId> 48 <version>5.1.26</version> 49 </dependency> 50 </dependencies> 51 52 53 <!--添加插件--> 54 <!--控制台Messages中Warning提示:源值1.5过时 55 即当前的编译版本是1.5 56 原因是maven项目创建时默认就是1.5,可以在project structure中看到language level 57 但是在maven项目中,一般不会手动修改project structure中的language level 58 而是通过在pom.xml中配置插件(类似于坐标) 59 --> 60 <build> 61 <!--tips:快速生成 alt+insert 62 选择Plugin Template 63 选择maven-compiler-plugin--> 64 <plugins> 65 <!--编译插件--> 66 <plugin> 67 <groupId>org.apache.maven.plugins</groupId> 68 <artifactId>maven-compiler-plugin</artifactId> 69 <version>3.1</version> 70 <configuration> 71 <target>1.8</target> <!--通过configuration手动配置编译的版本为1.8,可在project structure中的language level得到验证--> 72 <source>1.8</source> 73 </configuration> 74 </plugin> 75 </plugins> 76 </build> 77 78 </project>
1 package com.haifei.web.servlet; 2 3 import redis.clients.jedis.Jedis; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import java.io.IOException; 11 12 @WebServlet("/demo") 13 public class DemoServlet extends HttpServlet { 14 @Override 15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 System.out.println("hello maven servlet"); 17 /* 18 http://localhost/demo 19 hello maven servlet 20 */ 21 22 Jedis jedis = new Jedis(); 23 } 24 }
1 package com.haifei.test; 2 3 import redis.clients.jedis.Jedis; 4 5 public class DemoTest { 6 7 public void demoTest(){ 8 Jedis jedis = new Jedis(); 9 } 10 }
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index.html</title> 6 </head> 7 <body> 8 <h1>hello maven web</h1> 9 </body> 10 </html>
1 web.xml 2 3 <?xml version="1.0" encoding="UTF-8"?> 4 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 5 <web-app> 6 7 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.haifei</groupId> 8 <artifactId>maven_web_1</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> <!--不使用骨架创建maven的web项目时,pom.xml里不会自动生成packaging,必须手动设置打包方式为war--> 11 12 13 <dependencies> 14 <!--添加servlet的依赖jar包--> 15 <dependency> 16 <groupId>javax.servlet</groupId> 17 <artifactId>javax.servlet-api</artifactId> 18 <version>3.1.0</version> 19 <scope>provided</scope> <!--依赖范围,不配置的话访问servlet会报错500--> 20 <!--原因是依赖冲突 21 此处将servlet-api设置为provided,即仅在编译和测试阶段可用就行 22 如果不这样设置,而是采用默认的compile的话,那么在运行时阶段(war包阶段),war包里会被打进去一个servlet-api.jar 23 又因为tomcat7插件里自带servlet-api.jar 24 那么,war包在tomcat中被部署时,就会有两个servlet-api.jar,起冲突了 25 26 一般:tomcat服务器软件自带的jar包 27 在项目pom.xml的依赖中要设置为provided,防止起冲突 28 --> 29 </dependency> 30 31 <dependency> 32 <groupId>redis.clients</groupId> 33 <artifactId>jedis</artifactId> 34 <version>2.7.0</version> 35 <scope>compile</scope> <!--依赖范围的默认值为compile编译时,即不设置scope就是这个--> 36 </dependency> 37 38 <dependency> 39 <groupId>junit</groupId> 40 <artifactId>junit</artifactId> 41 <version>4.13</version> 42 <scope>test</scope> 43 </dependency> 44 45 <dependency> 46 <groupId>mysql</groupId> 47 <artifactId>mysql-connector-java</artifactId> 48 <version>5.1.26</version> 49 <scope>runtime</scope> 50 </dependency> 51 52 </dependencies> 53 54 55 <build> 56 <!--alt+insert : plugin template--> 57 <plugins> 58 <!--搜tomcat7 添加tomcat插件用于启动当前web应用--> 59 <plugin> 60 <groupId>org.apache.tomcat.maven</groupId> 61 <artifactId>tomcat7-maven-plugin</artifactId> 62 <version>2.1</version> 63 <!--可以详细配置tomcat服务器部署当前项目--> 64 <configuration> 65 <port>80</port> <!--端口号--> 66 <path>/</path> <!--虚拟目录--> 67 <!--这样设置后,项目启动直接访问localhost即可--> 68 </configuration> 69 </plugin> 70 <!--通过配置编译插件指定当前项目的编译JDK版本--> 71 <plugin> 72 <groupId>org.apache.maven.plugins</groupId> 73 <artifactId>maven-compiler-plugin</artifactId> 74 <version>3.1</version> 75 <configuration> 76 <target>1.8</target> 77 <source>1.8</source> 78 </configuration> 79 </plugin> 80 </plugins> 81 </build> 82 83 </project>