Created
January 18, 2012 14:59
-
-
Save tfnico/1633380 to your computer and use it in GitHub Desktop.
Our hacky workaround of Grails' lacking snapshot dependency mechanism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Libify version 1.0 | |
* | |
* Copyright (C) 2012 Viaboxx Systems GmbH | |
* | |
* This is a copy of our base _Events.groovy script that serves in all our Grails plugins/projects. | |
* | |
* It goes into the "scripts" folder of a Grails project. | |
* | |
* NOTE: It requires a maven-ant-tasks-2.1.3.jar in your project, in a folder called "build-lib". | |
* | |
* Its main purpose is to work around Grails" lacking handling of snapshot dependencies. | |
* | |
* See http://www.viaboxxsystems.de/blog for more thoughts about it. | |
* | |
*/ | |
import groovy.xml.NamespaceBuilder | |
/** | |
* Hook into grails clean | |
*/ | |
eventCleanStart = { | |
libifyClean() | |
} | |
/** | |
* Hook into trigger libify | |
*/ | |
eventResolveDependenciesEnd = { | |
libify() | |
} | |
private libify() { | |
File libifyDependenciesFile = new File("libify-dependencies.groovy") | |
if (libifyDependenciesFile.exists()) { | |
ant.echo "[Libify] Found libify definition in $libifyDependenciesFile" | |
def deps = new GroovyShell().evaluate(libifyDependenciesFile) | |
libifyDependencies(deps) | |
} | |
else println "No libify-dependencies.groovy found, so no deps will be added." | |
} | |
/** | |
* Empty the libify directories | |
* @return | |
*/ | |
private libifyClean() { | |
ant.echo "[Libify WARN] eventCleanStart was invoked, (deleting dir "target/libify-temp*")" | |
ant.delete(includeemptydirs: true, failonerror: "false") { | |
fileset(dir: "target", includes: "**/libify-temp*/**") | |
} | |
} | |
def libifyDependencies(dependencies) { | |
// Configure maven ant tasks: | |
ant.path(id: "maven-ant-tasks.classpath", path: "build-lib/maven-ant-tasks-2.1.3.jar") | |
ant.typedef(resource: "org/apache/maven/artifact/ant/antlib.xml", | |
uri: "antlib:org.apache.maven.artifact.ant", | |
classpathref: "maven-ant-tasks.classpath") | |
def mvn = NamespaceBuilder.newInstance(ant, "antlib:org.apache.maven.artifact.ant") | |
// Download dependencies into temp folder: | |
def libifyTempDir = "target/libify-temp" | |
ant.echo "[Libify] Copying maven dependencies into $libifyTempDir" | |
mvn.dependencies(filesetId: "artifacts", versionsId: "dependency.versions") { dependencies.each { dependency(it) } } | |
// Print the artifacts for debugging: | |
// ant.fileScanner { fileset(refid: "artifacts") }.each { println it } | |
ant.copy(todir: libifyTempDir, overwrite: true) { | |
fileset(refid: "artifacts") | |
mapper(classpathref: "maven-ant-tasks.classpath", classname: "org.apache.maven.artifact.ant.VersionMapper", from: "${dependency.versions}", to: "flatten") | |
} | |
ant.echo "[Libify] Adding direct dependencies to classpath: " + libifyDeps | |
// Add dependencies to the classpath: | |
dependencies.each { | |
if (it.scope != "runtime" && it.scope != "provided") { | |
def jar = new File(libifyTempDir, it.artifactId + "." + it.type) | |
rootLoader.addURL(jar.toURL()) | |
} | |
} | |
// Create a space-separated string of file names to be included in the WAR:WEB-INF/lib: | |
def includeDeps = "" | |
dependencies.each { | |
includeDeps += it.artifactId + "." + it.type + " " | |
} | |
// Reconfigure what goes into the WAR file: | |
buildConfig.grails.war.dependencies = { antBuilder -> | |
// add libified files to war/WEB-INF/lib | |
fileset(dir: libifyTempDir, includes: includeDeps) | |
fileset(dir: "${basedir}/lib") { | |
include(name: "*.jar") | |
} | |
grailsSettings.runtimeDependencies?.each { File f -> | |
fileset(dir: f.parent, includes: f.name) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment