Use the interface to transfer the local jar package to the nexus warehouse

Table of Contents

    • background
    • Optimization
      • 1. Use the interface call
        • result
      • 2. Use the mvn command to manually push (the password needs to be configured in the setting file, see the setting file at the top of the article for details)
    • Summarize

Background


At present, we need to write some code in the parent pom of the project to push the project package to the nexu warehouse, as follows, but it can also be achieved by deleting the configuration in the pom file when pulling the jar package below
, indicating that the drop-down is the configuration in the setting file. Then we think, according to the principle of minimum knowledge in object-oriented coding: in principle, specific projects do not need to know the relevant configuration of nexus, can we untie this layer of coupling

Currently used:
setting file configuration

<?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">

<!-- Local warehouse address -->
<localRepository>yourRepositoryPosition</localRepository>

<!--nexus server, id customization, user name and password are account password of nexus server-->
  <servers>
  <!--id is the corresponding resource library 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>
  <!--The url address of the group resource library id and name are customized, the value of mirrorOf is set to central, hard-coded -->
  <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>

Configuration in the project parent pom

 <distributionManagement>
        <!--Release type managed resource library-->
        <repository>
            <!--id corresponds to the id of the nexus warehouse-->
            <id>test_hosted</id>
            <!--Custom name-->
            <name>Releases</name>
            <!--The URL address corresponding to the warehouse-->
            <url>http://192.168.60.xxxx:8089/repository/test_hosted/</url>
        </repository>
        <!--Snapshot type managed repository -->
        <snapshotRepository>
            <!--id corresponds to the id of the nexus warehouse-->
            <id>test_snapshot_hosted</id>
            <!--Custom name-->
            <name>Snapshot</name>
            <!--The URL address corresponding to the warehouse-->
            <url>http://192.168.60.xxxx:8089/repository/test_snapshot_hosted/</url>
        </snapshotRepository>
    </distributionManagement>

Optimization plan

1. Use interface call

Upload the locally prepared jar package to the warehouse by calling the interface by writing code.

Introducing dependencies

<dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </dependency>

actual encoding

 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:\project development\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);

        }

Results

Result:

2. Use the mvn command to manually push (the password needs to be configured in the setting file, see the setting file at the top of the article for details)

mvn deploy:deploy-file -DgroupId=com.tfjybj -DartifactId=metaverse-gateway -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile=E:\zy\TGB-zgy-2023\ To be deleted\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

Summary

1. The seven principles in the design pattern can also be used in life. The minimum knowledge of the principle can ensure safety. What we need to do is to know why the principle is produced rather than how to use it.
2. Don’t let people do what can be done by machines.

maven concept reference
nexus concept reference