Intellij IDEA editing gradle project file vs project settings - java

It took a few tries to figure out that the way gradle integration works, is by Intellij scanning the gradle project file, and then creating the project settings from it. What was confusing me, and is still confusing me, is how the IDE fails to pick up certain things. It also seems to spontaneously change things like the src directory. I end up having to go in to the project settings, and fix things like marking the appropriate directory as a source directory, and re-adding the excluded source directories. It seems to ignore the exclude statement from the gradle file.
Am I going about this the right way? Why does my project build fine from the command line using the gradlew command, but often does not inside the IDE? Is it normal having to "fix" various project settings? Are these bugs or incomplete features in Intellij?

Related

How to solve multi-project build problems in IntelliJ IDEA

When opening directories that contain multiple Java projects with IntelliJ IDEA I regularly encounter build problems that cannot be solved with the built-in mechanisms (build tool reload or Invalidate Caches menu item). They typically occur after structural changes like refactoring or changing dependencies, both with Maven and Gradle as the build tools.
The effect of the problem is that new or changed dependencies are not recognized or that some projects in the monorepo are no longer recognized by IDEA. In the case of library projects that will cause build errors in all dependent projects.
I have found a manual fix for this which I will provide as my own answer but maybe someone can provide a better solution.
A possible solution to this is to manually delete IDEAs build tool cache. To do so first quit IDEA, than delete the corresponding project directory (named .) from the following location (I recommend to backup first):
On Unix: ~/.cache/JetBrains/IntelliJIdea<idea-version>/external_build_system
On Windows: <user-home>\AppData\Local\JetBrains\IntelliJIdea<idea-version>\external_build_system
On Mac OS: ~/Library/Caches/JetBrains/IntelliJIdea<idea-version>/external_build_system
When restarting IDEA it should re-scan the project correctly. At least in my case this has always worked so far.
This offtenly works for me:
1 In master project pom I just commenting lines which defines submodules.
2 Then I'm waiting when IDEA will suggest to exclude subprojects.
After this action submodule directories will be displayed just like some external file directories, not like a project.
3 Then I uncomment the lines in pom and wait when IDEA will add subprojects again.
4 After this action I select rebuild project in Build section and it works.
I've tested this with maven projects, hope should work for gradle too.
But I agree, sometimes we have to combine this with invalidating caches or just project reopen, or deleting .idea directories.

IntelliJ cannot resolve any symbols or methods

IntelliJ IDEA 2017.2
I know this has been asked, but I have tried every fix I could find, as shown below.
Every symbol in my java code has an error Cannot resolve method, or Cannot resolve symbol. However, the code still compiles and runs with 0 errors. The error messages in the IDE started the first time I opened the project, on 3 separate computers, but runs fine on a fourth....I can't find anything different on that fourth computer.
I have following the following instructions from this issue:
File -> Invalidate Caches / Restart
I deleted the [IDEA system](https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs) directories, and re-imported the Maven project.
I have re-imported the Maven project
I re-imported the project with auto-import
I also followed the instructions from this issue:
File -> Synchronize
then
File -> Invalidate Caches / Restart
As well as the solutions here:
Check source roots
Ctrl+Alt+Shift+S
There were no errors or fixes in the Module source roots, or the module dependencies.
I also tried re-importing with "Search for projects recursively" checked as recommended here.
Edit1
This isn't a screenshot from my actual code, but my errors look the same as the screenshots from this post.
EDIT2
I just found an error I hadn't seen before. When I go to Project Structure (Ctrl+Alt+Shift+S), under Libraries, I see the following errors.
I checked on the location shown /home/user/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7-sources.jar and it doesn't exist. I have antlr-2.7.7.jar, but I'm not sure if I need to download the antlr-2.7.7.7-sources.jar, or just point my Library to the .jar I have (as I'm unfamiliar what these settings do)
The answer was the look through the pom.xml file line by line, and see what the dependencies were that I didn't have. I thought it was a configuration issue, but it wasn't.
I didn't notice that I didn't have the lombok plugin, which was a dependency for my project.
The problem listed in EDIT2 wasn't actually an issue since all of the classes were available.
Since your project compiles and runs successfully with mvn clean install, you know your pom is valid.
Given that, I would recommend to:
CloseIntelliJ
delete the .idea directory and any *.iml, *.ipr files for each of your modules. Don't worry, IntelliJ will re-create them. Back them up by renaming them if you're really concerned.
Open IntelliJ and find the new project wizard.
Find the option to import from an existing maven project
Select the pom for your root project
Check if module settings are correct. Open Project structure (Ctrl+Alt+Shift+S) and check the source directory settings under Source Tab are valid.
This is a weird one. I faced the same issue and went through all questions on stackoverflow and other forums and applied all suggestion to an extent to reinstalling intelliJ.
Though what problem in my case was that i had excluded some files namely .gradle and build folder, which definitely sounds important. I must have done this get excluded for git status notifs but somehow i had excluded it from my project as i am new to intellij. To check if any of your folders are exclude goto -> modules CTRL + Shift +Alt + s and check for any red items you should probably include the ones necessary here.
Hope this helps few of those out there.
you are using maven project, delete .m2 folder and update maven project and clean the project the rebuild it and run it... some time maven project occur that type of issue, which resolve by rebuilding the project. also check the version of your dependency.

Resource file is missing with maven

I'm working on a GWT project with maven. Since I want to keep my workspace organized I'm putting all my resources on src/main/resources instead of src/main/java. The problem is that eclipse gives an compile error saying that the file is missing. On the other hand if I insist in proceding the project compiles and works properly (it does find supposedly missing resources). How can I correct this?
Thank you.
Remove the project from Eclipse and afterwards delete the Eclipse specific files (.settings, .classpath, .project) and import the project from scratch. The problem is based on the thing that the life-cycle is started from scratch in case of importing it. If a project is already important some parts seemed to be not executed.

How to import a Jar into Eclipse

I have been making something in eclipse until just recently I had some technical issues causing my hard drive to be completely destroyed and I lost everything except a jar file.
I wish to start working on it again but am not sure what to do since I have no source file just the jar. I tried decompiling and importing except it made a mess of my code and will take forever to clean it up. Hopefully I did something wrong and there is a much easier way to do this.
If you don't need the sources of the JAR you can make a dependency to it within eclipse (right click on project -> Build Path -> Dependencies).
Or if you use a build tool (maven, gradle, etc.) put the jar to your local/remote repository and put it as dependency to your build file.
Otherwise decompile the JAR for e.g. with Java Decompiler and put the java files to your eclipse project.

How to correctly version control (svn:ignore) a Java project (Maven, Spring)?

i was on a 2-day training introducing as to Java EE. We used there Java EE, Spring Framework, Maven, Springsource Tool Suite (Eclipse), Tomcat.
I took the Eclipse workspace we created there and run it on my work PC. I had, if i remember correctly, only to configure Tomcat properly, and it worked on my PC.
Now i want to save the created Eclipse workspace containing 5 "sub"-projects in subversion so that my work colleagues can checkout this to them and run it on their computers.
How to do this correctly? I found somewhere a svn:ignore rule:
.classpath
.project
.settings
target
Using tortoiseSVN i added to the folder with the workspace this ignore rule, but the found out that the underlying folders target were'nt deleted so i deleted them manually and "added to ignore list". But after that the project in spring source tool suite does not see the mevan dependencies (i think so) because the imports are broken. STS underlines org. in the imports and says it can not resolve this.
How do i correctly version control such a project?
In my project we are using Maven and Eclipse (Helios, currently) and the Maven plugins for Eclipse:
Maven Integration for Eclipse
Maven Integration for WTP
We only have the pom.xml file and the src/ directory tree in our version control system. We make sure not to add the eclipse files there. Then when a new developer starts in the project they do Import -> Maven -> Existing Maven Projects. The Maven plugins for Eclipse then set up perfect build paths, settings and so on.
This way it is also very easy to re-import your projects into Eclipse as needed.
So, my tip is to leave the Eclipse files out of SVN and make sure you can setup the project correctly automatically simply by importing a Maven project.
If I understand your problem the right way, you need to configure Eclipse in order to be able to launch tomcat from it. The key, here, is not maven anymore, but Eclipse, I think. As you've made modifications in your workspace that can't be put in your maven configuration file (the pom.xml), you become "Eclipse dependant".
The key here is that, as you're Eclipse dependant, you need the Eclipse configuration files to work. Consequently, I'm afraid you need to add back .classpath, .project, .settings to your versioning tool... It's not generic, because you force people who work on your project to use Eclipse. But if everybody in your team do so, it shouldn't be a problem.
As I don't use Eclipse anymore, I don't know if versioning theses files can lead to problems. However, I hope this answer will help you to configure your project back...
EDIT : to be more accurate... and maybe give a better answer.
When using a version control system, the main goal is often (always ?) to give all the keys to use the sources, and develop from them. Consequently, you need to put in your VCS your sources, and all the configurations needed to use them efficiently.
In your specific case, the key is that you've become Eclipse dependant through its Springsource Tool Suite plugin. Consequently, it becomes essential to add the configuration files for this tool, because they can't work without them, and if they can't work, you can't work.
I can tell you my way of subversioning maven eclipse projects. First, when you create the project structure you have to commit the .setting, .classpath, .project files into the subversion repository. If you can't do this the other colleagues will can not use the project structure after checkout. After you commit the project structure, the best way is not to commit these files except only when you change something important eclipse or build path settings, because the others will have conflicts due to the system dependent informations. Never commit the maven target directory. Sorry for my english. Hope it helps.

Categories