Skip to content

Instantly share code, notes, and snippets.

@jakub-bochenski
Forked from aleung/pom.xml
Last active July 8, 2022 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jakub-bochenski/7802ee7f44b8e3b99bdd69b2ab150e6c to your computer and use it in GitHub Desktop.
Save jakub-bochenski/7802ee7f44b8e3b99bdd69b2ab150e6c 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 ['sha1', 'md5']) 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')
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>
</project>
@martin-mfg
Copy link

Note that for this to work you need to specify repository and snapshotRepository, e.g. inside distributionManagement in your pom.xml. Also this script does not work on Windows due to different file separators ("\" instead of "/").

(I made these and some additional changes in my fork of the gist: https://gist.github.com/martin-mfg/3a351b5f05d3c7c44379f7dcea565dc5.)

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