一、简介
在微服务开发中,一般会使用到maven私服和gitlab代码管理。
有些公司不提供外网给开发人员,因此访问maven中央仓库需要在局域网搭建一个maven私服,开发人员通过访问私服来上传与下载jar包,而私服有外网可以访问中央仓库。maven私服更方便管理和维护,且开发人员从私服拉取jar包更快。还有一点是若开发人员很多,在同一个ip频繁访问中央仓库拉取jar包,有可能这个ip会进入中央仓库黑名单。因此搭建maven私服很有必要。
一般开源项目可以用github做代码管理,若是不开源的话,需要创建私人仓库,但是是收费的。而gitlab是免费的,可以自己搭建一个来做代码管理。
二、基于Nexus3的maven私服
创建仓库
如此便创建好了。
修改本地maven的settings.xml配置文件
添加server配置
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>shop</id>
<username>admin</username>
<password>admin</password>
</server>
添加mirrors配置
<mirror>
<id>nexus</id>
<name>nexus Mirror chenshu repository</name>
<url>http://47.98.183.103:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
<mirror>
<id>shop</id>
<name>shop repository</name>
<url>http://47.98.183.103:8081/repository/shop/</url>
<mirrorOf>shop</mirrorOf>
</mirror>
配置profiles
<profile>
<id>jdk1.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>
<repositories>
<repository>
<id>maven-public</id>
<url>http://47.98.183.103:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>shop</id>
<url>http://47.98.183.103:8081/repository/shop/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
ok,如此便配置好了,接下来我们用idea将我们的微信接口服务打包上传到maven私服试试。
首先在shop-parent项目的pom文件加入以下配置
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://47.98.183.103:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus Plugin Repository</name>
<url>http://47.98.183.103:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://47.98.183.103:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://47.98.183.103:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
接下来执行打包操作
发现控制台报错了
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.6.RELEASE:repackage (repackage) on project shop-service-api-weixin: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.2.6.RELEASE:repackage failed: Unable to find main class -> [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/PluginExecutionException
出现这个错误的原因是,在shop-parent项目pom文件中含有以下配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
这个插件的作用是当运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“Java -jar”命令就可以直接运行。一般的maven项目的打包命令,不会把依赖的jar包也打包进去的,只是会放在jar包的同目录下,能够引用就可以了,但是spring-boot-maven-plugin插件,会将依赖的jar包全部打包进去。
需要注意的是打包时会去扫描项目main方法入口,也就是说引入该配置,你就必须在项目src/main/java/下创建一个spring-boot启动类。
由于我们的微信接口服务是没有入口类的,所以报错了。
我们通过将这个插件的配置从shop-parent中删除,然后在shop-basics和shop-service-impl中添加这个插件的配置来解决。
接着我们继续执行打包操作。
发现打包成功了,而且日志显示上传jar包到了我们的maven私服。
打开我们的maven私服,也可以看到我们上传的jar包。
jar的version分为snapshot(开发版)和releases(稳定正式版)。
- snapshot:每次都会从服务器拉取一个最新的版本使用,因此这种版本很适合开发过程中使用,当有bug或者调整,维护提交了之后,使用就会获取到最新的使用。
- releases:当检测到本地的maven仓库有相应的版本之后,不会去服务器拉取,就直接使用本地的版本了。
三、gitlab
使用很简单,跟github类似。
github项目地址https://github.com/liazhan/shop-project/tree/c617ad6c8487eec54cd2c8a7e627af62172dbe14
版本号为c617ad6c8487eec54cd2c8a7e627af62172dbe14