Building custom libs that are missing in maven central or jcenter
root
So you really need that java lib somebody made but they can’t be bothered to upload it to maven central or jcenter? Build it yourself and host it on your nexus repo. That’s what the script below does. Additionally, it includes a little xslt magic which allows to change the pom.xml in any way you want. Are they using snapshot versions? No problem, just insert your own (as shown in the script). Need to change a dependency? With some more xslt you can definitely do that too! You can run this script locally or on a build server (the script is using jenkins’ BUILD_NUMBER environment variable).
Hope this is useful to someone. All you need to install is ‘xsltproc’.
#!/bin/bash
set -e
##
## SETUP
##
VERSION="4.2.${BUILD_NUMBER}.yourorg"
REPO="https://yourorg.com/nexus/content/repositories/yourorg"
user="nexus user here"
pass="nexus pass here"
##
## MANIPULATE POM
##
cat > pom.xsl << EOF
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pom="http://maven.apache.org/POM/4.0.0">
<xsl:output method="xml" indent="yes"/>
<!-- replace the version -->
<xsl:template match="/pom:project/pom:version/text()">
<xsl:value-of select="\$version" />
</xsl:template>
<!-- remove special jar naming -->
<xsl:template match="/pom:project/pom:build/pom:finalName" />
<!-- copy everything else to the output -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
EOF
xsltproc --stringparam version ${VERSION} pom.xsl pom.xml > newpom.xml
mv newpom.xml pom.xml
##
## BUILD
##
/usr/bin/mvn -Dmaven.compiler.target=1.7 -Dmaven.compiler.source=1.7 clean package
##
## DEPLOY TO NEXUS
##
curl -v -u $user:$pass --upload-file pom.xml \
${REPO}/that/other/custom-lib/${VERSION}/custom-lib-${VERSION}.pom
curl -v -u $user:$pass --upload-file target/custom-lib-${VERSION}.jar \
${REPO}/that/other/custom-lib/${VERSION}/custom-lib-${VERSION}.jar