xml files are not copied to target intellij idea - intellij-idea

I have some xml files within the source folder along with the java files.
Intellij Idea is not copying them to the target folder.
I want those xml files to be along with the classes in the target.
Eclipse does this fine.
Can anyone please tell how to achieve this in Intellij Idea?

If, like me, the other answer didn't solve your problem -
If IDEA thinks your project is a Maven module (or once was), and it isn't, it'll behave like this. You can fix this by closing the project, editing the .iml file to remove the isMavenModule property, reopening, and rebuilding:
<moduleorg.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true"type="JAVA_MODULE" version="4">

In its default configuration, IDEA should copy any xml file that are in a source directory into the target directory. Other build tools may not. So this answer is assuming you are making the project via IDEA's internal build/make. If using maven (or gradle), you should place your resources in src/main/resources and not src/main/java as Engineer Dollery mentions in his/her comment.
If you are using the IDEA builds and it is still not copying them over, there is a setting you can check. Go into Settings > Build, Execution, Development > Compiler. On the Compiler settings dialog at the top is a "Resource Patterns" text field. This will define what files IDEA will and will not copy over. The default entry is:
!?*.java
!?*.form
!?*.class
!?*.groovy
!?*.scala
!?*.flex
!?*.kt
!?*.clj
!?*.aj
It is just a set of negation patterns of things not to copy over. So XML files should be copied. Check this setting and see if there is something in it to prevent xml files from being copied.

Related

IntelliJ IDEA directory-based format and .iml file(s)

We started a directory-based project in IntelliJ IDEA (currently using version 2016.3 build 163.7743.44). Everything is fine, the .iml file is inside .idea/modules and we can easily share/update it.
The issue is that, sometimes, when someone clones the project and start using it the IDE doesn't care about that file (or project structure) and puts an .iml outside the project's root. It's an intermittent/random issue and I'm not able to reproduce it step by step.
We opted for deleting the .idea directory from Git and keep the configurations/settings in a JAR file.
Is there any way to tell the IDE to use one approach or the other? It's not a big deal, but I would like to know what's the reason behind that behavior.
The reason for that behavior may be that people don't use the "Open" action to open the project but use the "Import project from existing sources" action instead. The latter action does not use the existing .idea directory, but instead recreates the project structure from scratch. You should teach your colleagues not to use that action.
Note that storing the settings in a .jar file is not a replacement for sharing the project structure, because shared settings do not include project-specific items such as libraries and run configurations.

Java sources replaced by decompiled files in Intellij

When I'm opening a specific class using ctrl + right click I should be redirected to a window with the java sources which contains all the lines and the appropriate comments. But instead I get a window with the decompiled .class file.
I have tried to deactivate the decompiler plugin but it does not work.
If I add http://docs.oracle.com/javase/8/docs/api to Project Structure -> SDKs -> Documentation Paths I can get an overview of the class or method's I'm rolling over, but still can't open the right source file.
Any ideas?
When navigting to sources, IDEA will first go to attached sources. If not present, it will decompile the class and display it.
Try the following troubleshooting to track down the issue:
1. Make sure the sources are properly attached.
For libraries, Open the Project Structure dialog (Ctrl+Alt+Shift+S or ⌘;) and sel4ct "Libraries" on the left under "Project Settings". Find the library in question, and select it. It will show if the source is attached. You mentioned you have Maven's "Automatically download" sources/documentation both checked. So for any maven libraries that have source available, it should be listed.
If it is not listed or is in red, run "Download Sources" or "Download Sources and Documentation" from the Maven tool window. If the source is till in red, it likely means it is not available for that artifact in the maven repository. Use Maven Search to verify such.
For the JDK, select "SDKs" in the Project Structure dialog, find the JDK, and then select the "Sourcepath" tab. Make sure the {jdk-home}/src.zip file is listed.
IDEA should automatically set this when you add the JDK, but if not, add it.
2. Make sure the source JAR/ZIP file is not corrupt
Navigate to the archive in explorer/finder and make sure it can be opened and viewed.
3. Refresh IDEA's Caches
It's possible that IDEA's caches/indexes are corrupted. As such, IDEA is not "seeing" the link between the class and the source code. Go to File > Invalidate Caches / Restart...". Invalidate the caches and restart IDEA. Wait for IDEA to finish re-indexing the project and libraries (watch the progress in the bottom right of the IDEA window).
One of the above should resolve the issue.
FYI, If you do not want IDEA to decompile code(as a backup to no source being present), to the best of my knowledge (and based on the its help page), the only way to do such is to disable the "Java Bytecode Decompiler" in the Plugins list.
After ensuring my sources were attached, checking the source ZIP, invalidating the IntelliJ cache and restarting, I was still having the same problem.
To fix it, I created the project anew File > New > Project From Existing Sources..., and when I got to the library selection step I deselected the .jars for my own modules.

Configure automatic excluded directories and other directory types when they are present

In the Community Edition when I import a module IntelliJ goes through and tries to figure out which directories are what types. The modules I deal with sometimes have a src and a test directory, which work fine, but more often than not the test directory is actually named tst. IntelliJ likes to mark is as a "Sources" directory rather than "Tests". With the tools I am working with, the dependency configuration and generation of the .iml file happens after this, as well as using a modified build system that is not IntelliJ supported. I have to build via commandline which generates a build folder which I would like to have automatically excluded.
So a couple questions.
Can I configure IntelliJ to automatically recognize different folder names "Tests" and mark them as such?
Can I configure IntelliJ to automatically mark a folder as excluded when it is present in a module?
You can't configure it, the list is hard-coded. However, I've added "tst" to the list of names that are recognized as test source roots; that fix will be included in IntelliJ IDEA 14.1.
You can add "build" to Settings | File Types | Files and folders to ignore. Then IntelliJ will ignore the directories and files named "build" anywhere in your project structure. Given that "build" is a fairly common name, this may or may not be what you want.

In IntelliJ IDEA, how to copy non-source assets to output folder during build?

I have a project in IntelliJ IDEA, inside that a couple of modules and one of my modules has two build configurations. One of them needs to copy a <projectroot>/tools folder to its out/production/<BuildConfigurationName> folder. Can IDEA somehow automate this?
The accepted answer above is incorrect. IDEA can do this (without ant/gradle) via the artifacts system (accessed via Build menu or project settings). Any one artifact job copies multiple files/folders/build outputs to a chosen location (optionally jarred) and can be set to automatically run on make.
Artifacts can even be chained, i.e. output from one as input to another.
Can IDEA somehow automate this?
Not directly, no. Ultimately IDEA is an IDE and not a build tool. While it can do a lot during a build, it does not have the ability to copy non-source files to an alternate directory, let alone a dynamically named directory.
If you marked the tools directory as a source directory (and none of its contained file types were set in the "Ignore files and folder" setting at the bottom of the "File Types" settings dialog), IDEA would then copy the tools directory to the out directory. But renaming requires a more sophisticated build tool.
Ultimately, the "ideal" or "best practices" solution would be to build your project using a build tool like Maven, Gradle or Ant for which this type of thing would be a snap.
If that is not an option, or for some reason you really want IDEA to do the build, the best thing you could do is to write a simple Ant script to the copy for you. (Or possibly Gradle, I do not have much experience with Gradle yet. Maven could do it, but it'd be a bit cumbersome compared to Ant.) In any Run/Debug configurations, you can define the ant script target to run before or after the IDEA "make" in the Before Launch section. (You can set that as a default for any newly created configurations by configuring it in Defaults on the left). If you run your build manually, you can assign a shortcut to the ant build and then run it and the make in sequence. Alternatively, you could record a Macro (Edit > Macros) to run both in sequence and then (optionally) assign the macro a keyboard shortcut.

"Duplicate class found" - IntelliJ and .class files

I recently imported one of our company's project into IntelliJ Idea (10.5.1). We build and run the project using an ant build script and IntelliJ supports that just fine.
However, IntelliJ seems to have a distinct problem when the compile output directory equals the source code directory, ie .class files are placed in the same directories as their corresponding .java sources.
(Note that I am aware that is not a proper way to go, but tell my boss that. This project is over 15 years old and correspondingly large, too many things depend on it to be this way, there is nothing I can do about that.)
So once things are compiled, IntelliJ detects the .class files and adds them to the project tree. The problem here is that it considers them class declarations, thus I get a "duplicate class found" message for each and every class. This doesn't make me unable to work, but it is extremely annoying as you may guess.
I tried making the IDE ignore .class files, but apparently that makes it not load any classes at all, including the JRE runtime and anything else located inside of .jar files.
Is there any way to make IntelliJ Idea ignore .class files which are in the same location as their .java sources?
Make sure that you've configured the output directory to the source directory, disable the Exclude of the output directory to see your files.
I coped with this same problem when cloning a project from Bitbucket. To solve it in IntelliJ:
Project Structure > Modules > Source > Source folder > <<"Eliminate the unwanted source folder">>
In my case, I had non-implemented classes in one source folder and a second source folder with the implemented classes (same class names).
I deleted one, built again, and the problem got solved.
Give it a try!