把自己的項目發布到maven倉庫並在maven和gradle中開始使用
上一條博客中提到的日志打印項目總算是維護的差不多了, 不過現在使用它還是打成jar包放到其他項目內, 所以決定把項目傳到maven倉庫內, 使用時只需要配置一下即可了
我使用的是阿里雲的maven倉庫服務, 如何購買阿里雲倉庫這里就不多說了, 去阿里雲上找很容易找到
1. 修改maven配置文件conf/settings.xml
首先添加服務配置項, 相當於令牌, 連接遠程倉庫
<servers> <server> <id>rdc-releases</id> <username>阿里倉庫賬號</username> <password>密碼</password> </server> <server> <id>rdc-snapshots</id> <username>阿里倉庫賬號</username> <password>密碼</password> </server> </servers>
再添加倉庫地址的配置:
<mirrors> <mirror> <id>mirror</id> <mirrorOf>!rdc-releases,!rdc-snapshots</mirrorOf> <name>mirror</name> <url>https://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>
然后是私有倉庫的配置
<profiles> <profile> <id>rdc-private-repo</id> <repositories> <repository> <id>rdc-releases</id> <url>https://repo.rdc.aliyun.com/repository/119569-release-xxxx/</url> </repository> <repository> <id>rdc-snapshots</id> <url>https://repo.rdc.aliyun.com/repository/119569-snapshot-xxxx/</url> </repository> </repositories> </profile>
2.修改項目內的pom.xml文件
<groupId>com.xxx.common</groupId> <artifactId>xxxx-log</artifactId> <version>1.0</version> <packaging>jar</packaging>
有一點要注意, 項目的pom.xml文件呢不能有<build>標簽, 不然下一步會失敗, 而且正式版的版本號內不能帶有SNAPSHOT
3.部署項目到倉庫
在pom.xml文件中右擊,run As – Maven build … 打開如下的框。
在 Goal輸入如下命令:
上傳成功后如圖:
4.開始使用
maven:
首先在settings.xml中添加配置:
<servers> <server> <id>rdc-releases</id> <username>賬號</username> <password>******</password> </server> <server> <id>rdc-snapshots</id> <username>賬號</username> <password>******</password> </server> <profile> <id>rdc-private-repo</id> <repositories> <repository> <id>rdc-releases</id> <url>https://repo.rdc.aliyun.com/repository/119569-release-xxx/</url> </repository> <repository> <id>rdc-snapshots</id> <url>https://repo.rdc.aliyun.com/repository/119569-snapshot-xxx/</url> </repository> </repositories> </profile>
在項目的pom.xml文件中加入依賴項
<dependency> <groupId>剛才上傳時的分組id</groupId> <artifactId>剛才上傳時的項目id</artifactId> <version>1.0</version> </dependency>
gradle:
在build.gradle文件內加入配置:
allprojects { repositories { maven { url 'https://maven.aliyun.com/repository/public' } maven { credentials { username '賬號' password '******' } url 'https://repo.rdc.aliyun.com/repository/119569-release-xxx/' } maven { credentials { username '賬號' password '******' } url 'https://repo.rdc.aliyun.com/repository/119569-snapshot-xxx/' } } }
在build.gradle文件內加入依賴項
compile group: '剛才上傳時的分組id', name: '剛才上傳時的項目id', version: '1.0'
參考文章:
https://www.cnblogs.com/supiaopiao/p/9804733.html
https://blog.csdn.net/loveshunyi/article/details/88813433
本文鏈接: