I have declared
sourceSets {
micro {
resources.srcDir file('src/micro/resources')
}
}
but
micro/resources
not copied to ${buildDir}/resources/micro.
I want to add micro sourceSet to classpath. Thereby I can run from eclipse or from gradleConsole.
What am I missing here?
It looks like you might not be using the correct syntax: How do I add a new sourceset to Gradle?
Maybe you could try like this:
sourceSets {
micro{
resources {
srcDir 'src/micro/resources'
}
}
}
Related
How to just override sourceSets between gradle files without copying over all definitions. I want to define all groovy functions to build Bar Application in build-bar.gradle but just inherit everything from build-bar.gradle and override just the sourceSets in build-baz.gradle file. How can we do this?
Bar Application contains this
build-bar.gradle
sourceSets {
main {
java {
exclude 'com/foo/BarApplication.java'
}
}
}
Baz Application contains this
build-baz.gradle
sourceSets {
main {
java {
exclude 'com/foo/BazApplication.java'
}
}
}
In a regular build script you can easily use extensions on Project like Project.sourceSets, for example build.gradle.kts:
sourceSets {
main {
...
}
}
But when I am developing a Gradle plugin in my buildSrc module, I cannot access these. For example:
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.*
class ExamplePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.sourceSets { // error because `sourceSets` can't be resolved.
}
}
}
This is happening despite including the kotlin-gradle-plugin module in my buildSrc dependencies:
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
}
So, how can I access these extensions from within my Gradle plugin?
class ExamplePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.configure<JavaPluginExtension> {
sourceSets {
println(names)
}
}
}
}
See additional notes here: https://docs.gradle.org/current/userguide/kotlin_dsl.html#project_extensions_and_conventions
Basically for plugins, or other times when the plugins applied are not known, the accessors (sourceSets, configurations, etc) of extensions added by other plugins will need to go through a method call which sort of 'retrieves' that scope or object. Further down the link there is also an example of how to get tasks created by other plugins:
val test by target.tasks.existing(Test::class)
test.configure { useJUnitPlatform() }
// or
val test by target.tasks.existing(Test::class) {
useJUnitPlatform()
}
note that if the 'sourceSet' object does not exist on the project (because the java plugin was not applied), an exception will be thrown .
tested with gradle version 7.2, kotlin-dsl version 2.1.6
Sorry, if this isn't enough information, new to gradle, let me know if more is needed.
I have a log4j2.xml in a config folder that I define in a gradle.properties file.
project.ext.logConfigFile="./config/log4j2.xml"
The structure of my project
project
src
test
lib
config
log4j2.xml
Now when I run gradle test, it says that it can't find the log4j2 configuration.
This is what my build.gradle file looks like..
sourceSets {
test {
java {
srcDirs = ["test"]
}
}
main {
java {
srcDirs = ["src"]
}
}
}
test {
testLogging {
outputs.upToDateWhen {false}
showStandardStreams = true
events "PASSED", "STARTED", "FAILED", "SKIPPED"
afterSuite { desc, result ->
if (!desc.parent) { // will match the outermost suite
println "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
}
}
}
}
Been dinking around with things I found on the internet, that's why all those statements in the "testLogging" part. But basically, I have LOGGER.info() statements that aren't coming up because it defaults to errors only.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Can I tell gradle test exactly where to go for my log4j2.xml? Something like this? Though I don't think this works....
tasks.withType(Test) {
systemProperty 'log4j.configuration', logConfigFile
}
A little stuck right now. Thanks!
Just put it in src/test/resources. That comes first on the classpath during testing. Then configure log4j to load from classpath, not a file path; I think that’s the default behavior anyway
Believe I solved the problem, you just define where the resources are in the sourceSets also. It will look there for the log4j2.xml.
sourceSets {
test {
java {
srcDirs = ["test"]
}
resources {
srcDirs = ["config"]
}
}
main {
java {
srcDirs = ["src"]
}
}
}
I have a distributes projects with different sub-projects and I want to accomplish the following:
(root)
client
module A
module B
module C
model
I want to put
protoc {
artifact = 'com.google.protobuf:protoc:3.5.0'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.7.0"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
} }
dependencies {
compile "com.google.api.grpc:proto-google-common-protos:1.0.0"
compile "io.grpc:grpc-netty:1.7.0"
compile "io.grpc:grpc-protobuf:1.7.0"
compile "io.grpc:grpc-stub:1.7.0"
}
for module A, B and C.
For now I have the following in my root build.gradle
subprojects{
apply plugin: 'java'
sourceCompatibility = 1.8
group 'project'
version '0.0.1-SNAPSHOT'
jar {
manifest {
attributes 'Main-Class': "com.project.${project.name}.App"
}
doFirst {
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
}
}
So every sub-project use java plugin, and has the defines dependencies and jar task.
How can I only put the first block for some sub-projects ?
I tried using a variable like in Multi-project Builds - Gradle but I couldn't access it in subprojects block.
Thank you in advance. I'm really interested in using Gradle correctly and it's a bit hard to get into it outside of simple Android/Java projects. Feel free to include any documentations I should read :)
Edit:
Thank you. I wouldn't have posted here if I hadn't search before. Apparently I was missing the keyword "subset" who would have gave me the solution you linked.
A solution is described here: https://discuss.gradle.org/t/configure-only-subset-of-subprojects/5379/3
You can run configure() with a list of projects.
project.ext {
subprojectList = subprojects.findAll{
it.name == 'subprojectA' ||
it.name == 'subprojectB' ||
it.name == 'subprojectC'
}
}
configure(project.subprojectList) {
// insert your custom configuration code
}
or
configure([project(':a'), project(':b'), project(':c')]) {
// configuration
}
My project has both Java (N files) and Groovy code (1 file only). Java compile depends upon this single Groovy file's class file for Java compilation (compileJava task to succeed).
When I don't use src/java as one of the srcDir within main>groovy> sourceSet section, then I get an error saying class/symbol not found which is in the groovy file/class. In ANT, it's easy that we are calling compile-groovy target first, before calling compile-java target but the same in Gradle is what I'm trying to find.
I read some posts and found that if I make main>java section NULL and specify srcDir for main>java which is src/java inside main>groovy sourceSet section, then it compiles fine.
My ?s:
1. Is there any other way to do? for ex, the following should work:
compileJava {
dependsOn compileGroovy
}
though, this goes to an infinte loop.
OR
what about using doFirst for compileJava task:
compileJava {
doFirst {
compileGroovy
}
}
this doesn't work either.
build.gradle This works, but compileJava in one sense becomes useless here even though the source code has N no. of java files in the src/java or src/java-test etc tree. I know this build script is working but logically it might bring some confusion to the developer if s/he is not familiar why sourceSet for Groovy MUST have "src/java" as its srcDir value.
apply plugin: 'java'
apply plugin: 'groovy'
sourceSets {
main {
groovy {
srcDir 'src/groovy'
srcDir 'src/java'
}
java {
//The following needs to be commented out OR Gradle will always pick compileJava before compileGroovy
//srcDir 'src/java'
//srcDir 'src/java-test'
}
}
test {
groovy {
srcDir 'test/groovy'
}
java {
srcDir 'test/java'
}
resources {
srcDir 'test/resources'
srcDir 'conf'
}
}
integrationTest {
groovy {
srcDir 'src/groovy-test'
}
java {
srcDir 'src/java-test'
}
resources {
srcDir 'test/resources'
srcDir 'conf'
}
}
}
Other links:
How to make Gradle compile Groovy tests before Java tests
The Groovy (base) plugin makes GroovyCompile tasks depend on the corresponding JavaCompile tasks because it's more common to call from Groovy into Java than the other way around. If you need it the other way around (or both ways), joint compilation is a good solution. Here is a somewhat improved (over your version) joint compilation setup:
sourceSets {
main {
groovy {
// override the default locations, rather than adding additional ones
srcDirs = ['src/groovy', 'src/java']
}
java {
srcDirs = [] // don't compile Java code twice
}
}
}
If you prefer separate compilation with Java->Groovy dependencies only, something like the following should work:
// since you aren't using the default locations
sourceSets {
main {
groovy {
srcDirs = ['src/groovy']
}
java {
srcDirs = ['src/java']
}
}
}
// remove GroovyCompile->JavaCompile task dependencies
tasks.withType(GroovyCompile) {
dependsOn = []
}
// add JavaCompile->GroovyCompile task dependencies
tasks.withType(JavaCompile) { task ->
dependsOn task.name.replace("Java", "Groovy")
}
Because a JavaCompile task and its corresponding GroovyCompile task write to the same output directory, Java compilation will now have the compiled Groovy code on its compile class path.
PS: Calling a task from another task is not supported, and bad things can happen if you try. Instead, you should always work with task relationships (dependsOn, finalizedBy, mustRunAfter, shouldRunAfter).