服務端
首先,爲了有專業感,我在阿里雲搞了個服務器(cent),在cent上裝個nexus,啓動它
爲了方便,管它叫myrepos.mvn.com吧
本地
我通過eclipse建了個新項目smi_web
首先我要演示如果讓smi_web通過cent上的nexus獲得依賴包,而不是通過maven中央工廠
基本做法
第一步:修改本地setting.xml文件
要加入一些內容,如下
<profiles>
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>nexusFactory</id>
<name>nexusRepository</name>
<url>http://myrepos.mvn.com/:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>
<profile>是啥呢,它是本地maven找依賴包的工廠,<profile>包含<id>和<repositories><activeProfiles>呢,每個profile要能正常使用,就需要激活,activeProfiles就負責激活profile
注:activeProfile的內容profile中id的內容
ok,這樣就好了,可以通過myrepos.mvn.com來獲得依賴jar包了
但是,如果myrepos.mvn.com掛掉了,或者別的原因導致本地不能從myrepos.mvn.com上下載依賴包,那麼本地maven還會從中央工廠下載,怎麼解決呢。下面說
升級做法, <mirrors>
依然還是setting.xml文件,加入<mirrors>
<mirrors>
<mirror>
<id>mirrorId</id>
<!-- mirrorOf裏的內容是profile-repositories-repository-id的內容,可以配置多個,用逗號間隔,一般用*表示所有工廠都走這個鏡像 -->
<mirrorOf>*</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://myrepos.mvn.com:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
加入了鏡像,那麼之前對profile的激活也就可以刪掉了,
<activeProfiles>
<!-- <activeProfile>nexusProfile</activeProfile> -->
</activeProfiles>
把這個激活註釋掉,一切都走mirrors明天接着寫
發佈操作
第一步:在nexus上操作,建立自己的工廠
進入repository,點ADD
然後加入如下內容,這是我建立的一個自定義工廠,id是SmiReleases,這是release版本,會接收version是release的包(之前已經創建好了,看下configuratio好了)
增加完之後,在add一個snapshots的版本,這是snapshots工廠,會接收version是snapshots版本的包,內容如下
第二步:在nexus上操作,爲工廠添加權限
第三步:在nexus上操作,爲Privileges添加Role
第四步:在nexus上操作,增加一個User
第五步:在本地操作,修改setting.xml
<servers>
<server>
<!-- 這個smi_release要記好了,下文pom.xml文件中會用到 -->
<id>smi_release</id>
<!-- 這個smi和smi123是上面配置nexus user中配置的 -->
<username>smi</username>
<password>smi123</password>
</server>
<server>
<!-- 這個smi_snapshots要記好了,下文pom.xml文件中會用到 -->
<id>smi_snapshots</id>
<username>smi</username>
<password>smi123</password>
</server>
</servers>
第六步:在本地操作,修改工程中的pom.xml文件
<distributionManagement>
<repository>
<!-- 這裏的smi_release與上文的那個啥,setting.xml中的<server>裏的id對應 -->
<id>smi_release</id>
<name>smi</name>
<!-- 這裏的url,就是我們之前在nexus裏創建的自己的工廠的url,這是release版本 -->
<url>http://myrepos.mvn.com:8081/nexus/content/repositories/SmiReleases/</url>
</repository>
<snapshotRepository>
<id>smi_snapshots</id>
<name>smi</name>
<!-- 這裏是snapshots版本 -->
<url>http://myrepos.mvn.com:8081/nexus/content/repositories/SmiSnapshots/</url>
</snapshotRepository>
</distributionManagement>