How does Nexus import jar and batch import Maven's local library directory

foreword

  • The version based on Nexus in this article is nexus-3.55.0-01
  • This method is applicable to Linux and Windows
  • Windows needs to install Git, use Git Bash to execute

How Nexus uploads dependent packages

There are many ways to upload dependency packages to the Nexus server, including:

  1. Single jar upload: Upload a single jar on the Nexus console page
  2. Source code compilation and upload: Use Maven's deploy command in the source code project to publish
  3. Use scripts to upload the directories of Maven local libraries in batches

1. Upload a single jar: Upload a single jar on the Nexus console page

To upload the jar on the Nexus web console, you need to log in first. The specific steps are as follows:

  1. After logging in, click the "Upload" button on the left navigation bar
    insert image description here

  2. Select the library to be uploaded, select maven-releases here, click the library to be uploaded
    insert image description here

  3. Select the file, enter the group name, component name, version, etc.

Here we take the Oracle 12c Java driver ojdbc8 as an example, and fill in the information as follows:
insert image description here

  1. After clicking Finish, click the "Browser" button to see the uploaded package.

insert image description here

2. Use Maven's deploy command to publish in the source code project

To publish the project to the Nexus private server, you need to configure maven's settings.xml and local pom.xml.
settings.xml add the following configuration:

    <server>
      <id>osxm-nexus</id>
      <username>admin</username>
      <password>123456</password>
    </server>

Pom.xml configures the address of the published library

  <distributionManagement>
		<repository>
			<id>osxm-nexus</id>
			<name>Osxm Nexus Releases Repository</name>
			<url>http://localhost:8081/repository/maven-snapshots/</url>
		</repository>
  </distributionManagement>

Note: The id of the server must be consistent with the id of the repository.

After the successful deployment, the published results seen in the console are as follows:
insert image description here

For details on publishing projects to Nexus servers, please refer to:
How to publish projects to Nexus private servers in Maven

3. Use a script to batch upload the directory of the Maven local library

If it is necessary to import all the jars of the Maven local library into Nexus at one time, it is impossible to use a single import method.
Here, the Bash script can be used to import all the jars in the Maven local library directory into Nexus at one time. If it is in a Windows environment, you can install Git and use Git Bash to execute sh scripts.

Create the file mvnimport.sh under Maven's local library path, with the following content:

#!/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}/{} ;

Bash switches to Maven's local library path, similar to:

 cd /d/inssoftware/maven-3.9.2/repo

Execute the following command in this directory:

./mvnimport.sh -u admin -p yourpassword -r http://localhost:8081/repository/maven-releases/

The mvnimport.sh script can be downloaded directly from the following path: Batch import script of maven local library directory to Nexus private server



Guess you like

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