java1234初学maven
第一讲:
maven
maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。
maven安装与下载:
、确定jdk已经安装并且配置
、安装maven
、配置maven环境变量:M2_HOME D:\maven\apache-maven-3.3. (就是maven的安装目录) HelloWorld的实现
modelVersion:POM模型版本4..0固定
groupId:一般指某个公司或者某个组织的某个项目,比如org.springframework
artifactId:一般指某个具体项目的某个具体模块,比如spring-context
Version:项目的版本
Maven常见命令:
complie 编译
clean 清空
test 测试
package 打包
install 将项目安装到本地仓库
Mvn远程仓库地址:http://mvnrepository.com/
第二讲,我的实践:
使用idea创建helloWorld的maven项目:
Mvn远程仓库地址:http://mvnrepository.com/ 这个很重要,我们的需要什么jar包都可以在上面搜寻坐标
idea的时候文件夹的颜色标识这个也重要,不然可能无法new出想要的file 然后就是测试各个maven命令:
第三讲:
maven仓库的概念:
Maven远程仓库配置文件:
$M2_HOME/lib/maven-model-builder-3.3.9jar
下的文件:org/apache/maven/model/pom-4.0.0.xml
-<repositories> -<repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> -<snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
maven依赖:
我们可以将一个项目install到本地仓库,然后本地其他项目引用这个项目,就是引用这个项目的下标,如:
<dependencies>
<dependency>
<groupId>com.java1234.user</groupId>
<artifactId>user-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
我们做个例子:
使用maven创建ssm项目的各个模块并且成功运行:
关键代码:
其实没有什么关键代码,就是ssm的整合搭建
依赖的特性:
最短路径原则和最先声明原则。就是如果A项目中引用了B和C项目,B中间接引用了F,C直接引用了F,那么A中引用F依赖是通过C来达到的,就近原则。当两边路径相等的时候,哪个项目被先引用就使用哪个项目得到依赖。
第四讲:
maven的聚合与继承:
聚合:
新的maven项目中,这样聚合其他项目:
<modules>
<module>../user-dao</module>
<module>../user-service</module>
</modules>
注意该项目中package只能为pom:
<packaging>pom</packaging>
作用:方便统一管理
继承:
继承实现:
新建父项目,然后在子项目坐标前加上继承描述:
<parent>
<groupId>com.java1234.user</groupId>
<artifactId>user-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent>
relativePath一定要加上。
注意:一定要在子项目坐标前,然后就可以使用继承了。
方便版本版本管理与依赖管理
使用了继承子项目的groupId可以删掉了,默认使用的是父项目的groupId。默认状况下 version和packing也是继承父项目的,适当情况下可以省略不写。webapp项目注意packing不要省略,因为父项目一般是jar形式打包,webapp是war包
每个子项目中都有好多jar,它们可能太多的时候导致版本混乱,这样的话我们可以将这些依赖都假如到父类的dependency里面,然后子模块继承父模块可以省略子模块中的版本和scope。都使用父类的版本其他。
注意是放在dependencyManagement里面的dependencies里面添加依赖
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
可以在pom里面声明版本属性,然后在dependency里面应用它,我们常用来管理版本,使用方式看例子:
<properties>
<spring.version>4.3..RELEASE</spring.version>
<mybatis.version>3.4.</mybatis.version>
</properties> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
我再父项目中装载了其他的子项目,便于管理。使用的idea,感觉还是新建一个项目和新建其他模块比较好一点。虽然效果好像差不多。
最终的项目各模块的pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.</modelVersion> <parent>
<groupId>com.java1234.user</groupId>
<artifactId>user-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent> <artifactId>user-web</artifactId>
<packaging>war</packaging> <name>user-web Maven Webapp</name>
<url>http://maven.apache.org</url> <dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency> <dependency>
<groupId>com.java1234.user</groupId>
<artifactId>user-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> </dependencies> <build>
<finalName>user-web</finalName>
</build> </project>
user-web的pom
<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.</modelVersion> <parent>
<groupId>com.java1234.user</groupId>
<artifactId>user-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent> <artifactId>user-dao</artifactId>
<packaging>jar</packaging> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
user-dao的web
<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.</modelVersion> <parent>
<groupId>com.java1234.user</groupId>
<artifactId>user-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent> <artifactId>user-service</artifactId>
<packaging>jar</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> <dependency>
<groupId>com.java1234.user</groupId>
<artifactId>user-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> </dependencies> </project>
user-service的web
<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.</modelVersion> <parent>
<groupId>com.java1234.user</groupId>
<artifactId>user-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent> <artifactId>user-service</artifactId>
<packaging>jar</packaging> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> <dependency>
<groupId>com.java1234.user</groupId>
<artifactId>user-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> </dependencies> </project>
user-aggregator的pom
<?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.</modelVersion> <groupId>com.java1234.user</groupId>
<artifactId>user-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <properties>
<spring.version>4.3..RELEASE</spring.version>
<mybatis.version>3.4.</mybatis.version>
</properties> <modules>
<module>../user-dao</module>
<module>../user-service</module>
<module>../user-web</module>
</modules> <dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.</version>
</dependency> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> </dependencies> </dependencyManagement> </project>
user-parent的pom
java1234初学maven的更多相关文章
- 初学maven的一些配置
初学Maven的一些配置 1.maven的安装 2.从官网下载3.6.1版本后,高级版本可能会出现不兼容 jdk1.8 3.配置maven 在 settings.xml <settings> ...
- 使用maven来管理java项目
初学maven,简单总结一下学习心得,若有不对的地方,欢迎各位大神给我指正~ 总结分为6个部分 maven概述 maven安装 maven项目结构和创建方法 maven配置文件settings.xml ...
- Maven-004-使用 Nexus 搭建 maven 私服
从去年至今,自己一直在学习自动化测试工具,想利用自动化工具尽可能的将重复的.关键的.耗时耗力的工作实现自动化,减轻日常测试工作,提升测试效率.在学习的过程中,将 maven 作为了项目开发管理工具,进 ...
- Maven-003-私人定制 maven archetype
在使用 Maven 创建项目模块的时候,依据其默认的 archetype 模板,创建出的目录.及默认的单元测试工具为 JUnit 3.8.1,而且有些常用的资源文件目录.配置文件(例如:Log4J 的 ...
- Maven加依赖包
对于初学maven的人来说刚开始会有个困惑,那就是怎么知道依赖的jar的groupId和atrifactId是什么, 比如要依赖mybatis,会在pom.xml中配置如下: <dependen ...
- Maven的JAR包仓库,不用再百度搜JAR包了!
http://search.maven.org/ 今天初学Maven,发现Maven的中央仓库里差点儿什么jar都有...........还有各种版本号... 你值得拥有!
- maven配置环境
今天初学maven,先学习一下如何在windows下面配置maven,当然你要先配置好jdk的环境. 第一步,上官网下载maven插件,网址是:点击打开链接 第二步,解压文件夹,放在某一个盘符下,我是 ...
- Maven Web项目出现org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException错误
1. 问题描述 初学Maven,新建了一个基于Web骨架的Web项目,jar 包也导好了,作用域也设置正确了,Tomcat也正常运行了,可是就是说编译错误. 2. 问题原因 虽然我配置了Tomcat ...
- maven 报的一堆错
今天初学maven,刚开始下载的是Apache-maven-3.6.2然后配置运行一个servlet,但是在pom.xml中写jar包坐标时一直报错显示红色,本地仓库和官网上的中央仓库都试过了就是依赖 ...
随机推荐
- Baltic2008联合内阁
Description N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如 ...
- String or binary data would be truncated 解决办法
原因: 一般出现这个问题是因为数据库中的某个字段的长度小,而插入数据大 解决: 找到相应字段,修改表结构,使表字段大小相同或大于要插入的数据
- Virtualbox安装USB2.0/3.0
系统:Ubuntu16.04 软件:Virtualbox5.1 1.打开Virtualbox,不启动虚拟系统. 2.点击设置->USB->启动usb2.0. 3.若发现不能启用,则到官网下 ...
- ES6(一)ECMAscript6介绍
nvm-windows Node.js是JavaScript语言的服务器运行环境,对ES6的支持度比浏览器更高.通过Node,可以体验更多ES6的特性.建议使用版本管理工具nvm,来安装Node,因为 ...
- Leetcode--Swap Nodes in Pairs
最傻的方法: ListNode *swapPairs(ListNode *head) { if (head == NULL) return NULL; ListNode *temp = ); List ...
- Python学习笔记之抽象
一.创建函数 >>> import math >>> x=1 >>> y=math.sqrt >>> callable(x) # ...
- VPN安装后报错:Reason442 & Error56
VPN安装后一直报错,同样的32位安装包别人安装是正常,自己安装就不正常了,考虑到是自己电脑配置的问题. 经过一番努力,解决了问题,下面就本次解决过程做一个小小的总结. (1)确保VPN Servic ...
- 虚拟机VM安装linux系统
废话不多说,直接上图文过程: 1.首先是下载linux镜像文件了(CentOS,Ubuntu等,根据自己的实际需求下载) linux镜像下载(提供几个32位的linux镜像下载,如有其他需求请自行百度 ...
- Javascript学习笔记2.2 Javascript与DOM选项卡(滑动门)案例详解
学习了DOM的知识,今天开始做些练习,想到了一个网页滑动门的特效,见下图: 1.通过建立索引实现 <!doctype html> <html> <head> < ...
- VS2013 破解
密钥 : BWG7X-J98B3-W34RT-33B3R-JVYW9