maven笔记
1.下载maven
2.解压
3.配置环境变量
4.配置本地仓库 conf/settings.xml 的localRepository节点
如果不配置本地仓库,那么默认使用.m2文件作为本地仓库目录
配置本地仓库
在conf/settings.xml中加入如下节点,d:/maven/repository就是本地仓库了,当运行mvn时,首先去这里找插件,如果没有就去中央仓库下载到本地仓库。
<localRepository>d:/maven/repository</localRepository>
mvn clean 删掉target目录
mvn clean compile 编译项目,不会进行单元测试,不会生成jar
mvn clean package 打包项目,当然了,他会编译项目,运行测试,生成jar。
mvn clean install 将项目安装到本地仓库中去。
mvn clean deploy 将项目打包安装到私服。
helloworld
d:\javaproject\src\main\java
d:\javaproject\src\main\java\cn\francis\hello\Hello.java
d:\javaproject\src\test\java\cn\francis\hello\TestHello.java
d:\javaproject\pom.xml
mvn clean package
pom.xml如下
<?xml version="1.0" encoding="UTF-8"?> <!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more ~ contributor license agreements. See the NOTICE file distributed with ~ this work for additional information regarding copyright ownership. ~ The ASF licenses this file to You under the Apache License, Version 2.0 ~ (the "License"); you may not use this file except in compliance with ~ the License. You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and ~ limitations under the License. --> <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.0</modelVersion> <groupId>cn.francis.maven.hello</groupId> <artifactId>hello-first</artifactId> <version>SNAPSHOT-0.0.1</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
Hello.java如下
package cn.francis.hello; public class Hello{ public static void main(String[]args){ System.out.println("hello maven"); } public String hello(){ return "hello world"; } }
TestHello.java如下
package cn.francis.hello; import org.junit.*; import static junit.framework.Assert.*; public class TestHello{ @Test public void testHello(){ Hello h=new Hello(); assertEquals("hello world",h.hello()); } }
Eclipse下配置maven
window->preferences->maven->installations->add 添加maven
->userSettings->设置本地仓库
3.坐标查询
我们要引入一个项目,这个项目的坐标(GAV)是什么呢?可以去以下两个网站查询:
http://mvnrepository.com/
4.scope
compile 这是默认值,编译,打包用
import
provider 编译,测试用,比如servlet-api
test 测试用
runtime 运行时用,编译时不用
5依赖冲突
a->b1.0
c->b1.2
d->a,c
顺序优先:如果在d的pom中,a在前,那么就使用b1.0版本,否则使用b1.2版本.
最短路径优先:假设f->d,c,那么由于f到b1.2路径最短,所以f使用b1.2版本。
如果不想再f中使用b1.2,可以使用exclusions排除b:
<exclusions>
<exclusion>
<groupId>b</groupId>
<artifactId>b</artifactId>
<exclusion>
</exclusions>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!