背景
目前我们要把项目的包推送到nexu仓库中需要在项目的父pom中写部分代码,如下,但是往下边拉jar包的时候删除pom文件中的配置也能实现
,说明下拉的时候是走的setting文件中的配置。那我们思考,根据面向对象编码中的最小知道原则:具体项目原则上不需要知道nexus的相关配置,我们是不是可以将这一层耦合解开呢
目前使用方式:
setting文件配置
<?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/SETTINGS/1.0.0"> <!-- 本地仓库地址 --> <localRepository>yourRepositoryPosition</localRepository> <!--nexus服务器,id自定义,用户名和密码为nexus服务器的账号密码--> <servers> <!--id为对应资源库id--> <server> <id>test_snapshot_hosted</id> <username>admin</username> <password>admin</password> </server> <server> <id>test_hosted</id> <username>admin</username> <password>admin</password> </server> <server> <id>nexus</id> <username>admin</username> <password>admin</password> </server> </servers> <!--组资源库的url地址 id和name自定义,mirrorOf的值设置为central,写死的--> <mirrors> <mirror> <id>nexus</id> <name>nexus repository</name> <url>http://192.168.XXXXX.220:8089/repository/test_group/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> </settings>
项目父pom中配置
<distributionManagement> <!--Release类型的托管资源库--> <repository> <!--id对应nexus仓库的id--> <id>test_hosted</id> <!--自定义名称--> <name>Releases</name> <!--仓库对应的URL地址--> <url>http://192.168.60.xxxx:8089/repository/test_hosted/</url> </repository> <!--Snapshot类型的托管资源库--> <snapshotRepository> <!--id对应nexus仓库的id--> <id>test_snapshot_hosted</id> <!--自定义名称--> <name>Snapshot</name> <!--仓库对应的URL地址--> <url>http://192.168.60.xxxx:8089/repository/test_snapshot_hosted/</url> </snapshotRepository> </distributionManagement>
优化方案
1、使用接口调用
通过写代码的方式将本地打好的jar包通过调接口的方式上传到仓库中。
引入依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency>
实际编码
public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); String username = "admin"; String password = "admin"; restTemplate.getInterceptors().add( new BasicAuthenticationInterceptor(username, password)); String filename = "D:\\项目开发\\metaverse3-backend\\metaverse3-gateway\\target\\metaverse3-gateway-1.0-SNAPSHOT.jar"; File file = new File(filename); FileSystemResource resource = new FileSystemResource(file); MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>(); requestMap.add("file", resource); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestMap, headers); test(restTemplate,requestEntity); } public static void test(RestTemplate restTemplate ,HttpEntity<MultiValueMap<String, Object>> requestEntity){ String url = "http://192.168.xx.xxx:xxxx/repository/test_snapshot_hosted/"; String groupId = "com.yourcompany"; String artifactId = "my-artifact"; String version = "1.0-SNAPSHOT"; String packaging = "jar"; String classifier = "my-classifier"; String repositoryPath = groupId.replace('.', '/') + '/' + artifactId + '/' + version + '/' + artifactId + '-' + version + (classifier == null ? "" : '-' + classifier) + '.' + packaging; String nexusUrl = url + repositoryPath; ResponseEntity<String> responseEntity = restTemplate.exchange(nexusUrl, HttpMethod.PUT, requestEntity, String.class); }
结果
2、使用mvn命令手动推(需要在setting文件中配置密码,详情见文章最上边setting文件)
mvn deploy:deploy-file -DgroupId=com.tfjybj -DartifactId=metaverse-gateway -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile=E:\zy\TGB-zgy-2023\待删除\metaverseDemo\metaverse-gateway\target\metaverse-gateway-1.0-SNAPSHOT.jar -Durl=http://192.168.XXX.220:8089/repository/test_snapshot_hosted/ -DrepositoryId=test_snapshot_hosted
总结
1、设计模式中的七大原则也可以用到生活中,最小知道原则可以保证安全性,我们要做的更多的是要知道原则为什么产生而不是怎么使用。
2、能交给机器做的不要交给人去做。