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包坐标时一直报错显示红色,本地仓库和官网上的中央仓库都试过了就是依赖 ...
随机推荐
- Netscape HTTP Cooke File Parser In PHP
http://www.hashbangcode.com/blog/netscape-http-cooke-file-parser-php I recently needed to create a f ...
- Easy Sysprep更新日志-skyfree大神
Easy Sysprep更新日志: Skyfree 发表于 2016-1-22 13:55:55 https://www.itsk.com/forum.php?mod=viewthread&t ...
- 【转】 linux内存管理
一 为什么需要使用虚拟内存 大家都知道,进程需要使用的代码和数据都放在内存中,比放在外存中要快很多.问题是内存空间太小了,不能满足进程的需求,而且现在都是多进程,情况更加糟糕.所以提出了虚拟内存,使得 ...
- java对国际化的支持
国际化的英文为Internationalization,这个也太长了,所以它又称为I18n(英文单词 internationalization的首末字符i和n,18为中间的字符数). 除了i18n还有 ...
- JavaWeb前端:JQuery
Jquery基本概念 什么是Jquery Jquery是一个开源的,集成了Javascript,CSS,DOM,AJAX的前端框架:它诞生于2006年,最初是为了简化JavaScript开发而产生的, ...
- php file_get_contents() 用法
php 需要访问某个网页 <?php $fh= file_get_contents('http://www.baidu.com/'); echo $fh; ?> 知识扩充 file_get ...
- 本地hosts临时域名访问
当刚购买了空间,域名和空间还未进行绑定,可以用临时域名访问主机调试网站.您可通过本地hosts指向访问网站,具体方法如下: 特别说明:设置以后,只有当前设置的电脑才能访问,其他电脑访问无效. 第一步: ...
- 前端面试题之Html和CSS
又到了毕业季,很多小伙伴们都到了找工作的时候了,好多小伙伴问我有前端的面试题么?答:没有. 呃呃… … 小伙伴本宝宝真的没有骗你们,我从毕业到现在一直在一家公司没有换过,所以手里压根没有面试题.我们公 ...
- jquery选择相同ID
jQuery中$("#id")只能选择第一个对象,不能选择所有相同id的元素. 通过 $("input[id='xxxx']"); 可以选择多个相同id的元 ...
- 关于ssh上传文件
今天用ssh传项目到公司总部的服务器上,报了错误: encountered 1 errors during the transfer 重启ssh再次上传还是一样的错误,然后我让公司那里重启一下服务器, ...