Skip to content

Instantly share code, notes, and snippets.

@slavashvets
Created March 13, 2015 07:49
Show Gist options
  • Save slavashvets/add44d424e0d7d7cf43c to your computer and use it in GitHub Desktop.
Save slavashvets/add44d424e0d7d7cf43c to your computer and use it in GitHub Desktop.

Revisions

  1. slavashvets created this gist Mar 13, 2015.
    77 changes: 77 additions & 0 deletions inheritStaticContent.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    /**
    Inherit Static Content
    ======================
    Description:
    1. Extend output webapp with static folder (static resources) from
    depended projects and external modules
    2. Used for `runtime` first-level dependencies (not transitively)
    3. Works for WAR plugin (built-in) and for Gretty plugin (third party)
    Usage:
    1. Apply this plugin after applying WAR and Gretty plugin
    2. Add a dependency to `runtime` configuration (`compile` configuration
    will work also because `runtime` extends it)
    Example:
    apply from: "${rootDir}/gradle/inheritStaticContent.gradle"
    dependencies {
    compile project(':some-project-with-static-files')
    runtime 'com.test.plugins:some-plugin-with-static-files:1.0.10'
    }
    */
    afterEvaluate {
    configurations.runtime {
    gradle.taskGraph.whenReady { taskGraph ->
    def declaredExternal = incoming.dependencies
    .withType(ExternalDependency)
    .collect { id ->
    "${id.group}:${id.name}:${id.version}"
    }
    allDependencies.withType(ProjectDependency)*.dependencyProject.sourceSets.main.resources.srcDirs*.each { dir ->
    def staticContentDir = new File(dir, 'static')
    if (!staticContentDir.exists()) {
    return
    }
    if (taskGraph.hasTask(war)) {
    war.from staticContentDir
    }
    if (taskGraph.hasTask(prepareInplaceWebApp)) {
    gretty.extraResourceBase staticContentDir
    }
    }
    resolvedConfiguration.resolvedArtifacts.each { artifact ->
    if ("${artifact.moduleVersion.id}" in declaredExternal) {
    if (taskGraph.hasTask(war)) {
    war {
    from zipTree(artifact.file).matching {
    include 'static/'
    }
    eachFile { // waiting for GRADLE-3025
    if (it.path.startsWith('static/*')) {
    it.path = it.relativePath.segments.tail().join('/')
    }
    }
    includeEmptyDirs = false
    }
    }
    if (taskGraph.hasTask(prepareInplaceWebApp)) {
    prepareInplaceWebApp << {
    zipTree(artifact.file).matching { include "static/" }.files
    }
    def artifactName = artifact.file.name
    def artifactMD5 = HashUtil.createCompactMD5(artifact.file.absolutePath)
    def staticDir = new File("${buildDir}/tmp/expandedArchives/${artifactName}_${artifactMD5}/static")
    gretty.extraResourceBase staticDir
    }
    }
    }
    }
    }
    }

    import org.gradle.internal.hash.HashUtil
    import org.gradle.util.GFileUtils