有時候我們會使用第三方包到我們的項目中,但是想看源碼的時候,需要下載源碼查看,十分麻煩。
不如把源碼上傳到maven私服中,這樣查看源碼的時候就可以直接從mvaen nexus下載直接查看了。
方法如下:
1、在setting.xml文件中增加用戶名和密碼配置(特別注意這里的ID)
<servers> <!-- 用於發布正式版本 --> <server> <id>maven-repository-releases</id> <username>admin</username> <password>admin123</password> </server> <!-- 用於發布快照版本 --> <server> <id>maven-repository-snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers>
2、在項目的pom.xml中增加以下內容
<build> <plugins> <!-- 要將源碼放上去,需要加入這個插件 --> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <distributionManagement> <repository> <!-- 這里的ID要和setting的id一致 --> <id>maven-repository-releases</id> <url>http://ip:8081/nexus/content/repositories/thirdparty/</url> </repository> <!--這是打成快照版本的配置,如果不用這個snapshotRepository標簽,打包失敗,會報權限問題 --> <snapshotRepository> <id>maven-repository-snapshots</id> <url>http://ip:8081/nexus/content/repositories/thirdparty</url> </snapshotRepository> </distributionManagement>
3.執行Maven build的deploy命令
正常情況下,如果是發布新版本的話,一切是ok的。
4.如果是替換老版本,可能會出現如下錯誤
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project YourProject: Failed to deploy artifacts: Could not transfer artifact YourProject:jar:3.0.1 from/to maven-repository-releases (http://ip:8081/nexus/content/repositories/thirdparty/): Failed to transfer file: http://192.168.16.204:8081/nexus/content/repositories/thirdparty/../../YourProject.jar. Return code is: 400, ReasonPhrase: Bad Request. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
這是maven deploy 已存在的包的時候出現400錯誤,原因是 release 默認庫是不允許重復部署的。
解決: 修改maven 私服配置.
修改圖中配置就可以重復部署了:Deployment Policy:Allow Redeploy
然后,重新deploy,既可以將源碼和jar包一並上傳到maven私服了。
5.其他可能遇到的問題
用戶權限問題,可能導致發布失敗。
本文參考:http://blog.csdn.net/lzzyok/article/details/25626583f
http://blog.csdn.net/lulongzhou_llz/article/details/42869785