how to regenerate missing source paths in in a Gradle project? - gradle

how to regenerate missing source paths in in a Gradle project? I need to generate all those missing folders like src/java, src/clojure and so on. depends on project plugins. So if I add say Java plugin then src/java/ will be generated.
I just saw that some IDE has an option to create al source paths of the plugins when it imports the project - so I assume it is possible to do from the command line.

You can do that in build.gradle by changing the sourceSets.
sourceSets {
main {
java { srcDirs 'src/java' }
clojure { srcDirs 'src/clojure' }
}
}
If you also need to do the same for the test folders, this should work too:
sourceSets {
test {
java { srcDirs 'test/java' }
clojure { srcDirs 'test/clojure' }
}
}
Java plugin will not create folders for you, it instead assumes a project layout, and you are free to change this structure by doing something similar to what I have above. I have not used the clojure plugin, but I assume it behaves in a similar way.
If you want to list all the folders that the java plugin is making use of, you can do this:
println sourceSets.main.allJava.asPath
Displays all the files being watched by the java plugin.

Related

Intellij gradle package name incorrect error

I have just opened this (https://github.com/codetojoy/easter_eggs_for_gradle/tree/master/egg_StackOverflow_51685286) project in IntelliJ with the following files:
src/net/codetojoy/Foo.java
src/net/codetojoy/service/FooService.java
src/net/codetojoy/tests/FooServiceTestCase.java
and build.gradle contains a configuration for sourceSets like so:
sourceSets {
main {
java {
srcDir 'src'
exclude 'net/codetojoy/tests/*'
}
}
}
sourceSets.test.java.srcDir 'src/net/codetojoy/tests'
and On the FooServiceTestCase.java file, I am getting an error on the package line saying Package name 'net.codetojoy.tests' does not correspond to the file path.
I think it is because of the customised source and test set. But I am unsure how to fix it....
please help
The test source set definition needs to look more like the main one:
sourceSets {
main {
java {
srcDir 'src'
exclude 'net/codetojoy/tests/*'
}
}
test {
java {
srcDir 'src'
include 'net/codetojoy/tests/*
}
}
The reason the previous answer worked in command line but not the IDE is that for the Java compiler, the location of a source file is irrelevant. The package instruction is used to correctly place the produced class file, but the source file does not have to be in a matching directory structure. But usually an IDE performs this check.

How to set the gradle outout folder for the kotlin2JS plugin?

I have a KotlinJs only project which I use official kotlin2js gradle to build, and no problems there.
How to setup the output folder, currently, the building of subproject will result in a build which locates inside the subproject folder, how to set it to somewhere else? I tried:
sourceSets {
main {
kotlin.outputDir = new File(‘./out/‘)
}
}
and
sourceSets {
main.kotlin.outputDir = new File(‘./out/’)
}
No luck.
What I want is to no matter how many subprojects are there, the output folder should be in some path like ./build/projectA and ./build/projectB, rather than all in their own folder. How to do this?
Currently, it's done through the task configuration, namely setting its kotlinOptions.outputFile:
compileKotlin2Js.kotlinOptions.outputFile = "out/output.js"
It's briefly mentioned in the tutorial: Getting Started with Kotlin and JavaScript with Gradle

Filter class files from compiled sourcesets to be added in a JAR in Gradle/Groovy

I'm trying to include the compiled .class files from Project1 into the jar for Project2 since my project structure requires it to be done. For that, in the build.gradle for Project2, I write :
jar {
from project(':Project1').sourceSets.main.output.classesDir
}
Which successfully does what I had to do. But, I now want to filter some of the classes that are added based on path and/or some pattern. For example, to include only delegate files, I tried this :
jar {
from project(':Project1').sourceSets.main.output.classesDir {
include '**/*Delegate*.class'
}
}
But unfortunately it doesn't work. Is there a way to achieve this in Gradle/Groovy?
Using Gradle 2.12, the following works for me (this is build.gradle for Project 2):
task myBuild(type: Jar) {
baseName "myjar"
from project(':Project1').sourceSets.main.output.classesDir,
{ include "**/*Delegate*.*" }
}
From the doc for Jar.from, note that it takes 2 arguments (hence, the comma is used).
Thanks Michael
Although I got my answer as well, I was just missing a parantheses. The correct and working code goes something like this :
jar {
from (project(':Project1').sourceSets.main.output.classesDir) {
include '**/*Delegate*.class'
}
}

Gradle: Accept resource files in src/main/java folder

Is there a way to convince Gradle to accept resource files (like XML files) as well in the src/main/java folder?
This would be great since I need to put my JavaFX XML files there ...
You could define where gradle looks for resource files by doing something like this:
sourceSets {
main {
resources {
srcDirs = ["src/main/java"]
includes = ["**/*.fxml"]
}
}
}
The equivalent Kotlin DSL would be:
sourceSets {
main {
resources {
srcDirs("src/main/java")
include("**/*.fxml")
}
}
}
As the name suggests src/main/java is for JAVA files. Whilst it's possible to tweak gradle's defaults to allow resources in this folder, you shouldn't (in my opinionated view).
I suggest you stick to the sensible conventions and put your xml etc in src/main/resources. This convention is followed by both Maven and Gradle.

Gradle: assemble output of a specific configuration

For a project, I have a custom configuration which should simply extend the default one, both in terms of java sources and in terms of dependencies.
This is what it looks like at the moment:
configurations {
// Tools inherits everything from default and adds on
toolsCompile.extendsFrom(compile)
}
sourceSets {
main {
java {
srcDirs = ['src']
}
}
tools {
java {
// Tools extends on the core sources
srcDirs = sourceSets.main.java.srcDirs + ['src-tools']
}
}
}
dependencies {
compile libs.a
compile libs.b
compile libs.c
// Tools adds on the dependencies of the default configuration
toolsCompile libs.d
toolsCompile libs.e
}
I know I could have also used sub-projects for this. I gave up on it, after trying, because I can't get it work properly together with the Eclipse integration plugin (it works fine when used from command line).
I have a couple of questions on the solution above:
Is the way I extend tools.java.srcDirs correct? Is there a more elegant way?
EDIT: Apparently it is not correct, as gradle eclipse generates a .classpath with a duplicate entry for src. Help please?
After I created my tools configuration, I know I can for example use it as a dependency from another project, as in compile project(path: ':myproject', configuration: 'tools'). What do I need to add if I instead want to get the output of the assemble task for my tools configuration? Do I have to make an explicit task for that? The task toolsClasses is created automatically, but not toolsAssemble or toolsBuild.

Resources