之前做项目就开始接触maven但是一直没有好好的总结下,会用简单的一些东西。最近看了一下maven的其他东西又学习到了其它我不知道的东西,这篇博客首先从简单介绍起。
maven是一个异常强大的构建工具,能够帮助我们有效的管理jar包,帮助我们自动化构建过程,帮助我们对项目进行清理、编译、测试、生成报告、打包和部署的一个工具。有了maven我们只需要在配置文件中进行配置,就可以自动的下载固定版本的jar包而不需要我们手动的一个个的引入。如果引用一个jar,而这个jar又依赖其他的jar拿我们必须把其他的这几个jar全部引入才可以,容易出现错误,也很繁琐,有了maven就只需要你在pom.xml中声明对这个jar的依赖就可以了。
修改maven目录下面的confg的setting.xml文件,默认路径是在用户的文档下面的.m文件下,需要将其修改为自己电脑 的一个不会被删除的路径。具体配置如下:
<!-- 设置本地仓库路径 --> <localRepository>D:\maven\repository</localRepository> <!-- 设置发布 jar 包时的用户名及密码 --> <servers> <server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers> <!-- 设置 maven 的远程仓库为 nexus --> <mirrors> <mirror> <id>mirrorId</id> <mirrorOf>*</mirrorOf> <name>mirrorOfrepository</name> <!--<url>http://my.repository.com/repo/path</url>--> <url>http://192.168.XX.XXX:8081/nexus/content/groups/public</url> </mirror> </mirrors> <!-- 可选配置,用来覆盖原先 central 的路径等 --> <profiles> <profile> <id>centralprofile</id> <repositories> <repository> <id>central</id> <name>Central</name> <!-- 该 url 没有意义,可以随便写,但必须有。 --> <url>http://*</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>local private nexus</name> <url>http://repo.maven.apache.org/maven2</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <!-- 激活 central --> <activeProfiles> <activeProfile>centralprofile</activeProfile> </activeProfiles> <!-- 配置eclipse插件 --> <pluginGroups> <pluginGroup>org.mortbay.jetty</pluginGroup> <pluginGroup>org.codehaus.cargo</pluginGroup> </pluginGroups>
最后填写相应名称就可以。
源代码应该放置在src/main/java中
源代码的资源文件应该放置在src/main/resources文件中
测试代码应该放置到src/test/java中
测试代码的资源文件放置在src/test/resources文件夹中
一些jar包的配置信息在开发的时候就写入了pom信息了,配置信息在maven的中央仓库中有:http://mvnrepository.com/在搜索框中输入要查询的jar类包,如hibernate,会看到好多版本的jar包点击查看粘贴里面的信息到pom.xml中,如果本地仓库没有这个版本的jar会到中央仓库去下载。我们会看到对应版本的jar已经包含在项目中。
ps:maven坐标:
在数学里面坐标可以唯一确定一个点,在maven中引入坐标的概念也是为了确定具体的jar包。
groupId:表示项目的名称
artifactId;用来表示项目的模块名称,议案一使用项目的名称-模块名称来表示
version:表示这个项目版本名称
maven的初步小总结,比较重要的知识还有依赖,仓库以及插件,有机会继续分享。
