In HarmonyOS Next development, efficient package management is the key to the smooth progress of a project. The ohpm-repo private repository provides us with powerful package management functions. Today, I will introduce in detail how to download and upload packages in this private repository and related optimization techniques.
How to Download Dependent Packages from ohpm-repo?
ohpm config set registry to Set the Private Repository Source
Before downloading dependent packages from ohpm-repo, we first need to tell the ohpm package manager where to find our private repository. This requires using the ohpm config set registry
command to set the private repository source. The repository source address is usually the address of store.config.server
in the configuration file plus /repos/ohpm
. For example, if store.config.server
is http://192.168.1.100:8088
, then the command to set the repository source is as follows:
ohpm config set registry http://192.168.1.100:8088/repos/ohpm
In this way, ohpm knows to obtain dependent packages from the private repository we specified.
ohpm install for Installation
After setting the private repository source, you can easily install dependent packages using the ohpm install
command. For example, if we want to install the @ohos/lottie
package, we only need to execute the following in the terminal in the project directory:
ohpm install @ohos/lottie
At this time, ohpm will search for and download the @ohos/lottie
package and its dependencies from the private repository we just set, and then automatically complete the installation. If the project depends on multiple packages, ohpm install
will handle all dependencies at once, greatly improving development efficiency.
How to Publish Custom Packages to the Private Repository?
ohpm publish to Upload Packages like.har and.tgz
In HarmonyOS Next development, the custom packages we develop can be static shared packages (HAR packages) or dynamic shared packages (HSP packages). Starting from ohpm command-line tool version 1.3.0 and ohpm-repo private repository version 1.1.0, HSP packages can also be published to ohpm-repo in the form of.tgz files.
For static shared HAR packages, the publication is very simple. First, ensure that the HAR package has been generated. Suppose the package name is demo.har
, and execute the following in the terminal:
ohpm config set publish_registry <ohpm-repo Private Repository Management Address>/repos/ohpm
ohpm publish demo.har
You can also specify the publication address through the --publish_registry
parameter in the command line:
ohpm publish demo.har --publish_registry <ohpm-repo Private Repository Management Address>/repos/ohpm
For dynamic shared HSP packages, since they cannot be directly published in ohpm-repo, they need to be converted into.tgz packages first, and the conversion method can refer to the relevant documentation. After the conversion is completed, the publication process is the same as that of HAR packages. For example:
ohpm config set publish_registry <ohpm-repo Private Repository Management Address>/repos/ohpm
ohpm publish demo.tgz
Or
ohpm publish demo.tgz --publish_registry <ohpm-repo Private Repository Management Address>/repos/ohpm
When publishing, the filling of the ohpm-repo private repository management address should be determined according to the configuration of listen
. If the host
of listen
is not 0.0.0.0
, the management address uses the complete format of listen
; if the host
of listen
is 0.0.0.0
, the host
needs to be changed to the ip/domain name of the machine where the ohpm-repo private repository is deployed.
Package Management Optimization: How to Accelerate Downloading and Caching?
uplink_cache Mechanism
ohpm-repo provides the uplink_cache
mechanism to accelerate package downloading. uplink_cache_path
specifies the remote package cache path, and the default is ./uplink
; uplink_cache_time
sets the cache time of the remote package metadata, with the unit being hours, and the default is 168 hours. Reasonably adjusting these two parameters can significantly improve the download speed.
For example, we can set the cache path to a disk partition with faster read and write speeds to improve the read and write efficiency of the cache:
uplink_cache_path: /data/ohpm-cache/uplink
If the remote packages relied on by the project are not updated frequently, we can appropriately extend the cache time to reduce repeated requests for remote resources:
uplink_cache_time: 336
In this way, within the cache validity period, when downloading the same package again, ohpm-repo will directly obtain it from the local cache, greatly accelerating the download speed.
store.config.server Configuration
The store.config.server
configuration also plays an important role in package management optimization. It specifies the download address of the repository content, and correctly configuring this address can ensure the accuracy and efficiency of the download. When the host
of listen
is 0.0.0.0
and there are multiple network interfaces on the local machine, store.config.server
must be configured. If it is not configured properly, it may lead to download failures or slow download speeds.
For example, when listen
is 0.0.0.0:8088
, store.config.server
should be configured as http://<Local Machine ip/Domain Name>:8088
. If you need to access the ohpm-repo service through a reverse proxy, this field must be configured as the domain name address of the reverse proxy server, and the use_reverse_proxy
value needs to be configured as true
. In this way, by reasonably configuring store.config.server
, the download path of the package can be optimized, and the download speed can be improved.
Through the above methods, in HarmonyOS Next development, we can skillfully use ohpm-repo for package downloading and uploading, and improve the efficiency of package management through optimization measures, saving a lot of time and effort for project development. I hope these contents can help everyone make better use of the ohpm-repo private repository for package management in actual development.
Top comments (0)