Skip to content

Instantly share code, notes, and snippets.

@martin-mfg
Forked from jakub-bochenski/maven-deployer.groovy
Last active April 25, 2019 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-mfg/3a351b5f05d3c7c44379f7dcea565dc5 to your computer and use it in GitHub Desktop.
Save martin-mfg/3a351b5f05d3c7c44379f7dcea565dc5 to your computer and use it in GitHub Desktop.
A pom.xml to batch deploy artifacts which are put under local repository folder structure to a repository. 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/94148 Referenced by http://aleung.github.co…
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(
project.distributionManagement.repository.id,
project.distributionManagement.repository.url,
layout, true );
def snapshotRepository = repoFactory.createDeploymentArtifactRepository(
project.distributionManagement.snapshotRepository.id,
project.distributionManagement.snapshotRepository.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');
def baseFolder = new File(baseFolder);
baseFolder.eachFileRecurse{
if(it.isDirectory()) return
log.debug "Processing " + it.absolutePath
def packaging = it.name.replaceAll( /.+\./ , '' );
if (!(packaging in ['jar'])) return
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]
if (fileName == 'maven-metadata.xml') return
def isSnapshot = version.endsWith('-SNAPSHOT')
//if (isSnapshot) return
def uniqueVersion = version.replace('-SNAPSHOT',/-\d{8}\.\d{6}-\d+/)
def fileNameMatcher = (fileName =~ /($artifactId)-($version|$uniqueVersion)-?(.*)\./)
def actualVersion = fileNameMatcher[0][2]
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 + '-' + actualVersion + '.pom'
log.info "Deploying " + relativePath + " with POM " + pomFilePath.replace(baseFolder.absolutePath, '')
artifact.addMetadata( new org.apache.maven.project.artifact.ProjectArtifactMetadata(artifact, new File(pomFilePath) ) );
deployer.deploy(it, artifact, isSnapshot ? snapshotRepository : repository, localRepository );
}
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>deployer</artifactId>
<version>20161412</version>
<packaging>pom</packaging>
<properties>
<deploy.basefolder>${env.WORKSPACE}/target/m2repo</deploy.basefolder>
</properties>
<build>
<defaultGoal>org.codehaus.gmavenplus:gmavenplus-plugin:execute</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.7</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>execute</goal>
</goals>
<phase>package</phase>
<configuration>
<properties>
<property>
<name>baseFolder</name>
<value>${deploy.basefolder}</value>
</property>
</properties>
<scripts>
<script>file://${env.MAVEN_BULK_DEPLOYER}</script>
</scripts>
</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>
<snapshotRepository>
<id>my-repo</id>
<url>http://myhost.com/nexus/content/repositories/my-repo</url>
<layout>default</layout>
</snapshotRepository>
</distributionManagement>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment