maven-小白入门学习笔记1

来源:互联网 发布:服务业知乎 编辑:程序博客网 时间:2024/06/09 21:12

1.maven的目录结构

项目目录下
src
-main
-java
-package(自定义的java包 代码)
-test
-java
-package

-resource






测试类 test代码编写时用到Junit
import org.junit.*

2.pom.xml

maven使用pom.xml管理项目

<modelVerion>4.0.0</modelVersion>表示maven pom版本号

<groupId>com.bupt.mavenEx</groupId>项目包名
<artifactId>mavenEx</artifactId>项目名
<version></version>

<dependencies>
<dependency>
<groupId>com.bupt.mavenEx</groupId>项目包名
<artifactId>mavenEx</artifactId>项目名(src上一层的项目名)
<version></version>版本号
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>

<groupId>com.bupt.mavenEx</groupId>项目包名
<artifactId>mavenEx</artifactId>项目名
<version></version>版本号
这些相当于jar包的坐标


创建maven项目时就会让指定groupId和artifactId了。
如果本地有这些jar包,就会查找到,如果没有就会去maven的中央仓库中查找下载

3.target文件夹

会默认生成target文件夹,字节码文件和测试报告

4.常用命令mvn

mvn -v 查看maven版本
compile 编译 要进入到项目的目录下才可以编译
test 测试
package 打包,打包生成的jar在target中
clean 删除target目录 整个目录都会被删除掉
install 将jar包安装到本地仓库中,就是把当前项目发布成一个jar到本地仓库
   那么本地的其他项目就可以在dependency中通过groupId引用它并使用

5.archetype插件

创建目录骨架
使用命令 mvn archetype:generate
或者mvn archetype:generate -DgroupId=xxx -DartifactId=xxx -Dversion=xxx -Dpackage=xxx(代码所存在的包)
archetype是原型的意思,选择创建maven项目的原型模板

6.坐标

groupId(com.xxx.项目名)
artifactId(项目名-模块名)
version(版本号)
构件都是通过坐标定位的
构件存在于仓库中

7.仓库

本地仓库

中央远程仓库(maven-》lib-》maven-model-builder-》pom-4.0.0.xml所有的pom都继承这个pom)
这个pom.xml提供中央远程仓库的网址

镜像仓库
访问会更快更稳定。
修改镜像仓库位置
maven->conf->settings.xml->mirrors
例如国内镜像仓库地址为maven.net.cn/content/groups/public
id 镜像仓库的id
mirrorOf 所有指向这里的仓库(原仓库)都用镜像代替

修改本地仓库位置
下载的远程仓库默认存放在:c盘用户名下.m2->repository->com
修改settings.xml->写这个localRepository,使用D:/xxx/xxx/repo
把settings也复制一份到本地仓库中repo下

8.生命周期

clean default(构建项目compile test package install) site(生成站点)
在一个生命周期运行时例如install,前面阶段会顺序执行。

9.插件

在pom.xml中加入插件
<build>
<plugins>
<plugin>
<groupId><groupId>
<artifactId></artifactId>
<version></version>
<executions>
<execution>
<phase></phase>在哪个阶段运行插件
<goals>
<goal></goal>
</goals>
</excution>
</excutions>
</plugin>
</plugins>
</build>



阅读全文
0 0
原创粉丝点击