nexus的实际使用

创建需要上传的maven工程

nexus-实际使用_maven

看看这个项目,其实很简单,就是写了个工具类,然后上传到私服,这个平时在公司开发还是挺常见的;

然后看看pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wcx</groupId>
<artifactId>nexus-test</artifactId>
<version>1.0.RELEASE</version>



<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>release</name>
<url>http://192.168.109.122:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>snapshot</name>
<url>http://192.168.109.122:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

</project>

上面那个就是项目的版本个组id,这里是上传一个RELEASE版本的jar


distributionManagement:这里面的意思就是指定上传的仓库路径,有两个,一个是上传快照版本snapshot,一般都是不稳定版本,然后还有一个就是上传稳定版本release,那两个路径就是nexus的默认宿主仓库


然后在配置一下settings.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<localRepository>F:\maven_repository\repository\repository</localRepository>

<pluginGroups>

</pluginGroups>

<proxies>

</proxies>


<servers>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>


<mirrors>
<mirror>
<id>public</id>
<name>aliyun maven</name>
<url>http://192.168.109.122:8081/repository/maven-public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>


<profiles>
<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>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>public</id>
<name>Nexus Central</name>
<!-- 指向镜像url -->
<url>http://192.168.109.122:8081/repository/maven-public/</url>
<layout>default</layout>
<!-- 表示你可以从这个仓库下载releases组件 -->
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Nexus Central</name>
<!-- 指向镜像url -->
<url>http://192.168.109.122:8081/repository/maven-public/</url>
<layout>default</layout>
<!-- 表示你可以从这个仓库下载releases组件 -->
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>

<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

这里注意:

<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>

id需要跟pom里面的id保持一致,或者可以将两个server设置成一个

<id>public</id> 

pom里面两个也都叫public就行了;

配置差不多就只有这些,然后就是发布了:

nexus-实际使用_java_02

这样就发布成功了,现在可以去UI界面上看看:

nexus-实际使用_apache_03

我们现在只需要去粘贴这个依赖皆就可以使用了;

创建使用私服jar的项目

nexus-实际使用_java_04

看看pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wcx</groupId>
<artifactId>nexus-use-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.wcx</groupId>
<artifactId>nexus-test</artifactId>
<version>1.0.RELEASE</version>
</dependency>
</dependencies>

</project>

因为settings.xml中已经配置了镜像源和镜像地址的profile,所以这里可以直接生效,关于maven的profile想了解详细的可以看下这篇文章:

然后在测试一下:

nexus-实际使用_xml_05

然后看这里就已经使用到了

总结

这只是一点小小的测试,像如果有很多通用模块需要使用,就可以将其上传到maven私服上,团队一起使用