一、配置Maven setting.xml文件
要在 Maven 工程中使用私服,需要提供私服配置信息。
1、配置 Servers 信息
<servers>
<server>
<id>nexus-releases</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
2、配置 mirrors 信息
把 mirrors
中的相关配置注释掉。
3、配置 profiles 信息
3.1、配置JDK版本信息
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
3.2、配置 私有仓库地址 和 插件地址
<profile>
<!--可以任一名称-->
<id>sunday</id>
<!--JDK的相关版本-->
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.8</jdk>
</activation>
<repositories>
<!-- 私有地址库 -->
<repository>
<!--可以任一名称-->
<id>nexus</id>
<!-- 对应私有仓库中的地址 -->
<url>http://xxxx/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!--插件地址库-->
<pluginRepository>
<!--可以任一名称-->
<id>nexus</id>
<url>http://xxxx/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
私有仓库地址 和 插件地址库
3.3、配置 activeProfiles 信息
<!-- 激活profile -->
<activeProfiles>
<activeProfile>sunday</activeProfile>
</activeProfiles>
二、配置Maven项目 pom.xml 文件
1、新建一个Maven项目工程
maven工程项目
2、配置工程 pom.xml 信息
2.1、配置私服Server信息
<distributionManagement>
<repository>
<!-- 对应Maven setting.xml配置文件中 server 一一对应 -->
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://xxxx/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<!-- 对应Maven setting.xml配置文件中 server 一一对应 -->
<id>nexus-snapshots</id>
<name>Nexus snapshot Repository</name>
<url>http://xxxx/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
2.2、配置私服 build 插件信息
打包发布源码信息
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.3、添加一个Maven依赖,测试私服是否配置成功
- 添加依赖后,Maven从我们指定的地址下载依赖包
- Maven工程依赖的Jar已经从我们的私服下载到本地仓库了。
三、发布本地工程到私服
在 Maven 工程的 maven build...
中,输入命令 deploy
,即可实现发布工程信息到私服。如果通版本工程可能多次发布,需要修改 nexus 配置。
1、右键工程 –> Run As
–> Maven build ...
2、输入命令 deploy
,点击 Run
。
3、查看是否上传成功
控制台输出上传成功
查看私服,上传成功
Releases 版本包的上传
四、发布第三方插件到私服
本地下载了一个第三发包,直接通过本地上传到 3rd party
私服中。
例如本地的jstl包 :
1、填写第三方上传包或者插件信息
2、点击 Add Artifact
–> Upload Artifact(s)
上传第三方包
查看上传的第三方包
3、在Maven 添加刚才增加第三方包依赖。
已经成功从私服中下载刚才添加的第三方包。
五、发布时的一些错误汇总
可以配置 deploy -X
控制台打印详细的日志信息。
1、Maven项目工程pom.xml没有配置server信息
错误信息 :
org.apache.maven.lifecycle.LifecycleExecutionException:
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:
deploy (default-deploy) on project
FlyGo_Maven_Nexus: Deployment failed: repository element was not specified
in the POM inside distributionManagement element or
in -DaltDeploymentRepository=id::layout::url parameter
2、 Nexus私服没有设置允许多次发布
错误信息:
org.apache.maven.lifecycle.LifecycleExecutionException:
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project
FlyGo_Maven_Nexus: Failed to deploy artifacts: Could not transfer artifact com.flygo520:FlyGo_Maven_Nexus:jar:0.0.1
from/to nexus-releases (http://xxxx/nexus/content/repositories/releases/): Failed to transfer file:
http://xxxx/nexus/content/repositories/releases/com/flygo520/FlyGo_Maven_Nexus/0.0.1/FlyGo_Maven_Nexus-0.0.1.jar. Return code is: 400,
ReasonPhrase: Bad Request.
六、演示Demo源码地址
GitHub - jxaufang168/FlyGo_Maven_Nexus: FlyGo_Maven_Nexus