主要解决Sonatype Nexus return ’401′问题及查看maven当前生效的是哪个settings.xml
使用Sonatype把自己android-common的和android-auto-scroll-view-pager等库发布到Maven中央仓库
mvn clean deploy却发现始终401,提示
Java
1 2 3 4 5 6 7 |
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project trinea-android-common: Failed to deploy artifacts: Could not transfer artifact cn.trinea.android.common:trinea-android-common:pom:4.2.10-20140510.125625-1 from/to sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots/): Failed to transfer file: https://oss.sonatype.org/content/repositories/snapshots/ cn/trinea/android/common/trinea-android-common/4.2.10-SNAPSHOT/trinea-android-common-4.2.10-20140510.125625-1.pom. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1] |
检查了下
(1) 已添加C:\Users\your-user-name\.m2\settings.xml,并且内容包含
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<settings> ... <servers> <server> <id>sonatype-nexus-snapshots</id> <username>your-jira-id</username> <password>your-jira-pwd</password> </server> <server> <id>sonatype-nexus-staging</id> <username>your-jira-id</username> <password>your-jira-pwd</password> </server> </servers> ... </settings> |
(2) 上面的your-jira-id和your-jira-pwd已经替换为自己在https://oss.sonatype.org/上的用户名和密码。
用mvn help:effective-settings查看了下当前生效的settings文件内容,发现并没有上面(1)中添加的内容,怀疑settings文件是否生效了,用mvn -X命令查看如下
Java
1 2 3 4 5 6 7 |
Java version: 1.7.0_02, vendor: Oracle Corporation Java home: D:\Program Files\Java\jdk1.7.0_02\jre Default locale: zh_CN, platform encoding: GBK OS name: "windows 7", version: "6.1", arch: "x86", family: "windows" [INFO] Error stacktraces are turned on. [DEBUG] Reading global settings from D:\Program Files\apache-maven-3.2.1\conf\settings.xml [DEBUG] Reading user settings from C:\Users\Administrator\.m2\settings.xml |
先读的是maven安装目下conf里面的settings.xml,那为什么没生效呢?
直接修改D:\Program Files\apache-maven-3.2.1\conf\settings.xml mvn clean deploy可正常发布
D:\Program Files\apache-maven-3.2.1\conf\settings.xml拷贝到C:\Users\Administrator\.m2\下居然不会提示和已有的settings.xml文件重名冲突,复制两个文件名发现之前新建的文件名为?settings.xml,次奥,网页上复制过来多了个非法字符,并且一般不可见。这样删除?settings.xml这个文件保留settings.xml文件加上配置即可。
PS:
maven-help-plugin是一个小巧的辅助工具,最简单的help:system可以打印所有可用的环境变量和Java系统属性。help:effective-pom和help:effective-settings最为有用。
help:effective-pom用于查看当前生效的POM内容,指合并了所有父POM(包括Super POM)后的XML,所以可用于检测POM中某个配置是否生效
effective-settings可用于查看当前生效的settings.xml文件内容,所以可用于判断某个settings配置是否生效