Checking my maven repository with Groovy Inverting Brian’s Mind
Dec 01

Groovy mavenisms again: Building a lib dir from dependencies

Builds, Groovy, Tech Add comments

To follow on in the same vein as my last post, I also have had the need now and then to take my maven world, and build a lib directory snapshot of my projects dependencies to pass on to others.

This was used with people not using maven, and for awhile for my IDE to point too (although now I can % maven idea). The only annoying part of maven idea is that it whipes your config each time, and you can’t share the project as the files are all absolute to your own personal maven repo.

Maven Jar Builder

#!/bin/env groovy
# -----------------------------------------------------------------------------
# maven2jar.g: Take a given project.xml and build a directory with jar files
# -----------------------------------------------------------------------------

import groovy.util.XmlParser
import java.nio.channels.*

# -- Enforce the project.xml and dir
if (args.length < 1) {
println """
Usage: maven2jar.g project.xml [newdir]

e.g. maven2jar.g /foo/project.xml tempdir (defaults to .)

# -- Variables
projectxml   = args[0]
todir        = (args.length > 1) ? args[1] : ".";
badFiles     = []
mavenLocalRepository = System.getProperty("user.home") + "/.maven/repository"

# -- Open up the file and do the work
project = new XmlParser().parse(projectxml)

project.dependencies.dependency.each {
groupId    = it.groupId[0].text()
artifactId = it.artifactId[0].text()
version    = it.version[0].text()

fromFileName = "${mavenLocalRepository}/${groupId}/jars/${artifactId}-${version}.jar"
toFileName   = "${todir}/${artifactId}-${version}.jar"

println "-----------------------------------------------------------------------"
println " Copying From: ${fromFileName}"
println " Copying To  : ${toFileName}"

try {
copyFile(new File(fromFileName), new File(toFileName))
} catch (Exception e) {
println "Error: Could not copy: ${e}"

# -- Define the function to copy the files
def copyFile(inFile, outFile) {
FileChannel sourceChannel = new FileInputStream(inFile).getChannel()
FileChannel destinationChannel = new FileOutputStream(outFile).getChannel()
sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel)

One Response to “Groovy mavenisms again: Building a lib dir from dependencies”

  1. qwe Says:

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What is the number before 3? (just put in the digit)