Maven使用私服来进行jar包下载
私服搭建(nexus远程仓库的搭建)
settings.xml配置(conf目录下的配置文件)
- 配置servers
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
nexus默认登录用户名为admin密码为admin123 也可以使用nexus中的security中的users如下图,密码默认为用户名后添加123
- 将镜像的配置注释掉(配置mirrors)
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors>
将镜像注释原因:默认是从中央仓库下载,耗时多,将镜像注释掉可以直接从私服内取到想要使用的jar,也可以配置阿里云的镜像或者远程仓库(私服)的镜像。
- 配置profiles
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:6262/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:6262/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
1、id:profile的id可以自己任意取,**时使用
2、repositories:仓库配置,一个repositories可以配置多个仓库(repository),但要保证id不重复
id,仓库id,保证不重复
url:nexus仓库组的地址即
releases:enable属性表示是否下载releases的构件
snapshots:enable属性表示是否下载snapshots的构件
3、pluginRepositories:配置插件仓库,配置与repositories相同
- 配置生效(配置activeProfiles)
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
activeProfile:值为配置的profile的id,即**该id所对应的配置
项目内pom.xml配置(项目内的配置文件)
- 在pom中添加distributionManagement配置(只能上传jar包,无法上传源代码)
<distributionManagement>
<repository>
<id>releases</id>
<name>zlrelease</name>
<url>http://localhost:6262/nexus/content/repositories/releases/</url></repository>
<snapshotRepository>
<id>snapshots</id>
<name>zlsnapshots</name>
<url>http://localhost:6262/nexus/content/repositories/snapshots/</url></snapshotRepository>
</distributionManagement>
id要和settings.xml配置文件中的servers中的配置保持一致,url是nexus中的仓库所对应的地址
- 配置maven-source-plugin(可上传源代码到私服)配置在build的plugins下
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在项目中使用时,需要将maven配置文件(在IDE的配置中选择maven的配置文件地址为自己配置好的settings.xml)选成自己配置过的文件,否则不会生效!!!