- Snippet Path
Publish all maven dependencies to a private registry
- Authors
- ← Back to snippet list

- Name
- Aryan Ebrahimpour
- GitHub
- @avestura
- Snippet
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
mvncli anddependency:copy-dependenciescommand
Method 1: Extract using repository folder
- Find the maven
repositoryfolder in your home folder.
- On Windows:
%USERPROFILE%/.m2 - On Linux:
~/.m2
- If it already exists, rename it to something like
repository.backup - Invoke the maven command for building your project. It will download everything needed and copies
it into the newly created
repositoryfolder. - Copy the newly created
repositoryfolder to another location in your system. You can use%USERPROFILE%/.m2backup/repositoryon Windows and~/.m2backup/repositoryon Linux. - Set the location variable for the next step.
powershell
$DEPENDENCIES_LOCATION="$home/.m2backup/repository" # Based on Step 4
- Recover your old repository folder by renaming the
repository.backupback torepositoryin the original.m2folder.
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
mvncli anddeploy:deploy-filecommand
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.
- Set your authentication username and password.
powershell
$NEXUS_USERNAME="username"
$NEXUS_PASSWORD="password"
- 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
}
}