I have seen examples for using "by settings" delegation in build.gradle.kts scripts, but no matter what I have tried to do, Gradle complains that settings is an unresolved reference. If I change 'settings' to 'project', Gradle is happy. Has the use of 'by settings' been deprecated? I looked in the Gradle 6.4.1 manual and it shows this delegation. I've attached an image showing a simple Spring Boot app generated by Initializr with the only changes being the addition of the gradle.properties file and the delegation in the build.gradle.kts file. I'd really appreciate any hints about this issue... I know that I can use 'extra' properties or finding the property against the project object, but, frankly, this issue has annoyed me to the point that I need to know what is wrong! :P. Thank you for any help...
The project delegate is defined in the class ProjectDelegate. Your build script is evaluated against an instance of KotlinBuildScript which extends Project, so the delegate is available.
However, when settings.gradle is evaluated, projects aren't configured yet, so there's no instance of KotlinBuildScript. Instead, the script is evaluated against an instance of KotlinSettingsScript which extends Settings, which has the settings delegate. I don't think this delegate was ever available to project build scripts.
In both cases the delegate is provided by an extension function, because both Project and Settings are Java files part of the Gradle core API.
I hope that answers your question.
Related
The official Kotlin documentation states:
Add kotlin.code.style=official property to the gradle.properties file at the project root.
I'm trying to understand how kotlin-gradle-plugin handles this property.
Which gradle task uses it?
When running gradle build, I don't see my code being reformatted, even if I format my code badly on purpose.
I went through the Github source code of the plugin but couldn't properly get to understand it.
Thanks for your help.
Kotlin Gradle plugin doesn't use this property, as it's not responsible for reformatting the code. Instead, this property is used by Gradle importer of Kotlin plugin for IntelliJ IDEA.
This facade provides access to Gradle properties defined for the project:
https://github.com/JetBrains/kotlin/blob/v1.4.10/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/GradlePropertiesFileFacade.kt
It checks local.properties first in case user wants to override this value in the local configuration (this file is usually added to .gitignore for VCS to skip it during it's operations), then in usual gradle.properties.
Then the property gets consumed to configure the project here:
https://github.com/JetBrains/kotlin/blob/v1.4.10/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinGradleSourceSetDataService.kt#L158-L159
Same thing goes for Maven-based projects. These are the only two places the property is used throughout Kotlin repository apart from tests right now.
I am working on a project using kotlinjs for nodejs and I start to learn coroutines and integrate them into my code to say goodbye to the callback hell.
I managed to get coroutines working, my code can be compiled and executed, everything seems fine.
...except: The IDE still shows me errors. It seems to be confused by the additional libraries I added in order to get coroutines running.
I am currently fiddling around with the library dependencies, sometimes some errors go away, but then some code gets red which was okay before...
This is what I see:
Case 1:
Cannot access class 'kotlinx.coroutines.experimental.CoroutineContext'. Check your module classpath for missing or conflicting dependencies
Case 2:
Unresolved reference: JsModule
Modifier 'external' is not applicable to 'class'
You see, launch is recognized when I add the stdlib, but then the IDE complains with the other two errors.
Again, please note: In both cases, actual compilation is successful!
I am using IntelliJ 2018.1 with Kotlin Plugin 1.2.41.
Thanks to Alexander Chernikov at youtrack.jetbrains I could resolve my problem.
I cite his explanation:
The issue is that JavaScript libraries should be marked with special attribute to be recognized.
When they are imported from pom.xml or build.gradle, this mark is set, so the feature works.
In your project they are not marked.
At the moment, to correct the libs manually, please open .idea/libraries/org_jetbrains_kotlin_kotlin_stdlib_js_1_2_41.xml and .idea/libraries/org_jetbrains_kotlinx_kotlinx_coroutines_core_js_0_22_5.xml.
In both files find type="repository" and replace it with type="kotlin.js".
Make sure IDEA sees the change. (You can exit IDEA, make the change and restart.)
Then you can keep only these two libs in dependencies. The editor should work.
Here is the issue link:
https://youtrack.jetbrains.com/issue/KT-24575
There I have also attached a sample project with the problem.
I maintain an IntelliJ plugin (Codename one) and we need to control the users classpath. I'm adding a classpath either via the plugin or manually by going to here:
And pressing the + sign where I pick Java:
Then choose classes:
This seems to work OK:
But the completion and other such functionality doesn't work and when I go back the entry is disabled and I get this error message:
This doesn't really tell me anything?
A workaround is to open the .iml file in a text editor, and add the following to the orderEntry list:
<orderEntry type="library" scope="PROVIDED" name="LibraryName" level="project" />
Unfortunately this isn't very practical and it only solves some of the problems I'm experiencing. Any direction or hint would be appreciated here.
Edit: Adding screenshot of preferences UI:
Second Edit: Screenshot of the module section
The answer from kukido is good, for additional details though check out this: http://devnet.jetbrains.com/message/5509300
Essentially what I want is to still have my module but add a dependency programmatically which can be done by:
ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
model.addLibraryEntry(library);
model.commit();
This doesn't really tell me anything?
IntelliJ is trying to warn you that you have created a library, but it is not referenced anywhere in the project. It is gently asking "Did you mean to add this library as a dependency?".
The entry is not disabled. It is greyed out to show that it is not referenced in any module as a dependency. Once you add the library to the dependencies list, the color will change.
It is a two-step process:
Create a library
Reference new library in a module
Code completion is not working because your module is not aware of the library classes.
Module dependencies
Non-referenced and referenced libraries
I have a main plugin project which does not depend on any of Eclipse APIs. But I do want to use Eclipse API's in one of its fragment plug-in. Will it cause any problem for the main plugin?
If you add a dependency (to a plug-in or a package) in a fragment, then you effectively modify the class path of the host project as well. Whether that will cause any changes to the semantics of the host project or any other fragment for the same host project, depends on the specific use in the project.
Having said all that, the normal answer is: no, it should not cause problems, unless you have code that depends on the class path - e.g. if you are using Class.forName(...) or similar...
One final note: when you test this, use -clean argument in the launch configuration to force OSGi to accept the changed dependency. Otherwise, it will be ignored.
When I make one plugin extend another it normally inherits the properties from the parent. Sometimes it doesn't work though.
When the plugin is loaded up, the properties from the parent are all null
What might I be doing wrong? I package my project as a maven-plugin and it builds ok.
I've seen this myself and it drove me mad until I debugged the Plexus internals. I'm guessing the properties are not inherited when the parent is in another plugin?
If that is the case, the explanation below will help. If it is not, it might be a typo in the Javadoc annotations. Maven will skip any tags it doesn't recognise without warning.
If it's neither of these, can you post a little more detail? perhaps an example of the failing Mojo?
If the parent is in another project, here's the reason why you're having problems.
Maven plugins use Javadoc annotations to define the dependencies, goal declarations, and other configurations (Maven 2.1 introduced proper Java annotations but hardly any plugins use them yet). Once the plugin has been installed/deployed the Javadoc is lost, so any plugin that extends a plugin in another jar won't have access to the plexus-defined properties in the parent.
There is a workaround though. The plugin metadata is output to META-INF/maven/plugin.xml. There is a third-party plugin that reads the information from the parent Mojo's plugin.xml and merges the local plugin metadata with it. Your plugin should then work as normal.