发表于: 2017-08-12 23:10:09
1 981
一、今日完成
1)学习《Maven实战》,系统了解maven工具。
i.安装maven3.5.0配置环境变量,之前不明白为什会先新建M2_HOME变量,再将%M2_HOME%\bin添加到Path变量中,何必多此一举?今天学到因为maven更新频繁,只需要下载新的安装文件,解压至本地,然后更新M2_HOME环境变量即可。同理,配置JDK、tomcat等环境变量也可以这样做,避免修改Path变量疏忽出错,导致系统异常。
ii.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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spiece</groupId>
<artifactId>wolf</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>wolf Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>wolf</finalName>
</build>
</project>
代码第一行是XML头,指定了文档的版本和编码方式。紧接着是project元素,project元素是所有pom.xml的根元素,声明了POM相关的命名空间及xsd元素。
modelVersion指定了当前POM模型版本。
groupId:定义当前maven项目隶属的实际项目;
artifactId:该元素定义实际项目中的一个maven项目(模块),常用实际项目名称作为它的前缀;
version:定义maven项目当前的版本,SNAPSHOT意为快照,说明项目处于开发中,是不稳定版本;
packaging:定义maven项目的打包方式,打包方式(jar、war)与所生成的构件的文件扩展名对应,而且也会影响构建的生命周期
(groupId、artifactId、version、packaging和classifier是maven坐标的元素,是管理项目依赖的底层基础)。
name:声明了一个更易于理解的项目名称;
这里添加了dependencies元素,groupId、artifactId和version是依赖的基本坐标,有了这段声明,maven就能够自动下载相应的jar包;scope为依赖范围,若依赖范围为test则表示该依赖只对测试有效;maven的依赖范围有compile、test、provided、runtime和system,用来控制依赖与class path的关系。
iii.使用maven开发,对于如何寻找需要的依赖,可以使用仓库搜索服务来根据关键词得到maven坐标,推荐https://mvnrepository.com/和https://repository.sonatype.org/#welcome。
iv.配置中央仓库的镜像
配置阿里云镜像仓库
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
配置其他镜像仓库
<mirror>
<id>maven.net.cn</id>
<name>one of the central mirrors in China</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
可以避免因为GWF以及地理原因造成访问中央仓库缓慢。由于镜像仓库屏蔽了被镜像仓库,当镜像仓库不稳定或停止服务,maven将无法下载构件。
2)关于maven生命周期和插件在之前的日报已经总结这里不再赘述。
3)关于maven的聚合与继承特性因为在目前的任务中未涉及,所以没有总结整理。
4)这个是maven的标准目录结构,一般使用maven开发,应该尽量贴近它,遵循maven的“Convention Over Configuration”原则。
src/main/java | Application/Library sources |
src/main/resources | Application/Library resources |
src/main/filters | Resource filter files |
src/main/webapp | Web application sources |
src/test/java | Test sources |
src/test/resources | Test resources |
src/test/filters | Test resource filter files |
src/it | Integration Tests (primarily for plugins) |
src/assembly | Assembly descriptors |
src/site | Site |
LICENSE.txt | Project's license |
NOTICE.txt | Notices and attributions required by libraries that the project depends on |
README.txt | Project's readme |
apache做出了如下声明:
1.There are just two subdirectories of this structure: src and target. The only other directories that would be expected here are metadata like CVS, .git or .svn, and any subprojects in a multiproject build (each of which would be laid out as above).
2.The target directory is used to house all output of the build.
3.The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.
4.Within artifact producing source directories (ie. main and test), there is one directory for the language java (under which the normal package hierarchy exists), and one for resources (the structure which is copied to the target classpath given the default resource definition).
5.If there are other contributing sources to the artifact build, they would be under other subdirectories: for example src/main/antlr would contain Antlr grammar definition files.
二、明日计划
整理task1,争取在下周二以前提交task1。
三、遇到的问题
新建maven项目,project name的值即项目的名称,不得与既有项目重复,即使在存储路径下删除了原同名项目,否则IDEA会报错:
Maven:Failed to create a Maven project ‘…pom.xml’ already exists in VFS
四、今日收获
把当前需要了解的maven相关知识基本掌握,算是拿下了这个知识点,至于如何使用Nexus创建私服,留在以后有闲时练习。
评论