概覽
構(gòu)建工具發(fā)展史:Make -> Ant -> Maven -> Gradle
Eclipse + Ant組合的時候,引入庫都是下載jar包或aar包放到lib目錄下,然后添加引用。!!!不能實時更新。
Android Studio + Gradle組合中g(shù)radle中提供了可以從遠端拉取jar包和aar包引入本地。
Maven倉庫分為兩類:本地倉庫和遠程倉庫。
- 本地倉庫就是本地存放jar包的位置,Maven的本地倉庫在最開始的時候并不會創(chuàng)建,而是第一次啟動Maven時,在當前用戶的文件夾下建立一個.m2文件,其中存放Maven本地倉庫的所有jar包。
- 遠程倉庫又分為:
中央倉庫、Nexus私服和其他公共倉庫。
安裝與配置
http://maven.apache.org/download.cgi
配置環(huán)境變量:
- MAVEN_HOME——安裝路徑
- PATH——%MAVEN_HOME%\bin;
- 【mvn -v】命令行輸入,查看配置是否成功。
配置Maven:
- 打開maven的安裝目錄,找到文件
\conf\settings.xml - 標簽localRepository,配置本地倉庫存儲路徑。
- 標簽mirror,配置Maven鏡像。
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>E:\mavenRepository</localRepository>
Maven鏡像
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>repo2 maven</name>
<url>http://repo2.maven.org/maven2</url>
</mirror>
POM
概覽
http://maven.apache.org/pom.html
POM stands for "Project Object Model".It is an XML representation of a Maven project held in a file named 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">
<!--當前僅有的可以被Maven 2 & 3同時支持的POM版本-->
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<!--The current core packaging values are: pom, jar, maven-plugin, ejb, war, ear, rar.-->
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
The Super POM
Maven有一個超級pom,所有的pom文件均繼承此文件。
路徑:
lib\maven-model-builder-3.5.4.jar\org\apache\maven\model\pom-4.0.0.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!-- START SNIPPET: superpom -->
<project>
<modelVersion>4.0.0</modelVersion>
<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>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
<!-- END SNIPPET: superpom -->
Maven服務(wù)源
-
JCenter
托管在Bintray上面的Maven倉庫。 -
Maven Central
托管在Sonatype上面的Maven倉庫。 - JitPack
JCenter優(yōu)點:
- 基于CDN分布函數(shù)庫,JCenter提供了更快的下載速度。
- JCenter是最大的Java倉庫,可以說Maven Central是JCenter的一個子集,托管在Maven Central中的函數(shù)庫,幾乎都托管在JCenter上面。
- 上傳簡單,Bintray的用戶界面對用戶友好。
獲取函數(shù)庫的原理
完整的函數(shù)庫依賴字符串包含三部分:GROUP_ID:ARTIFACT_ID:VERSION
Gradle根據(jù)依賴配置,向Maven Repository服務(wù)器查詢是否存在該版本的函數(shù)庫,如果存在,則會根據(jù)服務(wù)器類型拼接下載請求url。
-
JCenter:
http://jcenter.bintray.com/... -
Maven Central:
https://oss.sonatype.org/content/repositories/releases...
Maven私服
私服是架設(shè)在局域網(wǎng)內(nèi)的遠程倉庫,目的是代理遠程倉庫及部署第三方構(gòu)件。
優(yōu)點:
- 節(jié)省外網(wǎng)帶寬:減少大量對遠程倉庫的重復(fù)請求
- 加速maven構(gòu)建
- 部署第三方構(gòu)件:構(gòu)件無法從遠程倉庫獲取,如公司內(nèi)部生成的私有構(gòu)件。
Sonatype Nexus
- https://www.sonatype.com/download-oss-sonatype
- 自定義參數(shù)etc\nexus-default.properties
- 啟動nexus
默認端口:8081
http://127.0.0.1:8081/
http://localhost:8081/
默認配置文件:C:\swTools\nexus-3.13.0-01\etc\nexus-default.properties
默認登錄:admin/admin123
常用命令:
【nexus.exe/install】安裝nexus為系統(tǒng)服務(wù)
【nexus.exe/uninstall】卸載nexus為系統(tǒng)服務(wù)
【nexus.exe/start】啟動nexus服務(wù)
【nexus.exe/stop】停止nexus服務(wù)
【nexus.exe/run】啟動服務(wù)