Create a Tomcat Installation from Dependencies using Gradle - kercheval/GradleCMPlugin GitHub Wiki

There are sometimes reasons to setup a tomcat instance that can be run explicitly. This gradle file shows how to obtain a specific tomcat release version from maven repositories and explicitly instantiate it so that you can deploy war files to it.

The commented out section in tomcatAssemble is an example of how to create a ROOT context that points to your source (an interesting post regarding this at http://stackoverflow.com/questions/7276989/howto-set-the-context-path-of-a-web-application-in-tomcat-7-0).

//
// This file is responsible for assembling a tomcat reference
// installation that can be used by IDEs outside of the command line.
// This enables dynamic updates to sources and allows internal tools
// and web-ui runs to occur simultaneously.
//
ext {
    tomcatVersion = '7.0.35'
}   

//
// The new configuration here is present to allow a dependency to be 
// set that is used to expand the distribution archives.
//
configurations {
    tomcatDist
}

dependencies {
    // Tomcat binary distribution originating in maven central
    tomcatDist "org.apache:tomcat:${tomcatVersion}"
}

//
// This task actually uses the configuration to obtain the distribution
// and to place the necessary files where we want them.  This does not'
// include the default webapps dir (which should be specified by a seperate
// catalina context file or by copying a war in another task)
//
task tomcatDistCopy(type: Copy) {

    //
    // Use a tarTree to get a file collection from the dependency
    //
    configurations.tomcatDist.findAll { distFile ->
        from tarTree(distFile) 
    }

    //
    // Place into the location we want the tomcat instance at
    //
    into "$rootProject.projectDir/dev/tomcat"

    //
    // include only interesting directories
    //
    [ 'bin', 'conf', 'lib' ].each { subDir -> 
        include "apache-tomcat-${tomcatVersion}/${subDir}/**" 
    }

    // 
    // Remap the distribution to the root of the output directory
    //
    eachFile { details ->
        details.path = details.path.replaceAll('([^/]+)/(.*)', { "${it[2]}" })
    }

    //
    // Don't create empty locations
    //
    includeEmptyDirs = false
}


// Uncomment if using ROOT contexts
// def rootContextFile = file("$rootProject.projectDir/dev/tomcat/conf/Catalina/localhost/ROOT.xml")

task tomcatAssemble(type: Copy, dependsOn: tomcatDistCopy) {

    // Uncomment if using ROOT contexts
    //    outputs.files rootContextFile

    //
    // Here is where the copy of a WAR file to our new tomcat webapp directory should occur.
    // Normally, this will be a WAR file created in another task.
    //

    // This block creates a ROOT context file creation.  Change <yourApp> to the correct location.
    //    doLast {
    //        rootContextFile.parentFile.mkdirs()
    //        rootContextFile.createNewFile()
    //        rootContextFile.write "<Context docBase=\"/opt/mywebapps/<yourApp>\" path=\"\" reloadable=\"true\" />"
    //    }
}


//
// These two tasks are added for clean support for the project
//
task tomcatClean(type: Delete) {
    delete "$rootProject.projectDir/dev/tomcat"
}

clean.dependsOn(tomcatClean)

Using eclipse? You may find these posts helpful...

⚠️ **GitHub.com Fallback** ⚠️