1. 手动安装 jar 包

该方式通过手工安装 jar 包,从而使得 pom 能够在本地依赖库里面找到它,简而言之就是自己手工完成了 maven 帮忙处理的依赖下载步骤。

1
mvn install:install-file -Dfile=mylib.jar -DgroupId=com.example -DartifactId=mylib -Dversion=1.0 -Dpackaging=jar

2. 将本地文件夹指定为 maven repo

这一方式通过将本地的某个文件夹视为一个 maven repo,当在官方 repo 无法下载指定的包时他会去本地仓库下载。

当然,这个方式对于本地文件夹的组织结构有所要求,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
yourproject
+- pom.xml
+- src
+- lib
+- com
+- example
+- mylib
+- maven-metadata.xml
+- ...
+- 1.0
+- mylib-1.0.jar
+- mylib-1.0.pom
+- ...

他要求必须以 groupId/artifactId/version/jarname-version.jar 的方式存储文件,从而让 maven 可以通过 pom.xml 文件来寻找指定 jar 包进行下载(即最后这个 jar 包会被下载,如同从远程仓库下载那样)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<repositories>
<!--other repositories if any-->
<repository>
<id>project.local</id>
<name>project</name>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>

<dependency>
<groupId>com.example</groupId>
<artifactId>mylib</artifactId>
<version>1.0</version>
</dependency>

这一方式可能会遭遇以下问题

  • 关于验证策略的警告
  • 因名称不规范的问题导致无法安装
    • jar 包的名字中 jarname 与 version 并不以-分割
    • groupId 试图以 com.example 作为文件夹名而非嵌套结构

关于名称不规范的问题,可以使用 maven install 的方式去解决它

1
mvn install:install-file -Dfile=jar_path -DgroupId=group_id -DartifactId=artifact_id -Dversion=jar_version -Dpackaging=jar -DlocalRepositoryPath=project_basedir/lib

它将把一个依赖安装到你指定的文件夹(本地仓库)中,并按规范的方式进行命名,而无需担心名称不规范的问题

3. 使用 system 的 scope

1
2
3
4
5
6
7
<dependency>
<groupId>com.example</groupId>
<artifactId>mylib</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/mylib-1.0.jar</systemPath>
</dependency>

这一方式可能会遭遇以下问题

  • maven 在打包时会发出警告
  • 若不将 jar 包放置于src文件夹内,在打包时不会被打包,故而运行时将找不到该 jar 包从而发生异常, 可通过以下方式补偿
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>shade</goal>
    </goals>
    <configuration>
    <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <finalName>xxx-jar-with-dependencies</finalName>
    </configuration>
    </execution>
    </executions>
    </plugin>
    </plugins>
    <resources>
    <resource>
    <targetPath>lib/</targetPath>
    <directory>lib/</directory>
    <includes>
    <include>\*\*/mylib.jar</include>
    </includes>
    </resource>
    </resources>
    </build>
  • 部分情形下可能失效,JDBCDriver 通过 Class.forName(“xxx.Driver”)的方式会找不到类

总结

综合来看使用手动安装的方式最为安全和稳定,但需要使用文档的方式告知其他开发者这一事项。

而使用本地 repo 的方式次之,在使用前务必确认可以正确安装避免给他人带来麻烦。

最不推荐使用 scope 为 system 的方式,maven 已经警告了该方案,并且当使用一些特殊 jar 包时可能发生问题。

Docker 源配置

源列表

name address
official https://registry.docker-cn.com(已废弃)
netease http://hub-mirror.c.163.com
ustc https://docker.mirrors.ustc.edu.cn
azure http://dockerhub.azk8s.cn
tencent https://mirror.ccs.tencentyun.com

临时使用其他源

1
docker pull mirror/username/image:tag
1
docker pull registry.docker-cn.com/library/ubuntu:16.04

长期使用

通过在 Docker 守护进程启动时传入 –registry-mirror 参数,达到无需每次拉取时指定 mirror 的目的

1
docker --registry-mirror=https://registry.docker-cn.com daemon

除此之外还可以直接修改配置文件,但因为系统的不同,具体参见 docker官方文档

npm 源配置

当前配置查看

使用 npm config list 可以查看到当前 npm 的相关配置信息,其中,registry是当前使用 npm 进行 node_module 下载安装时的源。

上述命令也可以直接使用下面的命令替代

1
npm config get registry

源的修改

可用源

name address
npm https://registry.npmjs.org/
cnpm http://r.cnpmjs.org/
taobao https://registry.npm.taobao.org/
nj https://registry.nodejitsu.com/
skimdb https://skimdb.npmjs.com/registry

临时修改

当某次模块安装出现网络问题是,可以使用以下指令应急

1
npm install module --registry third_registry

如下,使用淘宝源下载 webpack

1
npm install webpack --registry https://registry.npm.taobao.org/

全局修改

npm config set config_item 可以用来设置 npm 的配置项,若有需要则可以使用以下指令变更全局的源。

1
npm config set registry third_registry

如下,全局使用淘宝源

1
npm config set registry https://registry.npm.taobao.org/

NRM(NPM registry manager)

nrm 提供了在各类源进行快速切换的能力,并且可以测试自身网络对不同源的速度

安装

1
npm install -g nrm

使用

1
nrm use third_registry_name

如下,使用 cnpm 的源

1
nrm use cnpm

测试

1
nrm test third_registry_name

如下,测试当前网络使用 cnpm 的延迟

1
nrm use cnpm
0%