Skip to content

Instantly share code, notes, and snippets.

@milo-minderbinder
Last active January 19, 2022 10:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milo-minderbinder/88d0cec2259ddfa6eb504d17be428994 to your computer and use it in GitHub Desktop.
Save milo-minderbinder/88d0cec2259ddfa6eb504d17be428994 to your computer and use it in GitHub Desktop.
/**
* MIT License
*
* Copyright (c) 2018 Chris Passarello <www.insecurity.co>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* This init script modifies the Gradle project by adding a new task to all projects, `copyDependencies`,
* which can be used to copy all of the projects resolvable dependencies into a directory.
*
* To add it dynamically with a Gradle build command, add the `-I` option with the path to this file, e.g.:
* > gradle -I /path/to/copyDependencies.gradle clean copyDependencies
*
* Alternatively, to add the plugin automatically to all Gradle projects transparently, place this script
* in your `GRADLE_HOME/init.d/` directory.
*/
LogLevel LOG_LEVEL = LogLevel.DEBUG
logger.log(LOG_LEVEL, 'copyDependencies.gradle - initscript')
allprojects {
task copyDependencies {
group 'Security'
ext {
// Directory into which the dependencies will be copied
//example: file(new File(rootProject.buildDir, "dependencies/${project.name}"))
outputDir = file(new File(rootProject.buildDir, 'dependencies'))
// Configurations to copy dependencies from (Default: all configurations)
// example: ['compile', 'providedCompile', 'runtime']
includeConfigurations = null
// Configurations to exclude copying dependencies from
// example: project.configurations.names.findAll { it.startsWith('test') }
excludeConfigurations = null
// Closure to use to further filter dependencies invidually
// example: { Dependency d -> !d.group.startsWith('com.oath') }
dependencyFilter = { Dependency d -> true } // Don't filter individual dependencies by default
}
afterEvaluate { p ->
p.copyDependencies {
includeConfigurations = includeConfigurations ?: project.configurations.names
excludeConfigurations = excludeConfigurations ?: project.configurations.names.findAll { String name ->
name.startsWith('test') || name in ['pmd']
} as Set<String>
Set<String> invalidConfigs = (includeConfigurations + excludeConfigurations) - project.configurations.names
if (invalidConfigs)
logger.warn "copyDependencies.gradle - includeConfigurations & excludeConfigurations contain invalid name(s): ${invalidConfigs}"
Set<String> filteredConfigs = includeConfigurations - excludeConfigurations
logger.log(LOG_LEVEL, "copyDependencies.gradle - Included configurations:\n\t{}",
filteredConfigs.join('\n\t'))
logger.log(LOG_LEVEL, "copyDependencies.gradle - Excluded configurations:\n\t{}",
(project.configurations.names - filteredConfigs).join('\n\t'))
if (filteredConfigs.containsAll(project.configurations.names))
description "Copies all dependencies into ${outputDir}"
else
description "Copies ${filteredConfigs} dependencies into ${outputDir}"
outputs.dir outputDir
doLast {
Set<File> filteredDependencies = p.configurations.findAll { c ->
boolean canBeResolved = c.metaClass.respondsTo(c, 'isCanBeResolved') ? c.isCanBeResolved() : true
canBeResolved && (c.name in filteredConfigs)
}.collectMany { Configuration c ->
c.files { Dependency d ->
dependencyFilter(d)
}
}
logger.log(LOG_LEVEL, "copyDependencies.gradle - Copying dependencies into {}:\n\t{}",
rootDir.toPath().relativize(outputDir.toPath()),
filteredDependencies.collect { it.name }.join('\n\t'))
copy {
from filteredDependencies
into outputDir
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment