What command does Intellij Gradle project refresh execute - intellij-idea

Intellij hides which gradle command it runs whenver you press a button to do something with gradle. That makes it extremely difficult to find out what goes wrong. I am clicking "refresh" (NOT REFRESH DEPENDENCIES), and it seems to trigger all my sibling projects AND my project to build which is wrong. It is definitely not running "gradle build" because if it did, it would not trigger all the sibling projects in my multi-project build.

Clicking "refresh" in gradle shows a log like this:
2019-10-17 14:58:10,391 [9949323] INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: -Didea.sync.active=true -Didea.resolveSourceSetDependencies=true --init-script /tmp/ijinit.gradle
Here IDEA is using the Gradle Tooling API and using a gradle init script (in /tmp/ijinit.gradle on my machine). The file shows more:
$ cat /tmp/ijinit.gradle
...imports etc...
initscript {
dependencies {
classpath files([...list of IDEA jars...])
}
}
apply plugin: JetGradlePlugin
class JetGradlePlugin implements Plugin<Gradle> {
void apply(Gradle gradle) {
def processor = new RegistryProcessor()
gradle.addProjectEvaluationListener(processor)
def projectEvaluationIsNotCalledForIncludedBuilds = GradleVersion.current() >= GradleVersion.version("3.1") &&
GradleVersion.current() < GradleVersion.version("4.0")
if (projectEvaluationIsNotCalledForIncludedBuilds) {
gradle.rootProject {
it.afterEvaluate {
gradle.includedBuilds.each { included ->
// included builds should be configured by now, so calling `configuredBuild` should be safe
def toolingRegistry = (ToolingModelBuilderRegistry)included.configuredBuild.services.get(ToolingModelBuilderRegistry.class)
processor.process(toolingRegistry)
}
}
}
}
}
}
...other overrides...
As others mentioned the included builds can be excluded in the IDEA Gradle tab but hopefully this answers the specific title question.

If you are using a recent version of Gradle, you can use --refresh-dependencies option.
./gradlew build --refresh-dependencies

Related

How to share kotlin code between a project and gradle?

I have an appium project written in kotlin and a build.gradle.kts. I'm using InteeliJ but also need to invoke the tests from the command line.
I have a Datasource.kt file which I would like to access both in the code and in gradle; right now it's just duplicated in the build.gradle.kts.
How can I effectively share the Datasource.kt file so that I have a single source of truth throughout the project?
Code example:
if (Datasource.getValue("key").isFeatureEnabled()) {
// check that corresponding button exists
}
Gradle example:
tasks.test {
...
useJUnitPlatform {
if (!Datasource.hasSomeThings) {
println("Gradle excluding \"SomeThings\" tests tag")
excludeTags("SomeThings")
}
}

Breakpoints not working with GroovyShell evaluate()

I'm trying to add breakpoints in IntelliJ to debug foo.groovy when used like this:
def binding = new Binding();
binding.lineList = [list1];
binding.count = 5;
def shell = new GroovyShell(binding);
def result = shell.evaluate(new File("../../not-src-main-groovy/foo.groovy"));
This compiles and runs fine, BUT IntellIj won't stop at my breakpoints.
On the other hand, if I place foo.groovy in src/main/groovy/com/... then IntellIJ DOES stop at my breakpoints, as suggested in this stackoverflow answer.
But unlike that post, foo.groovy does multiple import statements. Those statements reference code from a different github repo, cloned locally outside my project at ../../not-src-main-groovy.
I've tried changing IntelliJ > Project Structure > Modules > mymodule > Dependencies > + > JARS or Directories > "../../not-src-main-groovy" but IntelliJ doesn't stop at the breakpoints.
How can I configure IntelliJ to stop at foo.groovy breakpoints?
I found a solution. Because my project uses gradle, I added the following to build.gradle.
dependencies {
...
compile files("include-me/**")
...
}
That way, gradle will include the files in the classpath during the compilation phase, but not actually compile them, since they're not in here:
sourceSets {
main.groovy {
srcDirs = [ 'src/main/groovy']
}
}

intellij idea 14 and gradle issues when running integTests

I have a gradle project imported into idea 14.0.3. The integration tests run fine from the command line. They were also running without any problems in idea 13 from the context menu (running single tests). However, in 14, when I use the context menu in IDE, for some reason the tests that depend on class path resources from src/integTest/resources are failing due to resource not being found. Any idea how I can add this folder in the classpath search in Intellij 14? Has anyone seen this issue before?
If I move the same resource to src/test/resources (or src/main/resources), the tests run fine. So it seems like intellij is not simply looking under src/integTest/resources.
Appreciate the help!!
I have run into this before as well, add the following to your build.gradle file:
// work-around to fix IDE-run test failures (may be fixed in future Gradle versions)
task copyMainResourcesToTest(type: Copy) {
from "${projectDir}/src/main/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyMainResourcesToTest
task copyTestResourcesToTest(type: Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResourcesToTest
I think this may be resolved in the newest release of Gradle but I have not verified yet. You will want to update the paths for your specific use case.
Looks like this is Bug (IDEA-128966) in IntelliJ 14.
The recommended workaround is something like this:
sourceSets {
integrationTest {
java.srcDir file('src/integTest/java')
resources.srcDir file('src/integTest/resources')
}
if(System.properties.'idea.active') {
main {
resources.srcDir file('src/integTest/resources')
}
}
}
For our projects I changed that to:
if(System.properties.'idea.active') {
test { //Add to test rather than main
That still works and I think it communicates the intent better.
This workaround assumes you have configured your integration tests like this in the build.gradle:
apply plugin: 'idea'
sourceSets {
integrationTest {
java.srcDir file('src/integTest/java')
resources.srcDir file('src/inteTest/resources')
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
}
task integrationTest(type: Test, dependsOn: jar) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
build.dependsOn(integrationTest)
Note: This answer was originally deleted by a moderator because of a duplicate answer that I posted on another question. I now deleted my answer there (and added a link to here) because I think it fits this question better.

How to get `gradlew idea` to get IDEA to use test conf for custom task?

I have the following:
sourceSets {
integrationTest {
java.srcDir file('.')
}
}
dependencies {
integrationTestCompile project(':sut-project')
}
task integrationTest(type: Test)
but when gradlew idea is run and the project opened, the Sources doesn't have a Tests conf for that project.
What needs to be done to get this to work?
The documentation for IDEA plugin is at http://www.gradle.org/docs/current/userguide/idea_plugin.html
From this links you can navigate to http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html where you can see how to map additional source roots (AKA content-roots in IntelliJ parlance) to your modules and configurations to classpath using proper scope. You will use something like:
idea {
module {
testSourceDirs += file('src/integTest/java')
scopes.TEST.plus += configurations.integrationTestCompile
}
}

Managing project dependencies with Gradle and IntelliJ

I am looking to use Gradle to build my Groovy / Grails based project for which we are using IntelliJ Idea as the IDE.
I am using IntelliJ version 11.1.4, Gradle version 1.2.
My project is a configured as a multi project build with various Groovy & Grails subprojects.
I was hoping that this would give me the same kind of IDE support that I would get if managing the build through Maven, such as:
Automatic dependency management (import new dependencies to IntelliJ when added to various build.gradle)
Build DSL support
Execution of build tasks
Use of the underlying build system (gradle) by the IDE when performing builds\
I have imported my project into IntelliJ by opening the root build.gradle file.
So far I am coming up against a few annoying problems:
IntelliJ is not recognising (or recognising randomly) changes to dependencies in the build.gradle files and therefore dependencies are not being updated.
The gradle "idea" plugin doesn't seem to work with multi module projects.
How are people working with Gradle inside IntelliJ? Are you managing dependencies manually within IntelliJ??
I have been using Gradle "idea" plugin for some time now and it works very well. Since "idea" plugin simply generates IntelliJ project configuration files it is somewhat limited in what you can do with it, but nevertheless, I have had more success with it compared to the IntelliJ gradle support (JetGradle thing).
Gradle "idea" plugin does work with multi-module projects, never had a problem with that. I always put parent project configuration in master folder (see Initialization chapter), which seems to work. Never tried the nested structure though.
In order to do additional IntelliJ configuration you can do some .ipr and .iml tinkering from gradle, or alternatively try using one of my plugins (see Utilities plugin) which will do most of the tinkering for you.
In the end I went with rodion's suggestion above and used the idea plugin. It turns out I had not configured it properly first time I tried it (only applied the plugin to the master project, not subprojects).
I only found this out after writing my own task to update the dependencies for an IntelliJ project (based on the .idea directory layout project structure). I am going to use the plugin as there will be less maintenance, but here is my solution for posterity and in case it is useful to anyone:
ext {
intelliJLibraryDir = "$gradle.rootProject.rootDir/.idea/libraries"
userHomeDir = gradle.gradleUserHomeDir.parent
}
task cleanIntelliJLibraries << {
ant.delete (includeEmptyDirs: 'true') {
fileset(dir: intelliJLibraryDir, includes: '*.xml')
}
}
task createIntelliJLibraries(dependsOn: cleanIntelliJLibraries) << {
// The unique set of dependency artifacts across all subprojects
def uniqueProjectArtifacts = subprojects.collectMany {
if (it.configurations.compile) {
it.configurations.compile.resolvedConfiguration.resolvedArtifacts.findAll {
it.moduleVersion.id.group != "my.project"
}
}
else { [] }
}.unique()
// Output a library xml file for each of the dependency artifacts
uniqueProjectArtifacts.each { artifact ->
def artifactPath = artifact.file.path
def artifactName = artifact.moduleVersion.id.with { "$group:$name:$version" }
def intelliJLibraryPath = artifactPath.replace(userHomeDir, '$USER_HOME$')
def intelliJLibraryFileName = "Gradle__$artifactName".replace(':', '_').replace('.','_') + ".xml"
new File("$intelliJLibraryDir/$intelliJLibraryFileName").withWriter { writer ->
def dependencyXML = new MarkupBuilder( writer )
dependencyXML.component (name: "libraryTable") {
library (name: "Gradle: $artifactName") {
CLASSES {
root (url: "jar://$intelliJLibraryPath!/")
}
JAVADOC {}
SOURCES {}
}
}
}
}
}
task updateIntelliJModules(dependsOn: createIntelliJLibraries) << {
subprojects.each { project ->
def root = new XmlSlurper().parse(new File("${project.name}.iml"))
// Remove the existing dependencies
root.component.orderEntry.findAll { it.#type == "library" && it.#level == "project" }.replaceNode {}
// Add in the new dependencies
if (project.configurations.compile) {
project.configurations.compile.resolvedConfiguration.resolvedArtifacts.findAll {
it.moduleVersion.id.group != "my.project"
}.each { artifact ->
def artifactName = artifact.moduleVersion.id.with { "Gradle: $group:$name:$version" }
root.component.appendNode {
orderEntry (type: "library", exported: "", name: artifactName, level: "project")
}
}
}
def outputBuilder = new StreamingMarkupBuilder()
new File("${project.name}.iml").withWriter { writer ->
groovy.xml.XmlUtil.serialize(outputBuilder.bind{ mkp.yield root }, writer)
}
}
}