Maven

简介

Maven是什么

  • Maven是基于项目对象模型(POM,project object model),可以通过一小段描述信息(配置)来管理项目的构建,报告和文档的软件项目管理工具
  • 通过pom.xml文件的配置获取jar包,而不用手动去添加jar包
  • 合理叙述项目间的依赖关系

Maven下载安装

  1. http://maven.apache.org/download.cgi
  2. 安装
  3. 直接解压就可以
  4. 目录结构
  5. bin
  6. 包含了Maven的运行脚本文件
  7. boot
  8. Maven的依赖的类加载器
  9. conf
  10. Maven的全局配置文件(settings.xml),定制Maven的运行行为
  11. <localRepository>D:\java\mavenRepository</localRepository>
  12. D:\java\mavenRepository作为本地仓库的路径,可以通过核心配置文件来改
  13. lib
  14. 依赖jar
  15. maven环境变量配置
  16. 配置的目的
  17. 想要在任何地方都能够运行mvn命令
  18. 配置两个一个Home 一个home/bin
  19. 变量名:MAVEN_HOME
  20. 变量值:D:\java\apache-maven-3.5.4
  21. 变量名:Path
  22. 变量值:D:\java\apache-maven-3.5.4\bin
  23. 打开命令行输入mvn -v 查看结果

Maven使用

Maven规定了一套默认的项目格式

  1. src/main/java
  2. 存放项目的.java文件
  3. src/main/resources
  4. 存放项目资源文件,如springstruts2配置文件,db.properties
  5. src/main/webapp
  6. 存放jspcssimage等文件
  7. src/test/java
  8. 存放所有测试.java文件,如JUnit测试类
  9. src/test/resources
  10. 测试资源文件
  11. pom.xml
  12. 主要要写的maven配置文件
  13. target
  14. 项目由maven自动输出位置

创建第一个Maven项目

  • 确保idea安装了Maven插件
  • 创建Maven工程



  • 添加Maven依赖jar包(会自动把jar依赖到项目当中)

    pom.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>com.itlike</groupId>
    6. <artifactId>SSMMavenPro</artifactId>
    7. <version>1.0</version>
    8. <packaging>war</packaging>
    9. <name>SSMMavenPro Maven Webapp</name>
    10. <!-- FIXME change it to the project's website -->
    11. <url>http://www.example.com</url>
    12. <properties>
    13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    14. <maven.compiler.source>1.8</maven.compiler.source>
    15. <maven.compiler.target>1.8</maven.compiler.target>
    16. <!--定义版本号 ${org.springframework.version}-->
    17. <org.springframework.version>5.0.7.RELEASE</org.springframework.version>
    18. <org.mybatis.version>3.4.6</org.mybatis.version>
    19. </properties>
    20. <dependencies>
    21. <!--单元测试-->
    22. <dependency>
    23. <groupId>junit</groupId>
    24. <artifactId>junit</artifactId>
    25. <version>4.12</version>
    26. <scope>test</scope>
    27. </dependency>
    28. <!--lombok-->
    29. <dependency>
    30. <groupId>org.projectlombok</groupId>
    31. <artifactId>lombok</artifactId>
    32. <version>1.16.6</version>
    33. </dependency>
    34. <!-- servlet-api -->
    35. <dependency>
    36. <groupId>javax.servlet</groupId>
    37. <artifactId>servlet-api</artifactId>
    38. <version>2.5</version>
    39. <scope>provided</scope>
    40. </dependency>
    41. <!--mysql驱动-->
    42. <dependency>
    43. <groupId>mysql</groupId>
    44. <artifactId>mysql-connector-java</artifactId>
    45. <version>5.1.21</version>
    46. </dependency>
    47. <!-- druid -->
    48. <dependency>
    49. <groupId>com.alibaba</groupId>
    50. <artifactId>druid</artifactId>
    51. <version>1.0.14</version>
    52. </dependency>
    53. <!-- common-lang -->
    54. <dependency>
    55. <groupId>commons-lang</groupId>
    56. <artifactId>commons-lang</artifactId>
    57. <version>2.6</version>
    58. </dependency>
    59. <!--spring-test-->
    60. <dependency>
    61. <groupId>org.springframework</groupId>
    62. <artifactId>spring-test</artifactId>
    63. <version>${org.springframework.version}</version>
    64. <scope>test</scope>
    65. </dependency>
    66. <!--spring-core-->
    67. <dependency>
    68. <groupId>org.springframework</groupId>
    69. <artifactId>spring-core</artifactId>
    70. <version>${org.springframework.version}</version>
    71. </dependency>
    72. <!--spring-context-->
    73. <dependency>
    74. <groupId>org.springframework</groupId>
    75. <artifactId>spring-context</artifactId>
    76. <version>${org.springframework.version}</version>
    77. </dependency>
    78. <!--spring-context-support-->
    79. <dependency>
    80. <groupId>org.springframework</groupId>
    81. <artifactId>spring-context-support</artifactId>
    82. <version>${org.springframework.version}</version>
    83. </dependency>
    84. <!--spring-expression-->
    85. <dependency>
    86. <groupId>org.springframework</groupId>
    87. <artifactId>spring-expression</artifactId>
    88. <version>${org.springframework.version}</version>
    89. </dependency>
    90. <!--spring-jdbc-->
    91. <dependency>
    92. <groupId>org.springframework</groupId>
    93. <artifactId>spring-jdbc</artifactId>
    94. <version>${org.springframework.version}</version>
    95. </dependency>
    96. <!--spring-tx-->
    97. <dependency>
    98. <groupId>org.springframework</groupId>
    99. <artifactId>spring-tx</artifactId>
    100. <version>${org.springframework.version}</version>
    101. </dependency>
    102. <!--spring-web-->
    103. <dependency>
    104. <groupId>org.springframework</groupId>
    105. <artifactId>spring-web</artifactId>
    106. <version>${org.springframework.version}</version>
    107. </dependency>
    108. <!--spring-aop-->
    109. <dependency>
    110. <groupId>org.springframework</groupId>
    111. <artifactId>spring-aop</artifactId>
    112. <version>${org.springframework.version}</version>
    113. </dependency>
    114. <!--spring-webmvc-->
    115. <dependency>
    116. <groupId>org.springframework</groupId>
    117. <artifactId>spring-webmvc</artifactId>
    118. <version>${org.springframework.version}</version>
    119. </dependency>
    120. <!-- aspectj -->
    121. <dependency>
    122. <groupId>org.aspectj</groupId>
    123. <artifactId>aspectjrt</artifactId>
    124. <version>1.7.4</version>
    125. </dependency>
    126. <!--aspectj weaver-->
    127. <dependency>
    128. <groupId>org.aspectj</groupId>
    129. <artifactId>aspectjweaver</artifactId>
    130. <version>1.7.4</version>
    131. </dependency>
    132. <!-- cglib -->
    133. <dependency>
    134. <groupId>cglib</groupId>
    135. <artifactId>cglib</artifactId>
    136. <version>3.1</version>
    137. </dependency>
    138. <!-- mybatis -->
    139. <dependency>
    140. <groupId>org.mybatis</groupId>
    141. <artifactId>mybatis</artifactId>
    142. <version>${org.mybatis.version}</version>
    143. </dependency>
    144. <!--mybatis-spring-->
    145. <dependency>
    146. <groupId>org.mybatis</groupId>
    147. <artifactId>mybatis-spring</artifactId>
    148. <version>1.3.0</version>
    149. </dependency>
    150. <!-- jackson-core -->
    151. <dependency>
    152. <groupId>com.fasterxml.jackson.core</groupId>
    153. <artifactId>jackson-core</artifactId>
    154. <version>2.9.4</version>
    155. </dependency>
    156. <!--jackson-databind-->
    157. <dependency>
    158. <groupId>com.fasterxml.jackson.core</groupId>
    159. <artifactId>jackson-databind</artifactId>
    160. <version>2.9.4</version>
    161. </dependency>
    162. <!--jackson-annotations-->
    163. <dependency>
    164. <groupId>com.fasterxml.jackson.core</groupId>
    165. <artifactId>jackson-annotations</artifactId>
    166. <version>2.9.4</version>
    167. </dependency>
    168. <!--slf4j-api-->
    169. <dependency>
    170. <groupId>org.slf4j</groupId>
    171. <artifactId>slf4j-api</artifactId>
    172. <version>1.7.6</version>
    173. </dependency>
    174. <!--slf4j-log4j12-->
    175. <dependency>
    176. <groupId>org.slf4j</groupId>
    177. <artifactId>slf4j-log4j12</artifactId>
    178. <version>1.7.6</version>
    179. </dependency>
    180. <!--log4j-->
    181. <dependency>
    182. <groupId>log4j</groupId>
    183. <artifactId>log4j</artifactId>
    184. <version>1.2.17</version>
    185. </dependency>
    186. <!--commons-fileupload-->
    187. <dependency>
    188. <groupId>commons-fileupload</groupId>
    189. <artifactId>commons-fileupload</artifactId>
    190. <version>1.3.1</version>
    191. </dependency>
    192. <!-- jstl -->
    193. <dependency>
    194. <groupId>jstl</groupId>
    195. <artifactId>jstl</artifactId>
    196. <version>1.2</version>
    197. </dependency>
    198. <!-- standard -->
    199. <dependency>
    200. <groupId>taglibs</groupId>
    201. <artifactId>standard</artifactId>
    202. <version>1.1.2</version>
    203. </dependency>
    204. <!--pagehelper-->
    205. <dependency>
    206. <groupId>com.github.pagehelper</groupId>
    207. <artifactId>pagehelper</artifactId>
    208. <version>4.1.4</version>
    209. </dependency>
    210. </dependencies>
    211. <build>
    212. <finalName>SSMMavenPro</finalName>
    213. <plugins>
    214. <plugin>
    215. <groupId>org.mybatis.generator</groupId>
    216. <artifactId>mybatis-generator-maven-plugin</artifactId>
    217. <version>1.3.2</version>
    218. <configuration>
    219. <verbose>true</verbose>
    220. <overwrite>false</overwrite>
    221. </configuration>
    222. <dependencies>
    223. <dependency>
    224. <groupId>mysql</groupId>
    225. <artifactId>mysql-connector-java</artifactId>
    226. <version>5.1.21</version>
    227. </dependency>
    228. </dependencies>
    229. </plugin>
    230. </plugins>
    231. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    232. <plugins>
    233. <plugin>
    234. <artifactId>maven-clean-plugin</artifactId>
    235. <version>3.1.0</version>
    236. </plugin>
    237. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
    238. <plugin>
    239. <artifactId>maven-resources-plugin</artifactId>
    240. <version>3.0.2</version>
    241. </plugin>
    242. <plugin>
    243. <artifactId>maven-compiler-plugin</artifactId>
    244. <version>3.8.0</version>
    245. </plugin>
    246. <plugin>
    247. <artifactId>maven-surefire-plugin</artifactId>
    248. <version>2.22.1</version>
    249. </plugin>
    250. <plugin>
    251. <artifactId>maven-war-plugin</artifactId>
    252. <version>3.2.2</version>
    253. </plugin>
    254. <plugin>
    255. <artifactId>maven-install-plugin</artifactId>
    256. <version>2.5.2</version>
    257. </plugin>
    258. <plugin>
    259. <artifactId>maven-deploy-plugin</artifactId>
    260. <version>2.8.2</version>
    261. </plugin>
    262. </plugins>
    263. </pluginManagement>
    264. </build>
    265. </project>

Maven仓库

  • Maven所有的Jar包都是放到maven仓库当中
  • 在项目当中是对仓库jar包的引用

Maven仓库

  1. 本地仓库
  2. 本地存放jar的目录
  3. 私服
  4. 私人搭建的服务器,一般在企业内部局域网使用
  5. 中央仓库
  6. Maven内置了一个远程仓库的地址,它就是中央仓库

Maven找jar包的过程

  • Maven查找引入jar包时, 会先到本地仓库当中查找,没有找到就会到私服中找,也没有就去到远程中央仓库查找.
  • 找到后会下载到本地仓库,下次就不用到远程仓库查找了
  • 最终都会把jar包下载到本地仓库
  • Maven工程最终引用的都是本地的jar包

设置本地仓库目录地址

  1. 默认本地仓库
  2. C:\Users\LE\.m2\repository
  3. 修改本地仓库
  4. 首先,把maven安装目录configsetting.xml复制一份到.m2文件夹(C:\Users\LE\.m2路径)下
  5. 其次,打开setting.xml修改本地仓库地址
  6. <localRepository>D:\java\mavenRepository</localRepository>

阿里云镜像

  1. <mirror>
  2. <id>alimaven</id>
  3. <mirrorOf>central</mirrorOf>
  4. <name>aliyun maven</name>
  5. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  6. </mirror>

Jar包的坐标

  • 组织groupId
  • 项目artifactId
  • 版本version

Maven常用命令

  1. validate
  2. 确保当前配置和 POM 的内容是有效的
  3. clean
  4. 删除target目录下及其目录下的所有内容
  5. mvn compile
  6. java文件编译成二进制放到target目录当中
  7. test
  8. 运行测试用例
  9. package
  10. 打包工程
  11. install
  12. maven打成的包发布到本地仓库当中
  13. 后一个命令执行时, 前面命令都会执行

命令

Maven作用范围(scope值)

  1. pom.xml文件
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-test</artifactId>
  5. <version>${org.springframework.version}</version>
  6. <scope>test</scope>
  7. </dependency>
  8. compile
  9. 默认值 他表示被依赖项目需要参与当前项目的编译,还有后续的测试,运行周期也参与其中,是一个比较强的依赖。
  10. 打包的时候通常需要包含进去
  11. test
  12. 依赖项目仅仅参与测试相关的工作,包括测试代码的编译和执行。
  13. 不会被打包,例如:junit
  14. runtime
  15. 表示被依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。
  16. compile相比,跳过了编译而已。
  17. 例如JDBC驱动,适用运行和测试阶段
  18. provided
  19. 打包的时候可以不用包进去,别的设施会提供。
  20. 事实上该依赖理论上可以参与编译,测试,运行等周期。
  21. 相当于compile,但是打包阶段做了exclude操作
  22. system
  23. 从参与度来说,和provided相同,不过被依赖项不会从maven仓库下载,而是从本地文件系统拿。需要添加systemPath的属性来定义路径

Maven创建Web工程

注意事项

  • generating project in batch mode 很慢,是因为maven获取archetype-catalog.xml导致。
  • 用浏览器打开http://repo1.maven.org/maven2/archetype-catalog.xml很慢。
  • 解决方法: 在用maven创建项目时在properties中添加 archetypeCatalog=internal,让maven读取本地配置即可。

tomcat插件

  1. <plugins>
  2. <plugin>
  3. <groupId>org.apache.tomcat.maven</groupId>
  4. <artifactId>tomcat7-maven-plugin</artifactId>
  5. <version>2.2</version>
  6. <configuration>
  7. <port>8081</port>
  8. <path>/</path>
  9. </configuration>
  10. </plugin>
  11. </plugins>

SSM项目实战 之 Maven的更多相关文章

  1. 云计算Docker全面项目实战(Maven+Jenkins、日志管理ELK、WordPress博客镜像)

    2013年,云计算领域从此多了一个名词“Docker”.以轻量著称,更好的去解决应用打包和部署.之前我们一直在构建Iaas,但通过Iaas去实现统一功  能还是相当复杂得,并且维护复杂.将特殊性封装到 ...

  2. SSM项目实战 之 权限管理系统

    目录 SSM权限管理系统 项目搭建 1.创建Maven-webapp工程 2.SSM框架集成 3.添加代码生成器 主页搭建 EasyUI主页 员工列表 1.在tree当中指定跳转的地址--暂时用tre ...

  3. SSM项目实战

    1.  实战才是检验学的怎么样的标准,一个小项目,运行老是出错,加上自己一贯的马虎的习惯,不严谨,就使学习之路更加的曲折了,感觉自己在这一行中比较吃力,但是自己选择了这条路,就得好好走下去,不要怀疑自 ...

  4. SSM项目实战 之 Shiro

    目录 Shiro 概述 shiro核心概念 核心类 整体类图 主要概念 Shiro架构 认证 什么是认证 关键对象 使用ini完成认证 认证流程 自定义realm 散列密码 授权 什么是授权 使用in ...

  5. SSM项目实战 之 EasyUI

    目录 EasyUI 简介 概述 使用EasyUI panel组件 简介 示例 JS形式及属性介绍 panel事件与方法 Window组件 概述 使用 行为 dialog 概述 使用 tabs组件 概述 ...

  6. Java归去来第4集:java实战之Eclipse中创建Maven类型的SSM项目

    一.前言 如果还不了解剧情,请返回第3集的剧情          Java归去来第3集:Eclipse中给动态模块升级 二.在Eclipse中创建Maven类型的SSM项目 2.1:SSM简介 SSM ...

  7. maven3实战之maven使用入门(使用archetype生成项目骨架)

    maven3实战之maven使用入门(使用archetype生成项目骨架) ---------- maven提供了archetype以帮助我们快速勾勒出项目骨架.以Hello World为例,我们使用 ...

  8. Maven 搭建 SSM 项目 (oracle)

    简单谈一下maven搭建 ssm 项目 (使用数据库oracle,比 mysql 难,所以这里谈一下) 在创建maven 的web项目时,常常会缺了main/java , main/test 两个文件 ...

  9. IntelliJ IDEA通过maven构建ssm项目找不到mapper

    idea运行ssm项目的时候一直报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 原 ...

随机推荐

  1. MySQL数据库之互联网常用架构方案

    一.数据库架构原则 高可用 高性能 一致性 扩展性 二.常见的架构方案 方案一:主备架构,只有主库提供读写服务,备库冗余作故障转移用 jdbc:mysql://vip:3306/xxdb 高可用分析: ...

  2. cs/bs架构的区别

    Client/Server是建立在局域网的基础上的,基于客户端/服务器,安全,响应快,维护难度大,不易拓展,用户面固定,需要相同的操作系统. Browser/Server是建立在广域网的基础上的,基于 ...

  3. requests模块 高级应用

    目录 requests模块 高级应用 HttpConnectinPool 问题解决 IP代理 简单使用代理 代理池 cookie的处理 页面中验证码识别 使用 multiprocessing.dumm ...

  4. CSS-2D动画笔记

    概念: 2D 动画要是使用 transform 属性来实现文字或图像的的各种变形效果,如位移.缩放.旋转.倾斜等 transform属性变形方法: translate():位移 将元素沿着水平方向(X ...

  5. ETC1/DXT1 compressed textures are not supported when publishing to iPhone

    Build application in Unity 2017.20f3 用Unity2017/2018编译iPhone版本出现以下错误: ETC1(or DXT1) compressed textu ...

  6. 高阶函数概念以及map/filter/reduce

    什么样的函数叫高阶函数:map(func, *iterables) --> map object 条件:1.函数接受函数作为参数 2.函数的返回值中包含函数 num_l = [1,2,3,4,5 ...

  7. H3C 802.11n的频宽模式

  8. 数据库开发-Django ORM的一对多查询

    数据库开发-Django ORM的一对多查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.联合主键问题 CREATE TABLE `employees` ( `emp_no` ...

  9. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组

    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...

  10. moviepy改进的想码

    这个要比前一个厚实点. 更改视频亮度,增加字幕,去除音轨,淡入特效,转换,截取时间,控制位置,组合图框,合成多段, 嗯,很多都有了. from django.test import TestCase ...