Skip to content

Instantly share code, notes, and snippets.

@aleung
Last active May 21, 2021 10:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save aleung/5194777 to your computer and use it in GitHub Desktop.
Save aleung/5194777 to your computer and use it in GitHub Desktop.

Note: This file was created in 2003. It might not work on current version of Maven.

A pom.xml to batch deploy artifacts which are put under local repository folder structure to a repository.

If you upload your local repository, make sure you have removed the non-artifact files like *.sha1, *.lastUpdated. Modify the setting in <deploy.basefolder> and <distributionManagement> before use.

Run mvn install to deploy.

This file is created base on StackOverflow answer: http://stackoverflow.com/a/3304212/94148Referenced by http://aleung.github.io/blog/2013/03/19/maven-batch-deploy-artifacts/

<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>localrepodeployer</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<properties>
<!-- This is where your artifacts are -->
<deploy.basefolder>./repository</deploy.basefolder>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>deploy-files</id>
<phase>prepare-package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
<![CDATA[
// read components from plexus container
def layout = session.lookup(
'org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout');
def repoFactory = session.lookup(
'org.apache.maven.artifact.repository.ArtifactRepositoryFactory');
def repository = repoFactory.createDeploymentArtifactRepository(
pom.distributionManagement.repository.id,
pom.distributionManagement.repository.url,
layout, true );
def localRepository = session.localRepository;
def helper =
session.lookup("org.apache.maven.project.MavenProjectHelper");
def afm = session.lookup(
'org.apache.maven.artifact.handler.manager.ArtifactHandlerManager');
def factory = new org.apache.maven.artifact.factory.DefaultArtifactFactory();
factory.class.getDeclaredField("artifactHandlerManager").accessible = true;
factory.artifactHandlerManager=afm;
def deployer = session.lookup(
'org.apache.maven.artifact.deployer.ArtifactDeployer');
// initialize properties
def baseFolder = new File(pom.properties['deploy.basefolder']);
// iterate over all files recursively
baseFolder.eachFileRecurse{
if(it.isDirectory())return;
System.out.println "Deploying " + it.absolutePath
// packaging = file.extension
def packaging = it.name.replaceAll( /.+\./ , '' );
def relativePath = it.absolutePath.replace(baseFolder.absolutePath, '')
def matcher = (relativePath =~ /\/?(.*)\/(.+)\/(.+)\/(.*)/)
if (!matcher) return;
def groupId = matcher[0][1].replace('/', '.');
def artifactId = matcher[0][2]
def version = matcher[0][3]
def fileName = matcher[0][4]
def fileNameMatcher = (fileName =~ /($artifactId)-($version)-?(.*)\./)
def classifier = fileNameMatcher[0][3]
if (classifier.equals(""))
artifact = factory.createBuildArtifact(groupId, artifactId, version, packaging );
else
artifact = factory.createArtifactWithClassifier(groupId, artifactId, version, packaging, classifier);
def pomFilePath = baseFolder.absolutePath + '/' + matcher[0][1] + '/' + artifactId + '/' + version + '/' + artifactId + '-' + version + '.pom'
System.out.println "POM: " + pomFilePath
File pomFile = new File(pomFilePath)
def metadata =
new org.apache.maven.project.artifact.ProjectArtifactMetadata(artifact, pomFile );
artifact.addMetadata( metadata );
// deploy file
deployer.deploy(it, artifact, repository, localRepository );
}
]]>
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- Modify below to point to your repository -->
<distributionManagement>
<repository>
<id>my-repo</id>
<url>http://myhost.com/nexus/content/repositories/my-repo</url>
<layout>default</layout>
</repository>
</distributionManagement>
</project>
@avoidik
Copy link

avoidik commented Jul 4, 2017

which mvn version do you have? because we have error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager@54336976' with class 'org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager' to class 'org.apache.maven.artifact.handler.manager.ArtifactHandlerManager'

@hayatbehlim
Copy link

I'm also getting same error @avoidik do you find for above issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment