Snippet Path

Publish all maven dependencies to a private registry

Authors

1. Config Server

Add the server configuration to settings.xml file located in ~/.m2 folder.

xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
	<servers>
		 <server>
		  <id>FooBarRegistry</id>
		  <username>username</username>
		  <password>password</password>
		</server>
		<server>
		  <id>central</id>
		  <configuration>
			<httpConfiguration>
			  <all>
				<connectionTimeout>1200000</connectionTimeout>
				<readTimeout>1200000</readTimeout>
			  </all>
			</httpConfiguration>
		  </configuration>
		</server>
	</servers>
</settings>

2. cd to project directory.

3. Extract all dependencies of project to a folder

There are 2 ways to extract dependencies of a maven project to a folder.

  • Extract using local repository folder (recommended)
  • Exteact using mvn cli and dependency:copy-dependencies command

Method 1: Extract using repository folder

  1. Find the maven repository folder in your home folder.
  • On Windows: %USERPROFILE%/.m2
  • On Linux: ~/.m2
  1. If it already exists, rename it to something like repository.backup
  2. Invoke the maven command for building your project. It will download everything needed and copies it into the newly created repository folder.
  3. Copy the newly created repository folder to another location in your system. You can use %USERPROFILE%/.m2backup/repository on Windows and ~/.m2backup/repository on Linux.
  4. Set the location variable for the next step.
powershell
$DEPENDENCIES_LOCATION="$home/.m2backup/repository" # Based on Step 4
  1. Recover your old repository folder by renaming the repository.backup back to repository in the original .m2 folder.

Method 2: Extract using mvn cli

This command will copy all dependencies into the target/dependency folder. Beware that this method sometimes doesn't export all dependencies of all tasks in the dependency folder.

powershell
mvn -D"mdep.copyPom"=true dependency:copy-dependencies
$DEPENDENCIES_LOCATION="target/dependency"

4. Deploy the packages to repository

There are 2 ways to deploy the dependencies of a maven project to a custom repository.

  • Upload using Sonatype Nexus REST API (recommended, works only on Nexus registry)
  • Deploy using mvn cli and deploy:deploy-file command

Beware

Remember there are a couple edge cases using either method.

If you're on Windows, but deploying for Linux or vice versa, you might not have the Linux binaries on your Windows machine's local repository folder. Therefore your offline registry won't have the materials to work with another operating system. These scripts also can't detect files with Classifiers, or packages with binaries other than .jar extension (sometimes packages ship with .exe extensions), therefore you might need to extends these scripts for your own need in those cases.


Beware

Make sure to set the correct value for the DEPENDENCIES_LOCATION variable from the previous step.

Method 1: Deploy using Sonatype Nexus REST API

This method is faster and less buggy, but only works if you're using Sonatype Nexus as your custom registry.

  1. Set your authentication username and password.

Beware

It's not safe to set your username and password like this. This is for demonstration purposes only. Using a safer method is left as a task to the reader.


powershell
$NEXUS_USERNAME="username"
$NEXUS_PASSWORD="password"
  1. Deploy files using REST API
powershell
foreach($pom in ls -recurse $DEPENDENCIES_LOCATION/*.pom) {
    $jarFile = [io.path]::ChangeExtension($pom, "jar");
	if(test-path $jarFile) {
		# Packages with both POM and JAR files
		curl -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -v -F "maven2.generate-pom=false" -F "maven2.asset1=@$pom" -F "maven2.asset1.extension=pom" -F "maven2.asset2=@$jarFile;type=application/java-archive" -F "maven2.asset2.extension=jar" -u "$NEXUS_USERNAME:$NEXUS_PASSWORD" "http://nexuxrepo:8081/service/rest/v1/components?repository=FooBarRegistry"
	}
	else {
		# Packages with only POM files
		curl -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -v -F "maven2.generate-pom=false" -F "maven2.asset1=@$pom" -F "maven2.asset1.extension=pom" -u "$NEXUS_USERNAME:$NEXUS_PASSWORD" "http://nexuxrepo:8081/service/rest/v1/components?repository=FooBarRegistry"
	}
}

Method 2: Deploy using mvn cli

powershell
foreach($pom in ls -recurse $DEPENDENCIES_LOCATION/*.pom) {
    $jarFile = [io.path]::ChangeExtension($pom, "jar");
	if(test-path $jarFile) {
		# Packages with both POM and JAR files
		mvn deploy:deploy-file -Durl="http://nexuxrepo:8081/repository/FooBarRegistry/" -Dfile="$jarFile" -DgeneratePom=false -DpomFile="$pom" -DrepositoryId=FooBarRegistry
	}
	else {
		# Packages with only POM files
		mvn deploy:deploy-file -Durl="http://nexuxrepo:8081/repository/FooBarRegistry/" -Dfile="$pom" -DgeneratePom=false -DpomFile="$pom" -DrepositoryId=FooBarRegistry
	}
}