一学赛、Maven的pom.xml
Maven是基于項目對象模型的概念運行的蚊逢。所有Maven的項目都有一個pom.xml用來管理項目的依賴以及項目的編譯等功能。
- dependencies元素
,此元素包含多個項目依賴盒刚,每個依賴需要用
元素來配置庭砍。
2.dependency元素
內(nèi)部通過groupId,artifactId以及version確定唯一的依賴拾并。groupId是組織的唯一標識揍堰,artifactId是項目的唯一標識,version是項目的版本嗅义。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
3.變量定義
可定義變量在中使用屏歹。比如上例中的元素中的變量就是pom.xml開始的元素中所定義的。
<properties>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
4.編譯插件
Maven 提供了編譯插件之碗,可在編譯插件中涉及java的編譯級別蝙眶。
二、Maven倉庫
Maven倉庫用來存放Maven管理的所有Jar包褪那。當項目需要jar包的時候通過坐標去倉庫尋找jar包幽纷。分為:本地倉庫、私服公司的倉庫)和中央倉庫博敬。
當項目編譯時友浸,Maven首先從本地倉庫中尋找項目所需的Jar包,若本地倉庫沒有偏窝,再到Maven的私服或中央倉庫下載所需Jar包收恢。
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!--阿里云Maven倉庫-->
<repositories>
<repository>
<id>aliyun-repos</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<pluginRepositories>
<pluginRepository>
<id>tomcat8-plugin</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</pluginRepository>
</pluginRepositories>
三、傳遞依賴與排除依賴
傳遞依賴:如果我們的項目引用了一個Jar包祭往,而該Jar包又引用了其他Jar包伦意,那么在默認情況下項目編譯時,Maven會把直接引用和間接引用的Jar包都下載到本地硼补。
排除依賴:如果不想引用間接引用的Jar包驮肉,那么可以在pom.xml中做exclusion配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除內(nèi)置容器,排除內(nèi)置容器導出成war包可以讓外部容器運行spring-boot項目-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
四已骇、生成項目站點報告
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
<configuration>
<locales>zh_CN</locales>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
五离钝、常見問題
-
亂碼解決:
MAVEN_OPTS:-Xms128m -Xmx512m -Dfile.encoding=UTF-8
JAVA_TOOL_OPTIONS:-Dfile.encoding=UTF-8
- 解決 IDEA Create Maven Project 卡在 Downloading plugins for
maven每次進行創(chuàng)建的時候回去網(wǎng)上下載artheType-catalog.xml