Batch import Maven's local library directory to Nexus script analysis

How to import jars and batch import Maven's local library directory in Nexus This article introduces how to use scripts to batch import all dependencies of Maven's local library directory to the Nexus private server. Many students reported that they could not understand the content of the script. This article will analyze the scripts imported in batches.

script review

#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
   case $opt in
   	r) REPO_URL="$OPTARG"
   	;;
   	u) USERNAME="$OPTARG"
   	;;
   	p) PASSWORD="$OPTARG"
   	;;
   esac
done

find . -type f -not -path './mvnimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

Screenplay analysis - section1

This is a B Shell script, which can be executed directly under Linux, and can be executed with the help of Git Bash under Windows.

  • The first four lines start with a pound sign ( #), which is the content of the comment
  • getopts ":r:u:p:" optis the value of the option to get the command line
  • ":r:u:p:"The first colon means that no error will be prompted when there are parameters that are not in the optstring on the command line. The colon after the option name indicates that the option will pass in a value. For example, the option setting r:in the command line is-r rvalue
  • while doneis a loop in the shell script
  • case in esac is a fork of Shell Script
  • The value of the obtained option will be put into $OPTARG"
while getopts ":r:u:p:" opt; do
   case $opt in
   	r) REPO_URL="$OPTARG"
   	;;
   	u) USERNAME="$OPTARG"
   	;;
   	p) PASSWORD="$OPTARG"
   	;;
   esac
done

Taken together, the meaning of the above script is:
to obtain the values ​​of the three options in the command line r u p . It is clear to see the way to pass the option values ​​when the command line is called,
-u admin -p yourpassword -r http://localhost:8081/repository/maven-releases/
that is, to obtain the Nexus library address, user name and password respectively. into the REPO_URL, USERNAME and PASSWORD variables.

Screenplay analysis - section2

find . -type f -not -path './mvnimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml'

The find command finds a list of files in the current directory (including subdirectories) that meet specified conditions. This command will filter out hidden files, files starting with . and some specified specific files.

Here is what each part of the command means:

.: Specifies to look in the current directory.
-type f: Indicates that only ordinary files are searched, excluding directories and other special files.
-not -path './mavenimport.sh*': Indicates that it does not match any file or directory named "mavenimport.sh", this part uses -not and -path to exclude any file named "mavenimport.sh" or directory.
-not -path ' /. ': Indicates that hidden files starting with "." are not matched. This section uses -not and -path to exclude any file paths that start with a slash followed by a period (eg "/.git").
-not -path ' /^archetype-catalog.xml ': means do not match any file or directory named "archetype-catalog.xml". The purpose of using -not and -path in this section is to exclude any file or directory with the filename "archetype-catalog.xml".
-not -path ' /^maven-metadata-local .xml': Means not matching any file or directory named "maven-metadata-local.xml". This section uses -not and -path to exclude any file or directory with the filename "maven-metadata-local.xml".
-not -path ' /^maven-metadata-deployment.xml': Indicates that no file or directory named "maven-metadata-deployment.xml" was matched. This section uses -not and -path to exclude any file or directory with the filename "maven-metadata-deployment.xml".
In summary, this command will find all normal files except hidden files starting with "." and some specific files not in the current directory.

Screenplay analysis - section3

| sed "s|^\./||" 
  • The vertical bar |is the pipe name, and the output of the previous command is used as the input of the next command.
    The sed command here is used to delete the "./" in the prefix of the path name. It is suitable for outputting as a file list or processing files in the current directory.

In this command, sed is the tool for text substitution, s indicates the start of the substitution operation, and | indicates the delimiter, which surrounds all parts of the substitution operation. Every command line ends with |. For example, ^./ means to find the text to be replaced from the beginning, ie ./, and delete it.

So, if there is a list of files with relative paths ./ in front of the file names, use the following command to remove the ./ path name prefix in front of all file names:

Copy code

sed "s|^./||" filelist
This will output a list of filenames without pathname prefixes.

./antlr/antlr/2.7.2/antlr-2.7.2.jar
=》
antlr/antlr/2.7.2/antlr-2.7.2.jar

Screenplay analysis - section4

| xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} 

This is a curl command to upload multiple local files to a remote repository. This command also uses the xargs command to pass the output as an argument to the curl command.

Here is what each part of the command means:

xargs -I '{}': Use the output of the previous command as the parameter of the following command, and -I '{}' specifies the placeholder.
curl: Used to send HTTP requests.
-u " USERNAME : USERNAME :USERNAME: PASSWORD”: Specify the authentication credentials, where $USERNAME and $PASSWORD are variables for saving the user name and password.
-X PUT: Specify the HTTP request method as PUT, which is used to upload files to the remote warehouse.
-v: Enable verbose mode , you can view the details of the request and response sent and received by the command.
-T {}: Specify the path of the uploaded file, {} as a placeholder, replaced by the actual file path by the xargs command.
REPURL / : Specify the remote location of the uploaded file Warehouse URL path, {REPO_URL}/{}: Specify the remote warehouse URL path for uploading files,REPOURL/:Specify the URL path of the remote warehouse for uploading files . { REPO_URL } is a variable that stores the URL of the warehouse.
In summary, this command will find all files in the current directory, and use the curl command to upload the files to the specified remote warehouse path. Authenticate with the specified username and password upon upload until all files have been uploaded.



Guess you like

Origin blog.csdn.net/oscar999/article/details/131446832