

新闻资讯
常见问题 Maven:是Apache提供的免费开源的项目管理工具。它提供了一个项目对象模型(pom.xml)、一个依赖管理系统(根据坐标,帮我们引入依赖)、一套项目生命周期(帮我们方便的进行项目构建)、一组标准集合(Maven工程的目录结构规范,坐标规范)、和一个插件管理系统(提供了进行项目构建的命令)。

c3p0, com.jdc3p00.9.1.2, 1.0-SNAPSHOT, 5.2.0-RELEASE 1.本地仓库:本地缓存jar包的文件夹
2.远程仓库(*):一些企业、组织、社区搭建的仓库服务
3.中央仓库
maven |--bin Maven的可执行命令文件夹 |--boot Maven的引导程序(类加载器) |--conf 配置文件夹 |--lib Maven的核心程序代码
1.Maven是免安装的,直接解压
2.注意检查JDK环境变量是否正确
3.配置Maven环境变量:
1.验证Maven配置
配置步骤:
扩展:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
仓库类型
maven的安装:
配置本地仓库:
一个项目要想使用Maven进行管理,那么这个项目的目录结构就必须要符合Maven的要求:
maven项目 |--pom.xml 项目对象模型文件,一个Maven项目必定有pom.xml文件 |--src |--main 项目的主体程序代码目录 | |--java 项目的Java程序代码放在这文件夹里 | |--resources 项目的配置文件放在这里 | |--webapp 项目的web资源放在这里,html,css,js,jsp,音频,图片,视频等等 | |--WEB-INF | |--其它web资源 |--test 项目的单元测试代码目录 |--java 项目的单元测试代码 |--resources 项目单元测试需要的配置文件
src\main\java:放主体程序代码的java文件
src\main\resources:放主体程序代码的配置文件
src\main\webapp:放主体程序代码的web资源:html、css、js、jsp等等
Maven命令执行的规律:
clean: clean compile: compile test: compile=>test package: compile=>test=>package install: compile=>test=>package=>install 人的生命周期:婴儿=>儿童=>少年=>青年=>中年=>老年
Maven有三套生命周期
cleancompile->test->package->install->deploy site。生成Maven项目的描述信息文档常用命令:
mvn clean:清理。删除掉target目录mvn compile:编译。把主体程序代码进行编译,放到target目录里mvn test:测试。编译并执行单元测试代码mvn package:打包。Java项目打成jar包,web项目打包war包mvn install:安装。把程序包安装到本地仓库,安装位置是坐标对应的文件夹Maven的生命周期
cleancompile=>test=>package=>install=>deploysiteDarchetypeCatalog=internal1.创建Module,选择Maven类型,选择骨架org.apache.maven.archetypes:maven-archetype-quickstart
2.设置项目的坐标
3.再继续步骤,跟以前的一样:设置module的名称和位置
注意:不要和已有的其它module重名或者重位置
4.创建后,如果弹窗Maven projects need to be imported,就选择 启动自动导入。
作用是:如果pom.xml文件有变更,Maven会自动读取,立即生效;否则就要手动导入
5.如果文件夹缺失,就创建补全,然后刷新
如果上边Add as module to和Parent是有配置的,那么要在右边全部选择none
如果项目中需要使用jar包,只需要把jar包的坐标配置到项目的pom.xml中即可。在pom.xml的<dependencies>标签里增加以下内容:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>依赖范围</scope>
</dependency>
可以在pom.xml里,输入
dep,会自动补充坐标模板,我们填充坐标值即可可以使用
alt+insert,会弹出依赖导入的界面,可以从本地仓库里搜索依赖如果不知道依赖的坐标,可以从中央仓库搜索坐标:https://mvnrepository.com/
如果项目中引入的jar包过多,可能会出现jar包冲突,我们可能设置jar包不同的依赖范围来处理jar包冲突。
Maven的依赖范围有:
compile:默认的依赖范围,全范围有效。test:单元测试有效。src/test里有效,src/main里无效provided:编译时有效,运行时使用其它地方提供的jar包。runtime:运行时有效,编译时无效。比如:数据库驱动包system:引入仓库之外的本地jar包。少用实际开发中特殊的jar包
Junit:依赖范围通常设置为testservlet-api, jsp-api:依赖范围通常设置为provided数据库驱动包:依赖范围可以是默认的,也可以是runtimeompile 依赖范围:
plugins标签中,通过plugin标签引入maven的功能插件。<plugins>标签,放在pom.xml的<build>标签内部
<!--jdk编译插件-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
<plugins>标签,放在pom.xml的<build>标签内部
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>82</port>
<!-- 项目路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
1.解压nexus-2.12.0-01-bundle.zip。注意:解压路径里不要有中文、空格、特殊字符
2.以管理员方式打开cmd,切换进入解压目录的bin文件夹里,运行命令:nexus.bat install
打开浏览器,输入地址:http://localhost:8081/nexus
conf/nexus.properties文件,修改端口号,重启nexus服务后生效登录“Log in”,输入帐号和密码进行登录(帐号admin,密码admin123)
hosted:宿主仓库, 部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分, Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库proxy:代理仓库, 用于代理远程的公共仓库,如 maven 中央仓库,用户连接*,*自动去中央仓库下载 jar 包或者插件。group:仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。virtual:虚拟仓库,兼容 Maven1 版本的 jar 或者插件使用场景
配置
第一步:在用户自己的Maven软件里修改conf/settings.xml:
servers标签里,配置*的帐号和密码
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
第二步:在用户需要发布的*的项目pom.xml里,配置*仓库的地址。
<distributionManagement>
<repository>
<!-- 注意:id的值,是刚刚在settings.xml中配置的server的id -->
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<!-- 注意:id的值,是刚刚在settings.xml中配置的server的id -->
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
测试
1.启动nexus服务(如果已启动,就不需要做这一步)
2.在项目里执行Maven的deploy命令:
使用场景
好处是:
配置
第一步:在settings.xml的profiles标签下配置:
注:由于settings.xml中没有repositories标签,所以要使用profile标签来配置仓库
<profile>
<!--profile 的 id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库 id, repositories 可以配置多个仓库,保证 id 不重复-->
<id>nexus</id>
<!--仓库地址,即 nexus 仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载 releases 构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载 snapshots 构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库, maven 的运行依赖插件,也需要从*下载插件 -->
<pluginRepository>
<!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
第二步:激活profile定义的仓库
激活后,配置的仓库才会生效
<activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles>
测试 创建Maven应用testdownload,配置依赖
<dependency>
<groupId>com.baidu</groupId>
<artifactId>commons-utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
编译项目,查看日志会出现:
查看本地仓库里,会有从*下载的jar包
把项目发布到*:
从*里下载jar:
cmd命令示例:mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar
settings.xml中配置*的server信息
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
DgroupId和DartifactId构成了 jar 包的坐标,项目就是依靠这两个属性定位。可以自己起名Dfile 表示需要上传的 jar 包的位置。Durl *上仓库的位置,打开 nexus——>repositories 菜单,可以看到该路径。DrepositoryId 服务器的表示 id,在 nexus 的 configuration 可以看到。Dversion 表示版本信息。 上传成功后,在 nexus 界面点击 3rd party 仓库可以看到这包。
通过添加注解的方式,帮我们生成一些方法,提高开发人员的开发效率。
例如:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
安装插件
编译时出错,可能是没有enable注解处理器。Annotation Processors > Enable annotation processing。设置完成之后程序正常运行。
目标
@Data
@Getter/@Setter
@ToString
@NoArgsConstructor, @AllArgsConstructor
@Data
@Data
public class User implements Serializable{
private Integer id;
private String username;
private String password;
private String address;
private String nickname;
private String gender;
private String email;
private String status;
}
@Getter/@Setter
public class User implements Serializable{
@Setter
@Getter
private Integer id;
private String username;
private String password;
private String address;
private String nickname;
private String gender;
private String email;
private String status;
}
@ToString
toString()方法toString方法会输出类名、所有属性(会按照属性定义顺序),用逗号来分割。exclude属性指定忽略字段不输出,
@ToString(exclude = {"id"})
public class User implements Serializable{
private Integer id;
private String username;
private String password;
private String address;
private String nickname;
private String gender;
private String email;
private String status;
}
@xxxConstructor
@NoArgsConstructor: 无参构造器
@NoArgsConstructor
public class User implements Serializable{
private Integer id;
private String username;
private String password;
private String address;
private String nickname;
private String gender;
private String email;
private String status;
}
@AllArgsConstructor: 全参构造器
@AllArgsConstructor
public class User implements Serializable{
private Integer id;
private String username;
private String password;
private String address;
private String nickname;
private String gender;
private String email;
private String status;
}
*.lastUpdated文件,是下载依赖出错后遗留的不完整数据第一步:配置阿里云的仓库镜像
打开Maven软件里conf/settings.xml
在<mirrors>标签中添加如下内容:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
第二步:删除本地仓库里的缓存文件
.lastUpdated结尾的文件,是下载依赖失败后遗留的文件,需要清除掉1.在本地仓库文件夹里搜索*.lastUpdated
2.把搜索到的所有文件全部删除
第三步:重新添加依赖
pom.xml中,删除原有的所有依赖本篇文章就到这里了,希望可以帮到你,也希望您能多多关注的更多内容!