how can I override source dirs in gradle BUT only for this one annoying subproject - gradle

We have one main project and two subprojects. One of the subprojects is the playframework which has a "unique" build structure. How can I override the source directories BUT only for that one subproject such that all other projects are using the standard layout of source directories src/main/java, etc.
I tried the first answer which is not working and for my directory structure
stserver
build.gradle (1)
project1
webserver
build.gradle (2)
The 2nd gradle file is this
sourceSets.main{
java.srcDirs = ['app']
}
task build << {
println "source sets=$sourceSets.main.java.srcDirs"
}
When I run this, it prints out stserver/app as my srcDir instead of stserver/webserver/app???? What am I doing wrong here?
thanks,
Dean

Please have a look at the docs Peter has suggested. I have a ready build.gradle that I have working with Play Framework 2.0~, so I'll share it here in hope you'll find some useful setup tips.
My project structure:
+- master/
+- build.gradle <-- contains common setup,
and applies 'java' plugin to all subprojects
+- ui/ <-- project using Play framework
+- build.gradle <-- excerpt from this file is posted below
The excerpt from build.gradle
repositories{
maven{
//Play dependencies will be downloaded from here
url " http://repo.typesafe.com/typesafe/releases"
}
}
//Optional but useful. Finds 'play' executable from user's PATH
def findPlay20(){
def pathEnvName = ['PATH', 'Path'].find{ System.getenv()[it] != null }
for(path in System.getenv()[pathEnvName].split(File.pathSeparator)){
for(playExec in ['play.bat', 'play', 'play.sh']){
if(new File(path, playExec).exists()){
project.ext.playHome = path
project.ext.playExec = new File(path, playExec)
return
}
}
}
throw new RuntimeException("""'play' command was not found in PATH.
Make sure you have Play Framework 2.0 installed and in your path""")
}
findPlay20()
configurations{
//Configuration to hold all Play dependencies
providedPlay
}
dependencies{
providedPlay "play:play_2.9.1:2.0+"
//Eclipse cannot compile Play template, so you have to tell it to
//look for play-compiled classes
providedPlay files('target/scala-2.9.1/classes_managed')
//other dependencies
}
sourceSets.main{
java.srcDirs = ['app', 'target/scala-2.9.1/src_managed/main']
//Make sure Play libraries are visible during compilation
compileClasspath += configurations.providedPlay
}
//This task will copy your project dependencies (if any) to 'lib'
//folder, which Play automatically includes in its compilation classpath
task copyPlayLibs(type: Copy){
doFirst { delete 'lib' }
from configurations.compile
into 'lib'
}
//Sets up common play tasks to be accessible from gradle.
//Can be useful if you use gradle in a continuous integration
//environment like Jenkins.
//
//'play compile' becomes 'gradle playCompile'
//'play run' becomes 'gradle playRun', and so on.
[ ['run', [copyPlayLibs]],
['compile', [copyPlayLibs]],
['clean', []],
['test', []],
['doc', [copyPlayLibs]],
['stage', [copyPlayLibs]] ].each { cmdSpec ->
def playCommand = cmdSpec[0]
def depTasks = cmdSpec[1]
task "play${playCommand.capitalize()}" (type: Exec,
dependsOn: depTasks,
description: "Execute 'play ${playCommand}'") {
commandLine playExec, playCommand
}
}
//Interate playClean and playCompile task into standard
//gradle build cycle
clean.dependsOn "playClean"
[compileScala, compileJava]*.dependsOn "playCompile"
//Include Play libraries in Eclipse classpath
eclipse {
classpath {
plusConfigurations += configurations.providedPlay
}
}
Note: I have just extracted the above from an existing bigger gradle file, so it might be missing some things, so no guarantees:) Hope it's useful anyway. Good luck.

Related

How to create a custom task in gradle to pack java and kotlin code to a jar?

We have a multi modular setup and we are sharing some tests classes between the modules (mainly Fakes implementations). Our current solution (that you can find below) works just for classes written in Java, but we are looking at supporting also shared kotlin classes.
if (isAndroidLibrary()) {
task compileTestCommonJar(type: JavaCompile) {
classpath = compileDebugUnitTestJavaWithJavac.classpath
source sourceSets.testShared.java.srcDirs
destinationDir = file('build/testCommon')
}
taskToDependOn = compileDebugUnitTestSources
} else {
task compileTestCommonJar(type: JavaCompile) {
classpath = compileTestJava.classpath
source sourceSets.testShared.java.srcDirs
destinationDir = file('build/testCommon')
}
taskToDependOn = testClasses
}
task testJar(type: Jar, dependsOn: taskToDependOn) {
classifier = 'tests'
from compileTestCommonJar.outputs
}
How can I modify the compileTestCommonJar so it supports kotlin?
Here is what we do:
In the module with shared test classes, pack the test source set output into a jar
configurations { tests }
...
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
from sourceSets.test.output
}
artifacts { tests testJar }
In a module that depends on the shared classes
dependencies {
testCompile project(path: ":my-project-with-shared-test-classes", configuration: "tests")
}
PS: Honestly, I would prefer to have a separate Gradle module with common test classes as it's more explicit solution.
The task compileTestCommonJar(type: JavaCompile) compiles .java files only because its the task of JavaCompile type.
There's KotlinCompile task aswell, so you would need to merge it, it basically works similary to JavaCompile but compiles .kt files only.
Said that i wouldn't use task system to share the dependencies across, i would use separate module and work with default compileTestKotlin and compileTestJava task's outputs

Using War plugin but without creating an archive

I'm trying to get Gradle to handle the deployment of a very large Web application. In the past when we used Ant, instead of creating a very large .war file, we would simply assemble all the code in one folder--libraries, .jsp's etc--and then scp them to the deployment destination. This would speed deployment since we would be moving only the files that changed.
I'm having trouble trying to do this with Gradle, however. Using the War plugin creates an actual .war file, which we don't want. I've tried simply creating a task that depends on 'classes' and that generates the necessary classes and resources folders. However, where are the library dependencies? How can I get this all in one place so I can do an scp?
Current build:
apply plugin: "java"
apply plugin: "maven"
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.6
targetCompatibility = 1.6
repositories {
maven {
url 'http://buildserver/artifactory/repo'
}
}
sourceSets {
main {
java {
srcDir 'src'
}
resources {
srcDir 'src'
}
}
}
webAppDirName = 'web'
dependencies {
compile 'antlr:antlr:2.7.6'
compile 'antlr:antlr:2.7.7'
*** etc ***
}
task deploystage(dependsOn: 'classes') << {
println 'assemble and scp code here'
}
your right Opal ... no easynway to use the War plugin for this ... although it would be nice if it had a createExplodedWar option. I just used the Jar plugin methods.
The .jars were available, along with other things in war.classpath.files:
gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
project.ext.set("allclasspathjars", files(war.classpath.files))
}
I then when through these to separate out the .jars from the other stuff:
task deployJars(dependsOn: 'classes') << {
FileUtils.cleanDirectory(new File("web/WEB-INF/lib/"))
project.allclasspathjars.each {File file ->
if(file.name.endsWith(".jar")) {
println "Including .jar: " + file.name
org.apache.commons.io.FileUtils.copyFile(file, new File("web/WEB-INF/lib/" + file.name))
}
}
After this I could copy all the classes & resources into the .war structure:
task createExplodedWar(type: Copy, dependsOn: 'deployJars') {
from 'build/classes/main'
from 'build/resources/main'
into 'web/WEB-INF/classes'
}
Finally used Gradle SSH Plugin to push it up to tomcat and restart the server.
All good.

How to run JBoss TattleTale from inside Gradle build

I am in love with JBoss TattleTale. Typically, in my Ant builds, I follow the docs to define the Tattletale tasks and then run them like so:
<taskdef name="report"
classname="org.jboss.tattletale.ant.ReportTask"
classpathref="tattletale.lib.path.id"/>
...
<tattletale:report source="${src.dir]" destination="${dest.dir}"/>
I am now converting my builds over to Gradle and am struggling to figure out how to get Tattletale running in Gradle. There doesn't appear to be a Gradle-Tattletale plugin, and I'm not experienced enough with Gradle to contribute one. But I also know that Gradle can run any Ant plugin and can also executing stuff from the system shell; I'm just not sure how to do this in Gradle because there aren't any docs on this (yet).
So I ask: How do I run the Tattletale ReportTask from inside a Gradle build?
Update
Here is what the Gradle/Ant docs show as an example:
task loadfile << {
def files = file('../antLoadfileResources').listFiles().sort()
files.each { File file ->
if (file.isFile()) {
ant.loadfile(srcFile: file, property: file.name)
println " *** $file.name ***"
println "${ant.properties[file.name]}"
}
}
}
However, no where in here do I see how/where to customize this for Tattletale and its ReportTask.
The following is adapted from https://github.com/roguePanda/tycho-gen/blob/master/build.gradle
It bypasses ant and directly invokes the Tattletale Java class.
It was changed to process a WAR, and mandates a newer javassist in order to handle Java 8 features such as lambdas.
configurations {
tattletale
}
configurations.tattletale {
resolutionStrategy {
force 'org.javassist:javassist:3.20.0-GA'
}
}
dependencies {
// other dependencies here...
tattletale "org.jboss.tattletale:tattletale:1.2.0.Beta2"
}
task createTattletaleProperties {
ext.props = [reports:"*", enableDot:"true"]
ext.destFile = new File(buildDir, "tattletale.properties")
inputs.properties props
outputs.file destFile
doLast {
def properties = new Properties()
properties.putAll(props)
destFile.withOutputStream { os ->
properties.store(os, null)
}
}
}
task tattletale(type: JavaExec, dependsOn: [createTattletaleProperties, war]) {
ext.outputDir = new File(buildDir, "reports/tattletale")
outputs.dir outputDir
inputs.files configurations.runtime.files
inputs.file war.archivePath
doFirst {
outputDir.mkdirs()
}
main = "org.jboss.tattletale.Main"
classpath = configurations.tattletale
systemProperties "jboss-tattletale.properties": createTattletaleProperties.destFile
args([configurations.runtime.files, war.archivePath].flatten().join("#"))
args outputDir
}
The previous answers either are incomplete or excessively complicated. What I did was use the ant task from gradle which works fine. Let's assume your tattletale jars are beneath rootDir/tools/...
ant.taskdef(name: "tattleTaleTask", classname: "org.jboss.tattletale.ant.ReportTask", classpath: "${rootDir}/tools/tattletale-1.1.2.Final/tattletale-ant.jar:${rootDir}/tools/tattletale-1.1.2.Final/tattletale.jar:${rootDir}/tools/tattletale-1.1.2.Final/javassist.jar")
sources = "./src:./src2:./etcetera"
ant.tattleTaleTask(
source: sources,
destination: "tattleTaleReport",
classloader: "org.jboss.tattletale.reporting.classloader.NoopClassLoaderStructure",
profiles: "java5, java6",
reports: "*",
excludes: "notthisjar.jar,notthisjareither.jar,etcetera.jar"
){
}
So the above code will generate the report beneath ./tattleTaleReport. It's that simple. The annoyance is that the source variable only accepts directories so if there are jars present in those directories you do not wish to scan you need to add them to the excludes parameter.

Can a Gradle plugin modify the list of subprojects in a multi-module project?

I've hacked together combination of build.gradle and settings.gradle below for creating an ad-hoc multi-module project out of several single-module projects (e.g., an application and all of its dependencies, or a shared library and everything that uses that library).
settings.gradle:
// find all subprojects and include them
rootDir.eachFileRecurse {
if (it.name == "build.gradle") {
def projDir = it.parentFile
if (projDir != rootDir) {
include projDir.name
project(":${projDir.name}").projectDir = projDir
}
}
}
build.gradle::
// Make sure we've parsed subproject dependencies
evaluationDependsOnChildren()
// Map of all projects by artifact group and name
def declarationToProject = subprojects.collectEntries { p -> [toDeclaration(p), p] }
// Replace artifact dependencies with subproject dependencies, if possible
subprojects.each { p ->
def changes = [] // defer so we don't get ConcurrentModificationExceptions
p.configurations.each { c ->
c.dependencies.each { d ->
def sub = declarationToProject[[group:d.group, name:d.name]]
if (sub != null) {
changes.add({
c.dependencies.remove(d)
p.dependencies.add(c.name, sub)
})
}
}
}
for (change in changes) {
change()
}
}
This works, but it's hard to share -- if somebody else wants to do something similar they have to copy my *.gradle files or cut and paste.
What I'd like to do is take this functionality and encapsulate it in a plugin. The build.gradle part looks easy enough to do in the plugin apply() method, but it seems like the list of subprojects is already set in stone before the plugin gets a chance at it. Is there any way to get in earlier in the build process, e.g. by applying to something other than Project? Or should I resign myself to giving my plugin a task for overwriting settings.gradle?
Solution: Per Peter Niederweiser's answer, I moved the code above into two plugins, one to be called from settings.gradle and the other to be called from build.gradle. In settings.gradle:
buildscript {
repositories { /* etc... */ }
dependencies { classpath 'my-group:my-plugin-project:1.0-SNAPSHOT' }
}
apply plugin: 'find-subprojects'
And in build.gradle:
buildscript {
repositories { /* etc... */ }
dependencies { classpath 'my-group:my-plugin-project:1.0-SNAPSHOT' }
}
evaluationDependsOnChildren()
apply plugin: 'local-dependencies'
Note that calling the plugin from settings.gradle doesn't work in Gradle 1.11 or 1.12 but does work in Gradle 2.0.
You'd need to apply a plugin in settings.gradle, which I believe is supported in recent versions.

How can I access the source artifacts of my gradle compile dependencies?

I have written the following task, which extracts all the compile dependencies for each of my sub-projects and puts them in a per sub-project directory:
task exportCompileLibs << {
subprojects.each { iSubProject ->
iSubProject.configurations.findAll{it.name == "compile"}.each{ jConfig ->
println "copying compile libs for ${iSubProject.name}..."
copy {
into "${iSubProject.buildDir}/gradle-lib-export"
from jConfig
eachFile {println it.name}
}
}
}
}
I'd like to extend this to also export the source artifacts that Gradle does already know about (I can see the source jars in the cache directory), I just can't figure out how to use the object model to get a handle to them.
The IDEA and Eclipse plugins seem to be able to do this (they point the project files they build directly into the gradle cache), but I can't figure out how to do it - and looking at the IDE plugin source code, it looks... tricky. I'm hoping there's something obvious that I'm missing in the gradle DSL or API.
Anyone got any ideas?
For anyone looking for at least an interim solution to this, the following seems to be doing pretty much exactly what I want at the moment.
You have to apply the IDEA plugin to the build.gradle file for each project you want to export the dependencies for:
apply plugin: 'idea'
And then define this task:
task exportDependencies << {
def deps = project.extensions.getByType(IdeaModel).module.resolveDependencies()
copy {
from deps*.classes.file
into "${buildDir}/gradle-lib-export/libs"
}
copy {
from deps*.sources.file
into "${buildDir}/gradle-lib-export/sources"
}
}
And here's my horrific hack so I don't have to apply the plugin for each sub-project:
task exportDependencies(description: "export project dependency jars") << {
subprojects.each { Project iSubProject ->
String target = "${iSubProject.buildDir}/gradle-lib-export"
IdeaPlugin ideaPlugin = new IdeaPlugin()
ideaPlugin.apply(iSubProject)
Set<Dependency> deps = ideaPlugin.model.module.resolveDependencies()
println "exporting dependencies for $iSubProject.name into $target"
copy {
from deps*.classes.file
into "${target}/libs"
eachFile { println "lib -> $it.name" }
}
copy {
from deps*.sources.file
into "${target}/sources"
eachFile{ println "source -> $it.name" }
}
}
}
+10 points for not cluttering my build task list with stuff I don't want, -several million points for ewwwww.
There isn't currently a simpler way than what the IDE plugins are doing. This will hopefully change in the future.

Resources