maven 自我学习笔记

2023-01-04,,

1、常用网站:
maven.apache.org
http://mvnrepository.com/
 
2、命令
mvn -v 查看maven的版本
mvn -compile 在项目的根目录下编译项目
mvn -test 测试项目
mvn -clean 删除项目的target目录(字节码和测试报告)
mvn -install 安装jar包到本地仓库
mvn -package 将项目打包
 依赖说明:
在maven中引入其他依赖,项目首先去本地仓库中查找,若没有找到,则去中央仓库中查找,并下载到本地仓库,若两者都没有则报错
 
3、maven 命令行创建项目 自动化创建目录结构命令:
1、mvn archetype : generate 
2、mvn archetype : generate -DgroupId=   -DartifactId= -Dversion= -Dpackage=
 
4、pom 文件说明
<project>
 
<modelVersion>4.0.0</modelVersion>指定了当前pom的版本
 
<groupId>反写的公司网址+项目</groupId>
<artifactId>模块的一个标识,一般用项目名+模块名</artifactId>
<version>当前项目的版本号</version>
第一个零表示大的版本号
第二个零表示分支版本号
第三个零表示小版本号
0.0.1snapshot(快照版)
alpha内部测试
beta公测
Release稳定
GA正式发布
 
<packaing></packaging>表示打包的方式,默认是jar
<name></name>项目的描述名 
<url></url>项目的地址
<description></description> 项目的描述
<developers></developers>开发人员信息
<licenses></licenses>许可证
 
依赖列表
<dependencies>
    <dependency>
        <groupId></groupId>
        <artifactId></artifactId>
        <version></version>
        <type></type>
        <scope>test/compile/runtime(编译和运行时有效)/provided(编译和测试有效)/system(与本地系统有关)/import(从其他项目引用过来的依赖)/</scope>依赖范围(编译、测试、运行)
        <optional>true/false</optional>设置以来是否可选
        <exclusions>排除依赖
            <exclusion>
                <groupId></groupId>
                <artifactId></artifactId>
                <version></version>
             <exclusion>
        </exclusions>
    </dependency>
</dependencies>
 
依赖的管理,并不会被运行,定义在父模块中,供子模块继承使用的
<dependencyMangement>
    <dependencies>
        <dependency>
        </dependency>
    </dependencies>
</dependencyMangement>
 
<build>
插件的列表
    <plugins>
    <plugin>
        <groupId><groupId>
        <artifactId></artifactId>
        <version></version>
    </plugin>
    </plugins>
</build>
<parent></parent>通常用于子模块对父模块的继承
<modules></modules>对多个模块进行一起编译
</project>
 
依赖冲突:A和B依赖不同版本的相同的构件
1、短路优先
A依赖B B依赖C C依赖X(jar)
A依赖D D依赖X(jar) (优先)
2、先声明先优先,相同路径下先声明的优先
 
聚合(多个项目进行聚合,一起编译和运行)
<packaging>pom</packaging>
<modules>
    <module>其他项目的路径</module>
</modules>
 
继承
被继承的父项目
<dependencyMangememt>
    <dependency>
        <groupId></groupId>
        <artifactId></artifactId>
        <version></version>
    </dependency>
</dependencyMangement>
 
<parent>(继承的项目坐标)</parent>
 
引用配置好的属性
<properties>
    <junit-version></junit-version> 
</properties>
<version>${junit-version}</version>
 
5、maven目录结构
src/main/java
src/main/resources
src/test/java
src/test/resources
pom.xml
 
6、注意:
创建好的web项目目录,其中的jsp报错,是因为没有添加servlet API的原因,需要在pom中添加相关依赖。
发布产品时候 不需要测试模块的代码 所以要删掉 项目右键 Properties 选择 Deployment Assembly 把测试部分的都删掉

maven 自我学习笔记的相关教程结束。

《maven 自我学习笔记.doc》

下载本文的Word格式文档,以方便收藏与打印。