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包坐标时一直报错显示红色,本地仓库和官网上的中央仓库都试过了就是依赖 ...
随机推荐
- 进fastreboot
1.红米1s 关机后,开机键+音量键下 同时按 2.红米note 关机后,开机键+音量键上 同时按 3. 4. 5.
- git备份sublime插件及配置
github备份sublime配置 sublime使用的时间长了,渐渐的就积累了一些有用甚至离不开的插件.但是有时候系统会出点问题,或者换电脑什么的,这时候要想在找回那个曾经的sublime就不那么容 ...
- 转:用C++实现的一种插件体系结构-----概述
用C++实现的一种插件体系结构-----概述 本文讨论一种简单却有效的插件体系结构,它使用C++,动态链接库,基于面向对象编程的思想.首先来看一下使用插件机制能给我们带来哪些方面的好处,从而在适当时候 ...
- 【转】 如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测
系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调试内存问题的恶梦.所以,在实践中会用到很多工具来 ...
- JAVA06数组之动手动脑问题解决
一.随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中. 1.设计思路:首先生成10个随机数,然后存放至长度至少是10的数组中,然后计算10个随机 ...
- 优化Google字体 全面加速WordPress
从5月27号起,由于某些原因,Google服务在大陆的崩溃影响了数百万的站长,因为很多wordpress主题都在使用Google的在线字体方案-google fonts包括新版的WordPress 后 ...
- Sql Server 删除所有表(转)
http://www.cnblogs.com/jys509/p/3589468.html 首先必须要清空所有表的外键 DECLARE c1 cursor for select 'alter tabl ...
- 三、最小化的Spring XML配置
Spring 提供了自动装配(自动识别如何装配Bean的依赖关系)和自动检测(检测哪些类需要被配置成Spring Bean) 1.自动装配Bean的属性 1.1四种类型得自动装配:byName.byT ...
- angularjs的一些坑关于 $sec
今天遇到$sec的问题 app.filter('to_trusted', ['$sce', function ($sce) { return function (text) { return $sce ...
- Wpf之Xaml属性值和特性值(一)
其实我一直很好奇在xaml中,通过Attribute=Value这种方式可以进行对元素的描述, 例如: <Rectangle Name=” rectangle” Fill=”Blue”/> ...