Maven安装及下载无法加载的依赖 添加Jar包(注册Jar)

搜索Jar的网站

依赖搜索

https://central.sonatype.com/

https://nowjava.com/jar/

类搜索

https://www.classnotfound.com.cn/

Maven镜像

可用镜像

阿里云的镜像站(首推,新站,速度暴快)

1
2
3
4
5
6
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

leancloud

1
2
3
4
5
6
<mirror>
<id>nexus-leancloud</id>
<name>Nexus leancloud</name>
<url>http://mvn.leancloud.cn/nexus/content/repositories/public</url>
<mirrorOf>central</mirrorOf>
</mirror>

ibiblio

1
2
3
4
5
6
<mirror>
<id>ibiblio.org</id>
<name>ibiblio Mirror of http://repo1.maven.org/maven2/</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>

JBoss的仓库

1
2
3
4
5
6
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>

使用方式

修改~/.m2文件夹下的settings.xml文件,

<mirrors>标签下加入上述内容即可。如下:

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
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央仓库2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
</settings>

Maven仓库

阿里云

1
2
3
4
5
6
7
8
9
10
11
12
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>

sonatype

1
2
3
4
5
6
7
8
9
10
11
<repository>
<id>oss-sonatype-snapshots</id>
<name>OSS Sonatype Snapshots Repository</name>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

sun

1
2
3
4
5
6
7
8
9
10
11
<repository>
<id>sun</id>
<name>sun</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

alfresco

1
2
3
4
5
6
7
8
9
10
11
<repository>
<id>alfresco.public</id>
<name>Alfresco Public Repository</name>
<url>https://maven.alfresco.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>

spring

1
2
3
4
5
<repository>
<id>springsource-repo</id>
<name>SpringSource Repository</name>
<url>http://repo.springsource.org/release</url>
</repository>

Maven安装

使用IDEA自带的

查找方式

在IDEA的安装的根目录搜索mvn

打开所在目录

D:\Program Files\JetBrains\IntelliJ IDEA 2022.2.4\plugins\maven\lib\maven3\bin

添加这个到环境变量的Path中。

设置依赖的下载位置

把当前用户下的.m2目录复制到新的路径

这里复制到了D:\Tools\.m2

找到上级目录下的conf

D:\Program Files\JetBrains\IntelliJ IDEA 2022.2.4\plugins\maven\lib\maven3\conf

打开settings.xml

把其中的下面的这一行解除注释,并设置新的路径

1
<localRepository>D:\Tools\.m2\repository</localRepository>

官方下载

Maven下载地址:http://maven.apache.org/download.cgi?Preferred=https%3A%2F%2Fmirrors.tuna.tsinghua.edu.cn%2Fapache%2F

下载好了之后解压到相应的目录,这里我的路径是 /Library/apache-maven-3.6.3

1
2
touch ~/.bash_profile
open ~/.bash_profile

添加

1
2
3
4
export M2_HOME=/Library/apache-maven-3.6.3
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home

立即生效

1
source ~/.bash_profile

Maven无法下载

比如我要下载的包为tio-websocket-server

到中央仓库 地址 http://mvnrepository.com/

搜索到的依赖为

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.t-io/tio-websocket-server -->
<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-websocket-server</artifactId>
<version>3.7.1.v20210106-RELEASE</version>
</dependency>

对应的地址为

https://mvnrepository.com/artifact/org.t-io/tio-websocket-server/3.7.1.v20210106-RELEASE

输入命令 需要修改的 url、groupId、artifactId、version

mvn dependency:get -DremoteRepositories=url -DgroupId=groupId -DartifactId=artifactId -Dversion=version

mvn dependency:get -DremoteRepositories=https://mvnrepository.com/artifact/org.t-io/tio-websocket-server -DgroupId=org.t-io -DartifactId=tio-websocket-server -Dversion=3.7.1.v20210106-RELEASE

Maven中添加Jar

常规安装

image-20221024114503196

我这里直接使用的Idea中自带的maven

D:\Program Files\JetBrains\IntelliJ IDEA 2021.2\plugins\maven\lib\maven3\bin

把Maven的路径添加到环境变量中

maven中添加

1
2
3
mvn install:install-file -Dfile="D:\Tools\BigData\datax\lib\datax-core-0.0.1-SNAPSHOT.jar" -DgroupId="com.datax" -DartifactId="datax-core" -Dversion="0.0.1" -Dpackaging=jar

mvn install:install-file -Dfile="D:\Tools\BigData\datax\lib\datax-common-0.0.1-SNAPSHOT.jar" -DgroupId="com.datax" -DartifactId="datax-common" -Dversion="0.0.1" -Dpackaging=jar

项目中引用

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.datax</groupId>
<artifactId>datax-core</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.datax</groupId>
<artifactId>datax-common</artifactId>
<version>0.0.1</version>
</dependency>

卸载依赖

卸载单个

1
mvn dependency:purge-local-repository -DmanualInclude="com.datax:datax-core"

卸载多个

1
mvn dependency:purge-local-repository -DmanualInclude="com.datax:datax-core,com.datax:datax-common"

添加Oracle

1
mvn install:install-file -Dfile="D:\Tools\Jars\ojdbc6-11.2.0.4.0-atlassian-hosted.jar" -DgroupId="com.oracle" -DartifactId="ojdbc6" -Dversion="11.2.0.4.0-atlassian-hosted" -Dpackaging=jar

项目中添加

1
2
3
4
5
6
<!--Oracle-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4.0-atlassian-hosted</version>
</dependency>

hive-jdbc

2.1.0-standalone

1
mvn install:install-file -Dfile="D:\Tools\dbeaver\jar\hive-jdbc-2.1.0-standalone.jar" -DgroupId="org.apache.hive" -DartifactId="hive-jdbc" -Dversion="2.1.0-standalone" -Dpackaging=jar

项目中添加

1
2
3
4
5
6
<!--Hive-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.0-standalone</version>
</dependency>

3.1.1-standalone

1
mvn install:install-file -Dfile="D:\Tools\dbeaver\jar\hive-jdbc-3.1.1-standalone.jar" -DgroupId="org.apache.hive" -DartifactId="hive-jdbc" -Dversion="3.1.1-standalone" -Dpackaging=jar

项目中添加

1
2
3
4
5
6
<!--Hive-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.1-standalone</version>
</dependency>

自定义Jar

1
mvn install:install-file -Dfile="D:\Jars\Bigdata\yxdp-algorithm-1.0.0.jar" -DgroupId="com.yxkj" -DartifactId="yxdp-algorithm" -Dversion="1.0.0" -Dpackaging=jar

项目下的依赖就可以改为

1
2
3
4
5
<dependency>
<groupId>com.yxkj</groupId>
<artifactId>yxdp-algorithm</artifactId>
<version>1.0.0</version>
</dependency>

带classifier的安装

1
2
3
4
5
6
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.0</version>
<classifier>standalone</classifier>
</dependency>

安装

1
mvn install:install-file -DgroupId="org.apache.hive" -DartifactId="hive-jdbc" -Dversion="2.1.0" -Dclassifier="standalone" -Dpackaging=jar -Dfile="D:\Tools\dbeaver\jar\hive-jdbc-2.1.0-standalone.jar"