JAVA框架——Maven

本文详细探讨了Maven在Java开发中的重要性,重点解析了Maven的本地仓库配置<localRepository>,该配置决定了Maven下载的依赖库存储的位置,对于项目的构建和管理具有关键作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Maven概述 
1、跨平台的构建工具  
服务于构建,是一个异常强大的构建工具,自动化构建过程(从清理、编  译、测试到生成报告,再到打包和部署。只要Maven配置好项目,输入简单命令如  :mvn clean install 即可完成一系列操作)最大化的消除了构建的重复,抽象了一个完  整的构建生命周期模型,它跨平台,对外提供了一致的操作接口,标准化构建过程  。
2、依赖管理工具和项目管理工具
通过一个坐标系统准确定位每一个构件(artifact);
通过Maven的衍生工具(如 Nexus),可以对开源类库快速定位,帮助自动下载。 maven的项目结构、测试用例命名方式等都有已经定好的规则,只要遵循  规则,用户在项目间切换时免去额外时间————约定优于配置 
3、它包含了:
一个项目对象模型 (Project Object Model), 一个项目一个pom.xml
一组标准集合,
一个项目生命周期(Project Lifecycle),
一个依赖管理系统(Dependency Management System),
 和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。
当你使用Maven的时候,你用一个明确定义的项目对象模型来描述你的项目,
然后 Maven 可以应用横切的逻辑,这些逻辑来自一组共享的(或者自定义的)插件。
二、maven安装
1、检查JDK安装(Maven可以允许在JDK1.4及以上版本,本案例示范  JDK1.7) 
打开DOC命令窗口,
输入echo %JAVA_HOME%   查看环境变量JAVA_HOME是否指向正确JDK  目录
输入java -version   查看java版本 无法执行java,则查看JDK是否正确安装
2、安装Maven
进入apache官网,点击Maven,进入maven.apache.org/download.html点击下载  apache-maven-3.3-bin.zip(根据自己需求下载);
解压至自己想要的目录;
打开环境变量设置:新建一个变量 M2_HOME(一般这样命名,也可以自  己命名) 再在path中加入  %M2_HOME%/bin;
3、检查maven
在DOC命令窗口输入  
echo %M2_HOME%  查看M2_HOME的配置
echo %path%  查看path的配置
mvn -v    查看maven的版本,成功则安装成功,否则检查环境变量,在path  中也可以不用M2_HOME,可以直接输入maven的安装目录/bin; 
  4、安装目录
bin:包含了mvn运行的脚本,在命令窗口输入mvn就是在bin中调用mvn.bat;
boot:plexus-classworlds是一个类加载器框架,提供丰富的语法以方便配置
conf:包含setting.xml:全局定制maven的行为,一般复制至  c:/Users/Administrator/.m2/中,然后修改以适应需求。不同计算机所在目录不同。
lib:包含所有Maven运行时需要的java类库
5、IDE 更方便适应Maven,构建项目就会适应IDE,如eclipse等开发工具
6、注意:c:/Users/Administrator/.m2中可能会有repository文件夹,这个是  maven的构建都会被存储在该仓库中的,放在C盘中占内存,可以删除。在setting.xml  中配置仓库所在位置:

<localRepository>D:/Program Files(x86)/Java/MavenRepository/</localRepository> 




三、在eclipse中使用Maven
1、在windows-->preference中选Maven--->installation---->add---->选择Maven  的安装目录
2、选中Maven--->User Settings 设置setting.xml 和仓库的位置 3、OK,保证配置成功此处配置是为了使用自己安装的Maven,而不是eclipse中已有的。使用的eclipse也可能不一样,有的eclipse需要自己安装maven,需要的自己百度。

3、使用私服nexus
a、只为一个项目配制(pom.xml):
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://私服nexus的ip地址:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus</name>
<url>http://私服nexus的ip地址:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
b、本机所有项目都可以使用(settings.xml):
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://私服nexus的ip地址:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus</name>
<url>http://私服nexus的ip地址:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>


<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
c、配置镜像让maven只使用nexus私服(settings.xml):
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://私服nexus的ip地址:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>


<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
d、部署构件至私服nexus(pom.xml)
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Releases Repositories</name>
<url>http://218.196.14.220:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshots Repositories</name>
<url>http://218.196.14.220:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>


为部署构件到私服nexus配置认证信息(settings.xml)
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
在setting.xml中配置,建议使用c
4、maven命令
mvn clean  : 清除
mvn clean compile : 清除并编译源文件
mvn clean test : 清除并编译源文件和测试源文件
mvn clean package : 清除并编译源文件和测试源文件后, 再打包
mvn clean install : 清除并编译源文件和测试源文件后, 再打包, 再安装到本地仓库
5、maven文件:
源文件存放位置: src/main/java
配置资源文件存放位置: src/main/resources
测试源文件存放位置:src/test/java
测试配置资源文件存放位置: src/test/resources
四、简单程序Hello 
1、创建项目
new --->other--->maven-->Mavern Project--->next-->next(quickstart)(webapp是web项目)---->下图所示






2、编写程序:




3、Junit需要导包而maven不用导包直接在pom.xml中配置即可
<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>com.yc.maven</groupId>
  <artifactId>maven-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


  <name>maven-test</name>
  <url>http://maven.apache.org</url>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <dependencies>
   <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
</dependency>

  </dependencies>
</project>
这是在私服nexus/中junit的配置中的,这样找更精确不易错
4、编译运行
右键-->run as ---->mavern clean---> 也顺便测试mvn是否可用
Mavern build -->输入compile
Mavern test
以下结果为成功,出错找原因

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值