Gradle - Configuration to exclude modules from all "except" - gradle

I have the following configuration in my root build.gradle file:
subprojects {
configuration {
all {
exclude module: 'slf4j-log4j12'
exclude module: 'slf4j-log4j-impl'
}
}
}
I want the configuration applied for all sub-projects, except for one. Is there a way to disable the configuration just for one sub-project?

You can select all but one like this . this should apply for all sub projects but the one you want .
//for all sub projects
subprojects {
if (it.name != 'project name') {
//do something
}
}

Related

Is there a way to include a specific module from a group while excluding rest in gradle?

I am using a library which has a transitive dependency on a module lets assume "abc.xyz:abc-module:1.1.1", the problem is, however, all of the modules from that group are excluded in my build.gradle for some reason using
configurations {
compile.exclude group: "abc.xyz"
}
It causes that transitive dependency to be ignored as expected. Is there a way I can specify only to include abc-module while excluding the remaining one as previously?
I think you should be able to do what you want with a component selection rule like
configurations {
compile {
resolutionStrategy {
componentSelection {
all { ComponentSelection selection ->
if (selection.candidate.group == 'abc.xyz' && selection.candidate.module != 'abc-module') {
selection.reject('Dependencies from group "abc.xyz" except of "abc-module" are not allowed.')
}
}
}
}
}
}

Ignore generated resources from Spotless

My Project is using spotless plugin. I need to ignore java files from the generated-resources directory. How to do the same.
This is how I am using the plugin.
apply plugin: "com.diffplug.gradle.spotless"
spotless {
lineEndings = 'unix';
java {
eclipseFormatFile "eclipse-java-google-style.xml"
}
}
sourceSets has generated-resources directory included which I do not want to remove.
You can specify a target for the spotless formatter which allows includes and excludes.
I use the following in the top-level build.gradle in a multi-project build where all Java code resides in subdirectories under the modules directory:
subprojects {
...
spotless {
java {
target project.fileTree(project.rootDir) {
include '**/*.java'
exclude 'modules/*/generated/**/*.*'
}
googleJavaFormat()
}
}
...
}

How to specify output.classesDir for custom sourceSet in Gradle?

My build uses source code from two projects: ProjectA and ProjectB, and produces JAR with classes and resources from ProjectB. I defined custom sourceSet mainProjectB which is supposed to have output in a separate directory:
sourceSets {
mainProjectB {
output.classesDir = "$buildDir/build/classes/projectB"
output.resourcesDir = "$buildDir/build/resources/projectB"
java { srcDirs = ['src/main/java']}
resources { srcDirs = ['src/main/resources']}
}
mainProjectA {
java { srcDirs = [
'../projectA/src/main/java'
]}
resources { srcDirs = [
'../projectA/src/main/resources'
]}
}
test {
java {
srcDirs = [
'../projectA/src/test/java',
'src/test/java'
]}
resources {
srcDirs = [
'../projectA/src/test/resources',
'src/test/resources'
]}
}
}
compileJava {
source sourceSets.mainProjectB.allJava
source sourceSets.mainProjectA.allJava
}
processResources {
from sourceSets.mainProjectB.resources
from sourceSets.mainProjectA.resources
}
jar {
from sourceSets.mainProjectB.output.classesDir
from sourceSets.mainProjectB.output.resourcesDir
}
Problem: custom sourceSet mainProjectB ingores specified output directories.
The directories "$buildDir/build/classes/projectB" and "$buildDir/build/resources/projectB" are not created, and as a consequence, JAR includes files from both projects (instead of ProjectB).
UPDATE:
Projects A and B have circular dependencies. That is why they have to share source code.
I would consider to use subprojects and project to achieve your goal - gradel docs . With the following approach you can get any kind of jar file depending on your build :
group 'CoreProject'
version '1.0-SNAPSHOT'
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
project (':projectA') {
}
project (':projectB') {
def generatedResources = "$buildDir"
//in case you want resources and classes to be written to custom location where
//redefined paths are relative to projectB root folder
sourceSets {
main {
output.classesDir = 'build/classes/projectB'
output.resourcesDir = 'build/resources/projectB'
}
}
dependencies {
compile project(':projectA')
}
jar {
manifest.mainAttributes(
'Main-Class': "ProjectBClass"
)
}
//To create fat Jar that will contain classes and resources from all dependencies
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "ProjectAResource" //if want to exclude resources from projectA
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
}
If you run jar task of projectB following jars will be created, each including only its own classes and resources : projectA/build/libs/projectA.jar , projectB/build/libs/projectB.jar('JAR with classes and resources from ProjectB' as you asked in your comment)
If you run farJar task of projectB the following jar file will be created that includes classes and resources from both projects and you can exclude any files patterns from projectA and projectB projects to create any final jar you like : projectB/build/libs/projectB-all.jar
Here is a screenshot of projects folders structure I created to mimic your scenario(as I understood it):
P.S. Also make sure none of the folders projectB/build and projectA/build are locked by any process and remove those handles if any, as otherwise Gradle will fail to run.

Configuration for projects in subdirectory

I have a Gradle multi-project build that looks like this:
rootProject
build.gradle
settings.gradle
shared/
SharedLib
SharedLib2
plugins/
FirstPlugin
SecondPlugin
I'd like to add all projects in the directory shared as dependencies to all projects in plugins.
More generally speaking: How do I configure subprojects by directory?
Contents of settings.gradle:
include 'plugins:FirstPlugin', 'plugins:SecondPlugin', 'shared:SharedLib', 'shared:SharedLib2'
Contents of build.gradle:
task wrapper(type: Wrapper) { gradleVersion = "2.1" }
allprojects{ apply plugin:"java" }
subprojects {
group = "eu.test.myGroup"
repositories { mavenCentral() }
dependencies { testCompile "junit:junit:4.11" }
}
Adding this to build.gradle does what I wanted to achieve:
// Define what's a plugin and what's a shared library by directory paths.
// Ignore empty projects
def plugins = subprojects.findAll{ it.path.contains("plugins") && hasSrc(it) }
def sharedLibs = subprojects.findAll{ it.path.contains("shared") && hasSrc(it)}
/** Checks whether a project has a source directory */
def hasSrc(proj){
return new File(proj.projectDir, "src").exists()
}
// Configure the plugins to depend on the shared libraries at compile time
configure(plugins) { dependencies { sharedLibs.each{compile it} } }

Is it possible to customise the source sets used by the Gradle license plugin?

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.6.0'
}
}
apply plugin: 'license'
license {
sourceSets {
main {
java {
exclude '**'
}
test {
exclude '**'
}
}
}
mapping {
javascript='JAVADOC_STYLE'
xml='XML_STYLE'
xsl='XML_STYLE'
html='XML_STYLE'
Rptdesign='XML_STYLE'
}
}
I have read that by default license is added to all sourceSets created by Java Plugin.to customise sourceSets - when i add sourceSets license is not adding to .java files,when i remove sourceSets block license is updating all .java,.groovy files in src/main/ test & java directiries but i have to add license for specfic files like .xml,.xsl at root directory and some specific java files like which starts with s*.java, can any one helpme.
I had the same problem and I found a solution here : https://github.com/hierynomus/license-gradle-plugin/issues/9
You can add the following to your build.gradle and then the license plugin skips the excluded files.
import nl.javadude.gradle.plugins.license.License
tasks.withType(License).each { licenseTask ->
licenseTask.exclude '/*.json'
licenseTask.exclude '/*.properties'
}
( and remove license { sourceSets { ... } })

Resources