I would like to know if there is a property that returns a SourceSet full path string or file directory, something like ${project.projectDir}/src/${sourceSet.name}.
apply plugin: 'java'
sourceSets {
foo {
java {
srcDir 'example/dir/java'
}
}
}
// For example, this could return pathToProject/src/example/dir
println sourceSets.foo.srcDir
Try this:
productFlavors {
admin { }
customer { }
}
sourceSets {
main {
java.srcDirs = ['src/main']
//other typical sourceSets stuff
}
admin.java.srcDirs = ['src/admin']
customer.java.srcDirs = ['src/customer']
}
Related
How can I add the output of a task to a SourceSet. My goal is that the task will implicitly be executed before compileGenJava-task.
sourceSets {
gen {
java {
srcDir "${buildDir}/generated-sources/markup2pojo" // equals output directory of generateSources
}
}
[...]
see https://docs.gradle.org/current/userguide/java_plugin.html#sec:changing_java_project_layout
if you have task that generates code before compile ; you can add the generated folder path ex: build/gensrc
sourceSets {
main {
java {
srcDirs = ['src/java', 'build/gensrc']
}
resources {
srcDirs = ['src/resources']
}
}
}
Consider this toy build.gradle file:
plugins {
id "java"
}
import org.gradle.internal.os.OperatingSystem;
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
def pathExists(pathname) {
// Returns true iff pathame is an existing file or directory
try {
// This may throw an error for a Windows pathname, c:/path/to/thing
if (file(pathname).exists()) {
return true;
}
} catch (GradleException e) {
// I don't care
}
if (OperatingSystem.current().isWindows()) {
try {
// If we're on Windows, try to make c:/path/to/thing work
if (file("file:///${pathname}").exists()) {
return true;
}
} catch (GradleException e) {
// I don't care
}
}
return false
}
def someVariable = "absent"
if (pathExists("/tmp")) {
someVariable = "present"
}
task someCommonTask() {
doLast {
println("Did some setup stuff: ${someVariable}")
}
}
task someATask(dependsOn: ["someCommonTask"]) {
doLast {
println("A: ${someVariable}")
}
}
task someBTask(dependsOn: ["someCommonTask"]) {
def otherVariable = someVariable == "absent" || pathExists("/etc")
doLast {
println("B: ${otherVariable}")
}
}
Is it possible to reorganize this build file so that someATask is in a.gradle and someBTask is in b.gradle? I've made some attempts to use apply from: without success and, if the answer is on this page: https://docs.gradle.org/current/userguide/organizing_gradle_projects.html it eludes me.
In the real project, there would be a couple of dozen tasks in each of those subordinate files and the goal of dividing up the build.gradle file isn't to have them be subprojcts, per se, they're just logical groupings of tasks.
This split can be done using apply from: ... to separate the files.
E.g.
build.gradle:
plugins {
id "java"
}
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
ext {
someVariable = "absent" // XXX
}
apply from: "setup.gradle" // XXX
apply from: "common.gradle" // XXX
task someATask(dependsOn: ["someCommonTask"]) {
doLast {
println("A: ${someVariable}")
}
}
setup.gradle:
task setup() {
someVariable = "present"
}
common.gradle:
task someCommonTask(dependsOn: ["setup"]) {
doLast {
println("Did some setup stuff: ${someVariable}")
}
}
# gradle someATask
> Task :someCommonTask
Did some setup stuff: present
> Task :someATask
A: present
BUILD SUCCESSFUL in 403ms
My guess, the problems you are facing are with toplevel code and not
using properties.
I want to setting a new dir for compiled java class file when use gradle.
But it never work.
apply plugin: 'java'
//apply plugin: 'war'
sourceSets {
main {
java {
srcDirs = ['src/']
outputDir = 'WebContent/WEB-INF/classes/'
}
resources {
srcDirs = [ 'src/' ]
}
}
}
For gradle > 4. Note for type of the value for outputDir property. It should be File. Not String like in your question's code:
sourceSets {
main {
output.resourcesDir = file('out/res')
java.outputDir = file('out/bin')
}
}
see
https://docs.gradle.org/current/javadoc/org/gradle/api/file/SourceDirectorySet.html#setOutputDir(java.io.File)
and
https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/SourceSetOutput.html
Before version 4 gradle was using single output directory for different jvm languages.
So, for gradle < 4:
sourceSets {
main {
output.resourcesDir = file('out/res')
output.classesDir = file('out/bin')
}
}
see https://docs.gradle.org/3.3/javadoc/org/gradle/api/tasks/SourceSetOutput.html.
I have the following gradle.properties
version=2.1
paths=['/home/desk/hie', '/home/mydesk1/hai1', '/home/mydesk2/hai2']
sources=['/src/path/impl', '/src/path/src']
I need to access these paths and sources form gradle.properties to build.gradle
sourceSets {
main {
java {
srcDir=${paths}
}
}
}
but ${paths} is not working.
Can you someone help to get out of this issue and how to use those list in build.gradle file
Consider the following, which converts the paths String to an Iterable (that is, a List):
apply plugin: 'java'
def pathsList = Eval.me(project.ext.paths)
sourceSets {
main {
java {
srcDirs = pathsList
}
}
}
You can share the value using gradle object:
// settings.gradle
gradle.ext {
paths = ['/home/desk/hie', '/home/mydesk1/hai1', '/home/mydesk2/hai2']
}
// build.gradle
sourceSets {
main {
java {
srcDir = paths
}
}
}
I have a file that contains list of paths paths.list
current/path/to/hai
current/path/to/hai2
path/to/hai3
path/to/hai4
I want to read this file and place these paths in srcDirs list
sourceSets {
main {
java {
srcDir = ['current/path/to/hai','current/path/to/hai2','path/to/hai3','path/to/hai4']
}
}
}
I have several paths to be added in the list
How to read these paths from list so that they get reflected in scrDir list.
apply plugin: 'java'
def srcDirs = file('paths.list').text.readLines()
srcDirs.each { srcDirectory ->
sourceSets {
main {
java {
srcDir srcDirectory
}
}
}
}