Maven学习总结
Maven是什么?
in short :maven是用来管理项目的(POM)。主要功能是管理项目依赖(dependency),拆分项目(减少项目的耦合)。
Maven项目的目录结构
src
main
java
resources
webApp
test
java
resources
pom.xml
Maven命令
clean:把target目录删除,相当于把编译的.class文件全部删掉
compile:建立target目录,相当于编译,创建.class文件
install:把项目打成jar包或者war包,安装到本地repository
package:把项目打成jar包或者war包,到target目录
test:运行工程的测试
Maven管理依赖的方式
核心:pom.xml
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>zipeng</groupId> 4 <artifactId>parent</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>pom</packaging> 7 8 <!-- 属性: 定义版本的常量,方便修改 --> 9 <properties> 10 <!-- 名称是任意的,建议使用有意义的名字 --> 11 <spring.version>4.2.4.RELEASE</spring.version> 12 <hibernate.version>5.0.7.Final</hibernate.version> 13 <struts.version>2.3.24</struts.version> 14 </properties> 15 16 <!-- 锁定版本: struts2-2.3.24, spring4.2.4, hibernate5.0.7 --> 17 <dependencyManagement> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-context</artifactId> 22 <!-- OGNL 表达式 --> 23 <version>${spring.version}</version> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework</groupId> 27 <artifactId>spring-aspects</artifactId> 28 <version>${spring.version}</version> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-orm</artifactId> 33 <version>${spring.version}</version> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-test</artifactId> 38 <version>${spring.version}</version> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework</groupId> 42 <artifactId>spring-web</artifactId> 43 <version>${spring.version}</version> 44 </dependency> 45 <dependency> 46 <groupId>org.hibernate</groupId> 47 <artifactId>hibernate-core</artifactId> 48 <version>${hibernate.version}</version> 49 </dependency> 50 <dependency> 51 <groupId>org.apache.struts</groupId> 52 <artifactId>struts2-core</artifactId> 53 <version>${struts.version}</version> 54 </dependency> 55 <dependency> 56 <groupId>org.apache.struts</groupId> 57 <artifactId>struts2-spring-plugin</artifactId> 58 <version>${struts.version}</version> 59 </dependency> 60 </dependencies> 61 </dependencyManagement> 62 63 <!-- 依赖管理 --> 64 <dependencies> 65 <!-- spring --> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-aspects</artifactId> 69 </dependency> 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-orm</artifactId> 73 </dependency> 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-test</artifactId> 77 </dependency> 78 <dependency> 79 <groupId>org.springframework</groupId> 80 <artifactId>spring-web</artifactId> 81 </dependency> 82 <!-- hibernate --> 83 <dependency> 84 <groupId>org.hibernate</groupId> 85 <artifactId>hibernate-core</artifactId> 86 </dependency> 87 <dependency> 88 <groupId>com.mchange</groupId> 89 <artifactId>mchange-commons-java</artifactId> 90 <version>0.2.11</version> 91 </dependency> 92 <!-- 导入struts2 --> 93 <dependency> 94 <groupId>org.apache.struts</groupId> 95 <artifactId>struts2-core</artifactId> 96 </dependency> 97 <dependency> 98 <groupId>org.apache.struts</groupId> 99 <artifactId>struts2-spring-plugin</artifactId> 100 </dependency> 101 <!-- servlet jsp --> 102 <dependency> 103 <groupId>javax.servlet</groupId> 104 <artifactId>servlet-api</artifactId> 105 <version>2.5</version> 106 <scope>provided</scope> 107 </dependency> 108 <dependency> 109 <groupId>javax.servlet</groupId> 110 <artifactId>jsp-api</artifactId> 111 <version>2.0</version> 112 <scope>provided</scope> 113 </dependency> 114 <!-- 日志, 需要引入 log4j 的配置文件 --> 115 <dependency> 116 <groupId>org.slf4j</groupId> 117 <artifactId>slf4j-api</artifactId> 118 <version>1.7.2</version> 119 </dependency> 120 <dependency> 121 <groupId>org.slf4j</groupId> 122 <artifactId>slf4j-log4j12</artifactId> 123 <version>1.7.2</version> 124 </dependency> 125 <dependency> 126 <groupId>log4j</groupId> 127 <artifactId>log4j</artifactId> 128 <version>1.2.17</version> 129 </dependency> 130 <!-- junit --> 131 <dependency> 132 <groupId>junit</groupId> 133 <artifactId>junit</artifactId> 134 <version>4.12</version> 135 <scope>test</scope> 136 </dependency> 137 <!-- jstl --> 138 <dependency> 139 <groupId>javax.servlet</groupId> 140 <artifactId>jstl</artifactId> 141 <version>1.2</version> 142 </dependency> 143 <!-- JSON 处理库:javaBean转换成 --> 144 <dependency> 145 <groupId>com.alibaba</groupId> 146 <artifactId>fastjson</artifactId> 147 <version>1.1.15</version> 148 </dependency> 149 <dependency> 150 <groupId>org.apache.commons</groupId> 151 <artifactId>commons-lang3</artifactId> 152 <version>3.4</version> 153 </dependency> 154 <!-- 数据库驱动 --> 155 <dependency> 156 <groupId>mysql</groupId> 157 <artifactId>mysql-connector-java</artifactId> 158 <version>5.1.6</version> 159 </dependency> 160 <dependency> 161 <groupId>c3p0</groupId> 162 <artifactId>c3p0</artifactId> 163 <version>0.9.1.2</version> 164 </dependency> 165 </dependencies> 166 167 <!-- 设置JDK 版本 --> 168 <build> 169 <plugins> 170 <plugin> 171 <groupId>org.apache.maven.plugins</groupId> 172 <artifactId>maven-compiler-plugin</artifactId> 173 <version>3.1</version> 174 <configuration> 175 <source>1.7</source> 176 <target>1.7</target> 177 <encoding>UTF-8</encoding> 178 </configuration> 179 </plugin> 180 </plugins> 181 </build> 182 183 184 <modules> 185 <module>zipeng-dao</module> 186 <module>zipeng-service</module> 187 <module>zipeng-web</module> 188 </modules> 189 </project>
Maven拆分项目
案例:拆分ssh项目
实现建立父工程,packing类型为:pom,其pom.xm包含所有依赖
创建子模块,其父模块为上面
把子模块用install打成jar包,变成另外项目的依赖(核心步骤)