java1234初学maven

时间:2023-03-10 06:27:39
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项目:

java1234初学maven

java1234初学maven

Mvn远程仓库地址:http://mvnrepository.com/ 这个很重要,我们的需要什么jar包都可以在上面搜寻坐标
idea的时候文件夹的颜色标识这个也重要,不然可能无法new出想要的file 然后就是测试各个maven命令:
java1234初学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>

java1234初学maven

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来达到的,就近原则。当两边路径相等的时候,哪个项目被先引用就使用哪个项目得到依赖。

java1234初学maven

第四讲:

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