发布本地项目和源码到maven私服中

有时候我们会使用第三方包到我们的项目中,但是想看源码的时候,需要下载源码查看,十分麻烦。

不如把源码上传到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

原文地址:https://www.cnblogs.com/sloveling/p/maven_deploy.html