本文不设计pom.xml的项目配置引用 主要是记录一下公共jar的管理配置apache
只须要修改一下maven setting配置maven
<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"> <localRepository>e:\maven</localRepository> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> -->
不须要配置中央仓库 由于在 maven 解压路径lib中 E:\xxxx\lib\maven-model-builder-3.3.9.jar解压打卡pom.xml 中已经默认配置好中央仓库ui
<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>
当存在Nexus 咱们本身的项目就要考虑首先访问本身公司的Nexus (本地毕竟比远程仓库下载快),在项目中就要配置本地Nexus地址this
一种能够在单个项目pom.xml中配置以下添加便可url
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 添加nexus repository --> <repositories> <repository> <id>nexus</id> <name>Nexus Repository</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> </repository> </repositories> <dependencies>
第二种 在maven setting.xml配置文件中配置设计
<profiles> <profile> <id>nexus</id> <repositories> <repository> <id>Releases</id> <name>my-nexus-repository</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <profile> </profiles> <!-- 记得启动--> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>
每个profile 能够配置一个仓库地址(多个仓库 做用本身查)code
当本地Nexus中没有相应的jar存在会直接去远程仓库寻找,那么当本地Nexus没有下载过相应jar时 ,仍是要去远程仓库寻找 。server
那么问题是在哪去寻找 (注意:**不是再去maven lib下 E:\xxxx\lib\maven-model-builder-3.3.9.jar 查 而xml
是在Nexus本身那几个仓库中配置的远程仓库去找** ) 在Nexus中有一个默认的central仓库如图:图片
本文再也不介绍Nexus几个仓库的具体做用 (本身单独去查一下就能够)。
可是当咱们的Nexus关闭了呢 本身的项目仍是会去 经过maven lib下 E:\xxxx\lib\maven-model-builder- 3.3.9.jar 去查中央仓库因此要设置 只能经过咱们的Nexus去访问中央仓库 ,当个人仓库关了 就不能去访问中央仓库获取jar包。(这样也是为了更灵活统一 毕竟jar包中的中央仓库 是不可变得 Nexus访问中央仓库的地址是能够配置的)
解决上面的问题须要配置镜像(mirror)代码以下
<mirrors> <mirror> <id>Releases</id> <!--*表明全部须要访问的 仓库都会镜像到本地12.0.0.1 8081仓库去寻找 也就是远程中央仓库 你只能经过个人本地仓库去访问 --> <mirrorOf>*</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> </mirror> <mirror> <id>Snapshots</id> <mirrorOf>*</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://127.0.0.1:8081/nexus/content/repositories/central/</url> </mirror> </mirrors>
一样能够配置多个mirror ,mirrorOf表明哪些要访问的仓库地址(profiles 下的profile)须要镜像必须走本身的 Nexus地址。 设置成星号表明全部。
这种状况下在咱们本地的maven 配置的profile和maven lib下 E:\xxxx\lib\maven-model-builder-3.3.9.jar
pom.xml中的profile 地址都会转到mirror
同时注意一下 在maven的 那个jar包中的配置 snapshots和enabled都是关闭的false
<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>
虽然访问全部的仓库都是转到mirror 可是他必须先去找到 下面的profile 地址和maven jar中的中央工厂地址才回去转到 mirror 中,注意:镜像过去的只是地址 配置中的其余配置仍是按照原来的模式去设置
因此在咱们要访问中央仓库时他根据上面设置的false去设置 咱们应该重写jar包中的中央工厂的配置。
将releases和snapshots设置为true
<profiles> <profile> <id>central</id> <repositories> <repository> <id>Releases</id> <name>my-nexus-repository</name> <url>http://mvnrepository.com/</url> <!-- 下面两个true 表示能够下载 releases和 snapshots包(具体也不清楚是什么)--> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile> </profiles> 开启是必须的 <activeProfiles> <activeProfile>central</activeProfile> </activeProfiles>
总结:最后说的好像有点乱,核心配置看setting.xml吧 一共三四段
第一段 本身本地jar存储: <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"> <localRepository>e:\maven</localRepository> 第二段Nexus用户: <servers> <server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey> <passphrase>optional; leave empty if not used.</passphrase> </server> --> <server> <id>Releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>Snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers> 第三段mirror镜像配置: <mirror> <id>Releases</id> <!--*表明全部须要访问的 仓库都会镜像到本地12.0.0.1 8081仓库去寻找 也就是远程中央仓库 你只能经过个人本地仓库去访问 --> <mirrorOf>*</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> </mirror> <mirror> <id>Snapshots</id> <mirrorOf>*</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://127.0.0.1:8081/nexus/content/repositories/central/</url> </mirror> </mirrors>
第四段 须要访问的仓库:
<profiles> <profile> <id>**nexus**</id> <repositories> <repository> <id>Releases</id> <name>my-nexus-repository</name> <url>http://mvnrepository.com/</url> <!-- 下面两个true 表示能够下载 releases和 snapshots包(具体也不清楚是什么)--> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>Snapshots</id> <name>my-nexus-repository</name> <url>http://mvnrepository.com/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>**nexus**</activeProfile> </activeProfiles>