IntelliJ gradle - unknown properties - intellij-idea

The error:
Build file 'C:\Users\Me\Dev\project\app\build.gradle' line: 21
What went wrong:
A problem occurred evaluating root project 'app'.
Could not get unknown property 'libraries' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
inside app.gradle
dependencies{
compile libraries.my_lib
deploy libraries.my_lib
}
inside project.gradle
ext.libraries = {
my_lib: 'com.myCompany:my-lib:1.0.0'
}
inside pom.xml
<modules>
<module>my-lib</modules>
</modules>
Pressing CTRL+Space in app.gradle's libaries. shows the my_lib library that may be autocompleted, but when compiling using gradle clean deploy, it fails and returns the message above.
This only happened after I updated the IDE to the latest IntelliJ.

ext defines extensions only for a project it was called in. In your case, libraries.my_lib is defined in project.gradle (I guess, it is build.gradle in project directory) and, thus, is not available in a different build file (app.gradle, which is again, I guess, is a build.gradle in app directory)) unless project is not a parent of app. Child projects have access to parent's properties, and that is the only way to share ext: projects should be related to each other.
BTW, you can define properties in gradle.properties file. There may be many of them, one for every project in your build. Placing one gradle.properties in the root of your build you apply its content to the root of your project tree, thus, making the properties available everywhere (the same applies to ext block of a root object, as it follows from the previous paragraph).

Related

HelloWorld in Kotlin gives error "Could not find or load main class"

I spent the last 1,5 hour trying to make this simple tutorial work in IntelliJ IDEA, as you can see in this video.
When trying to run the code, I get the error:
/[...] -Dfile.encoding=UTF-8 src.HelloKt
Error: Could not find or load main class src.HelloKt
Caused by: java.lang.ClassNotFoundException: src.HelloKt
I have tried setting up SDK, invalidating cache, removing .idea and .gradle, rebuilding project, deleting the profile and adding it again. I tried those actions in different orders.
Here's a screenshot of the project:
It also complains Kotlin is not configured, but I have already configured it.
Here's the run configuration:
Here are the project settings:
Your Hello.kt file needs to be somewhere inside the src/main folder, probably in src/main/kotlin. This is different from the tutorial, because your project is using Gradle, and the one in the tutorial isn't. I think this is because newer versions of IntelliJ use Gradle by default for new projects, which wasn't the case when the tutorial was written.
The use of src/main/kotlin and src/test/kotlin as source code directories is a convention in Gradle (and Maven). When importing a Gradle project into IntelliJ, main becomes a module, and kotlin becomes a source folder within that module. The same goes for test. In your screenshots, the bold text and blue icons on main and test confirm that's how your project is set up. Files outside of those folders aren't treated as source files, which explains why your Hello.kt file isn't being compiled or recognised correctly.
It's likely that the default behaviour of IntelliJ when creating a new project has changed since this tutorial was written. In the tutorial, they select "Kotlin" as the project type and this creates a project that doesn't use Gradle. As a result, the project doesn't use the src/main/kotlin directory structure.
I can see from your video that you selected the same option, but on the next screen, IntelliJ still automatically selected Gradle as the build system for the new project. To match the project structure used in the tutorial, I think you would need to select "IntelliJ" as the build system.

Kotlin/IntelliJ Modules with Shared Content Root

I'm working on a project in IntelliJ Idea using Kotlin. I'm trying to create a unit test for a Kotlin class, and I can create the class, except that when I run the test, I get an error that there is no JDK configured. I go into the project structure and change the JDK for the module, but when I click on Apply, I get a message saying that :
Content root
"C:\Users\2rtmar\Documents\xproject\xproject\examples\src\main" is
defined for modules "xproject-examples-utils_main" and
"xproject-examples-java_main". Two modules in a project cannot share
the same content root.
Another team member had said that these modules were faulty, and that they couldn't be used, but I'm not using them and still prevent me from running my code. I tried to unmark this module as a sources root, but did not fix the problem. I went as far as to removing the xproject\examples module entirely, but received the same message when trying to set the JDK of my module.
Any help is appreciated!
In my case I was trying to setup a multi-module Gradle project by adding a new module to my root Gradle project.
Despite adding an include ':my-submodule' to my settings.gradle, it
was always telling me gradle wrapper not found, because the submodule was kind of outside of the context of the root module.
This however is IntelliJ-specific
When I looked into Right-Click on module -> Open Module Settings - > Modules I was seeing the following hierarchy:
my-root
-> my-submodule
my-submodule
For whatever reason, IntelliJ has added the new module as a child and as a standalone module, and this caused the issue Two modules cannot have the same content root, as my-root:my-submodule and my-submodule are now both pointing to the same src/main directory.
This is IntelliJ's out of the box behaviour when adding a new
submodule, which is in my opinion totally wrong and likely a bug! You
cannot even fix it within the module settings
Solution
But I discovered a way to fix it.
Right-click on the module
Load/Unload Modules
Now unload the module that is not below the my-root
After that, there is just one single root pointing at the src directory and the active module is now within the parent's Gradle scope.
I hope Jetbrains fixes these issues. Those problems exist for a while now and are not pleasant to solve.
I got this issue recently.
The very first check should be to check if any other module with the same content root is added or not.[you can check this in Project Structure -> Modules]
If YES, remove the other project by clicking on - (minus) button at the top.
If you're using Intellij and have modified / are modifying the pom.xml files directly:
Go to the affected sources' pom.xml files
Go to the following area:
...
<build>
...
<resources>
<resource>
...
<directory>[DIRECTORY]</directory>
...
<includes>
...
</includes>
...
</resource>
</resources>
...
</build>
...
Change the [DIRECTORY] to something unique. For instance, I was referencing ../plugin-outputs. As such, I changed it to ../plugin-outputs/[specific sub-directory], wherein I named the sub directory as I wished.
Delete any "modules" listing the specific output directory. No, they aren't modules... however, they have the module icon applied to this, preventing the pom.xml files' changes to be updated while editing modules through the standard project structure tab.
I know this thread is 2 years old; however, there are MANY people who still read these artifacts... hopefully this helps someone.
-Dan

How to mark a directory as a generated-test-source root in IntelliJ?

I'm trying out Dagger2 in Intellij 2016.1 (but not with gradle) on ubuntu.
Intellij creates dagger's generated sources in either
./out/production/<ProjectModule>/generated/ or
./out/test/<ProjectModule>/generated_tests/ depending on if it was generated from a source or test directory, respectively.
But from what I tell, I can only mark those directories as either sources root, test sources root, or generated sources root; there is no option for generated test sources root, say.
Why is this important? Because the generated test sources depend on my test sources. If they are marked as a generated sources root then Intellij cannot find the dependencies.
Note: I don't think they should be marked as test sources root because then Intellij tries to compile those again; unless there is some way of preventing this of which I am unaware.
So is there a way to mark this directory as a generated test sources root or something equivalent?
To mark a diectory as "generated test sources root", open the "Project Structure" dialog at Project Settings > Modules and click on the little "P" next to your folder of choice, and select the "For generated resources" button.
Dagger uses annotation processing to generate sources during compilation. IntelliJ has a specific configuration for this feature in Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors
When it is enabled IntelliJ automatically adds generated sources to project.
With annotation processing enabled I can see that generated test sources are marked both as Test Sources Root and Generated Sources Root. But when I try to manually set both flags it does not work - I get flags Sources Root and Generated Sources Root.
For me it looks like a bug.
Here's what worked for me. Create a directory in the Module root called generated and under it have two simlinks to <ProjectRoot>/out/production/<ProjectModule>/generated/ and <ProjectRoot>/out/test/<ProjectModule>/generated_tests/. Mark the first as Resource Root and the second as Test Resource Root.
I created the new directory and simlinks because it appears Intillij auto-marks <ProjectRoot>/out as Excluded.
I marked the directory as Test Resource Root so that Intellij doesn't try to compile the source twice to the same class. (Hint: big complains from the compiler.)
In the end, no red squiggles and auto-complete works.
Note: I didn't change the Intellij's generated sources directory for the module. (Well, I did to try another answer, but changed it back.)

"Class already exists" error in IntelliJ on Groovy class

in IntelliJ (2016.2 and previous) we have our Groovy classes marked red with the error "class already exists".
I think we can exclude that the cause is the stub-generation, as this is deactivated.
Probably it's caused in our constellation: We have included our compiled groovy (and java) classes in a jar that is registered as dependency.
Dependency MyProduct.jar contains com.mycompany.MyGroovyClass
Our source contains com.mycompany.MyGroovyClass
The error disappears if the dependency is registered with Test-Scope, in all other scopes the error appears.
However, in our structure we kinda have to include the compiled classes in a compile scope, as we want to avoid that each developer needs to compile all classes (I know about the compile in background ability, but we have a constellation that prevents this from working).
We have no errors in com.mycompany.MyJavaClass which exists as well in source and in MyProduct.jar.
Any ideas on how we can solve this?
We've been suffering the same issue, it seems to be that IntelliJ registers the Java class, but also the Groovy class, and because of that it is showing that message (BTW, we are using a Maven Project).
So we ended up by going to the target folder -> right click -> Mark Directory As -> Excluded. Then, this setting will be saved on the IML file, and it won't happen again.
Hope it works for your as well!
Cheers
I'm using gmavenplus-plugin:1.5
After marking target/generated-sources/generated-sources/main as "Excluded", The error disappeared. I even did "invalidate cache and restart", It persists the setting. This is great. Intellij 2017.1.5
We have two ways to fix this issue
Exclude Stub Directory
target folder -> generated-sources -> groovy-stubs -> Right click main folder -> Mark Directory As -> Excluded
Remove generateStubs goal from gmaven plugin
Remove <goal>generateStubs</goal> from gmavenplus plugin
Make sure you Mark the src folder as Sources Root and do the same for the test folder
Then delete the target folder (most likely it's marked in yellow) and don't worry it won't delete any code from your project
If the issue persist, proceed to go to File -> Invalidate Cache/Restart

Model project dependencies in Gradle: Add a dependent project to another project

I am facing the following problem. I have two Gradle projects (ProjectA and ProjectB) on the same hierarchy without any root project. Since ProjectA depends on ProjectB, I tried to model this as follows:
ProjectA's settings.gradle:
includeFlat 'ProjectB'
ProjectA's build.gradle:
dependencies {
compile project(':ProjectB')
}
However, then I am getting the following error when executing 'gradle build' on ProjectA:
* What went wrong:
A problem occurred evaluating root project 'ProjectA'.
> Could not resolve all dependencies for configuration ':compile'.
> Module version group:, module:ProjectA, version:0.0.1-SNAPSHOT, configuration:compile declares a dependency on configuration 'default' which is not declared in the module descriptor for group:ProjectA, module:ProjectB, version:unspecified
Do I have to define some kind of default configuration in ProjectB?
Do I have do defined some kind of default configuration in ProjectB?
Either that, or apply the base plugin. Many other plugins (java, groovy, scala, etc.) apply the base plugin automatically, so that you don't have to.