Excluding files in a Gradle multi-project in Intellij - intellij-idea

I have a simple multi-project gradle build file that I use as a base for an Intellij project, and I want to exclude some files from the build, so I use 'excludes' on the compile task (which I have just copy-pasted), as shown in the listing.
This seems to be working from the command line, but it doesn't work from within Intellij i.e. excluded files still get included in the compilation.
Any suggestions? (Gradle 2.10, Intellij 15.0.3)
subprojects {
apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.5'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}
}
project(':projfoo') {
group = 'com.bshishani.projfoo'
}
project(':projbar') {
group = 'com.bshishani.projbar'
dependencies {
compile project(':projfoo')
testCompile project(':projfoo')
}
compileGroovy {
excludes = ['**/aaa.groovy', '**/bbb.groovy']
}
}

You can exclude directories programmatically from IntelliJ project by configuring the idea plugin:
For example:
idea {
module {
excludeDirs += [file("myproject/dir")]
}
}
I'm not sure how to exclude files (or globs) from compilation in IntelliJ. If this isn't possible, it may be preferable to move all of the "special" sources to their own directory.
More information about IntelliJ Module config with Gradle

To mark a folder in the source as excluded from the IDEA complier you must first right click a folder in the projects view.
Right click folder > Mark directory as > Excluded

Related

How to make idea plugin in gradle generate proper project configuration for Kotlin?

I have configured my build.gradle to run gradle test and gradle run properly. However, IDEA does not show any run/test tasks after importing the configuration generated by gradle idea. It seems that these tasks are not included at ipr/iws at all.
Here is my build.gradle:
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
}
}
apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'application'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'idea'
dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
}
sourceSets {
test.kotlin.srcDirs += 'src/test/kotlin'
}
junitPlatform {
enableStandardTestTask true
}
defaultTasks 'run'
mainClassName = 'simpledb.server.Startup'
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.2'
testRuntime (
'org.junit.jupiter:junit-jupiter-engine:5.0.2',
'org.junit.platform:junit-platform-launcher:1.0.2'
)
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
I was wondering is there any way to make generated project configurations include these tasks(run/test)? I have also heard people saying Don't use gradle idea, so is it impossible to use gradle idea to implement this?
It is simply not necessary in your case. Just open the build.gradle file with IDEA and everything should be smooth. The idea Gradle plugin is somewhat deprecated. Not officially, but it was created by Gradle team and is not actively developed to adapt to new IDEA versions and features and so on. So if you don't need special customizations, just open the build.gradle with IDEA and you should be good to go.

How to add dependency sources to to IntelliJ

I have an IntelliJ project with a gradle build file that includes several projects from a maven central repository. One such dependency is Geb.
When I navigate my classes, I sometimes come across a Geb class that looks interesting. I select "Go to declaration" and get a sad "Cannot find declaration to go to".
Obviously this is because IntelliJ has not loaded the Geb source files. But how do I get it to do that without including Geb as a source in my project? I DO NOT want Geb to be compiled into my project from source because I'm already including it as a dependency in my gradle build file.
Adding it as a module dependency does not work. This is like adding more sources.
I suppose I can grab the repo and build the jars and then include those. Is that really necessary?
Adding the IDEA plugin to the gradle file doesn't work.
Relevant part of the gradle script:
apply plugin: 'groovy'
apply plugin: 'idea'
dependencies {
// need to depend on geb-spock
testCompile "org.gebish:geb-spock:0.13.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile "org.apache.commons:commons-lang3:3.4"
testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
}
idea {
module {
downloadJavadoc = true // defaults to false
downloadSources = true
}
}
This is a complete build script that downloads all the dependencies with sources:
apply plugin: 'java'
apply plugin: 'idea'
idea {
module {
downloadJavadoc = true // defaults to false
downloadSources = true
}
}
repositories {
mavenCentral()
}
dependencies {
// need to depend on geb-spock
testCompile "org.gebish:geb-spock:0.13.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile "org.apache.commons:commons-lang3:3.4"
testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
}
task wrapper(type: Wrapper) {
gradleVersion = '3.3'
}
The dependency shows in the list:
And I am able to browse the source code, you can see the comments are there:
One of the possible explanation might be that in your repository list is a repo, such as mavenLocal or a caching Artifactory, that doesn't have the sources dependency.
The ordering of the repositories matters, so if mavenLocal is first and the sources are not available there, I believe they will not get downloaded. A possible fix would be to remove the dependency from mavenLocal and re-download it, change order of dependencies or if it is the parent script, exempt your subproject when adding the repositories:
configure(allprojects - project(':my-subproj')) {
repositories {
...
}
}
I don't think there is any way you can prevent that from the subproject's build script though. It must be done in the parent.

How to build my xtext language with gradle for android

I've written a test-DSL in xtext and generated an IntelliJ plugin.
I have a small Android-test project and in Android Studio my DSL-editor shows up and also generates the output files as expected.
Now I try to configure gradle to also generate the files, but this fails with an error.
I found the Android Integration section for the xtext-builder. This seems out-dated (e.g. it refers to org.xtext.android, which does not exist - also the link there is broken).
Anyway, in the xtext-gradle-plugin github repo there is an org.xtext.android.builder plugin: I guess this is the correct one.
My project build.gradle file looks like this (relevant parts only):
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/" // needed for org.xtext:xtext-android-gradle-plugin
}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath "org.xtext:xtext-android-gradle-plugin:1.0.14"
}
}
allprojects {
repositories {
jcenter()
mavenLocal() // This is required so that the mydsl language is found
}
}
when I activate the org.xtext.android.builder plugin in the build.gradle file of in my app module:
apply plugin: 'com.android.application'
apply plugin: "org.xtext.android.builder" // causes the Error!
I get this error:
Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getJavaCompiler()Lorg/gradle/api/tasks/compile/AbstractCompile;'...
I guess there's some version mismatch or something is not up-to-date.
Any ideas?
This was a bug (#73) in the xtext-gradle-plugin. It was fixed in Version 1.0.16.

Why is Gradle not applying the dependency scope correctly under sourceSets in IntelliJ 2016?

I have a gradle project with a single module.
I have declared the 'provided' configuration to enable provided-scoped dependencies in the parent build.gradle file:
subprojects {
apply plugin: 'maven'
apply plugin: 'java'
apply plugin: 'idea'
configurations {
provided
}
idea {
module {
scopes.PROVIDED.plus += [configurations.provided]
}
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
... other stuff...
}
In the module build.gradle I have declared the following dependencies:
dependencies {
testCompile 'org.elasticsearch:elasticsearch:2.3.1:tests'
compile 'org.apache.commons:commons-io:1.3.2'
compile 'org.apache.commons:commons-lang3:3.4'
compile 'org.elasticsearch:elasticsearch:2.3.1'
compile 'org.slf4j:slf4j-api:1.7.12'
provided 'org.slf4j:slf4j-simple:1.7.12'
}
When I expand the Gradle tool window, it declares the second to last dependency there as provided as well, even though it has compile scope:
I would expect to see the dependency listed with (Compile) next to it in this tool window, not (Provided).
So the question is: Why aren't I?
Is it because the implementation of slf4j (slf4j-simple) is provided, and depends on slf4j-api, so automatically makes that one provided as well? How do I stop that? Should I be stopping that? I want the API as a compiled dependency, but I want projects that use this to decide on their own implementation...
I had the very same problem with IntelliJ IDEA Ultimate build 163.12024.16. I wasn't able to fix it. BUT it magically disappeared when I upgraded to build 171.4249.39. (When using the previous build again, the scope goes back to the incorrect "provided" again.)

How to add plugin jar to the idea module classpath in gradle?

I have a gradle build file and use the idea plugin to generate my project files.
import comp.BuildConfigurator
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'base'
apply plugin: 'comp-gradle-plugins'
assert gradle.gradleVersion == '1.0-milestone-7'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
}
test {
systemProperties = System.properties
}
idea.project {
javaVersion = "1.6"
downloadJavadoc = true
downloadSources = true
}
What I would like to is that we use some class from our internal plugin comp-gradle-plugins in the build file. When I open the build.gradle, Intellij Idea does not recognize the class comp.BuildConfigurator because it is not added to the classpath. How do I make the idea plugin to add the plugin jars to the module classpath?
Adding the plugin Jars to the module class path won't help. To make IntelliJ aware of Gradle's build script class path, you'll have to use the Gradle import in IntelliJ 13. However, I don't know if this will work with a Gradle version as old as 1.0-milestone-7.