Confused about how Intellij compiles things and de

2020-04-17 07:40发布

问题:

I'm quite new to IntelliJ from visual studio, and I'm generally quite confused about how resource files work in an IntelliJ project. I'm read a few questions & answers about getting resources onto the classpath, as well as the IntelliJ docs, but am still confused. I have a few further questions:

  1. Some questions mention maven, some not. How do maven projects differ from normal modules in how they compile things and deal with resources?
  2. When I run a main method from within IntelliJ, where does it run it from? Directly from the compiled .class files, or from the generated jar? Where do both those exist in the filesystem, and whats the difference?
  3. If I just put an .xml file somewhere in the source tree, what does IntelliJ do with it by default when compiling and running from within IntelliJ? How can I change where it puts it?
  4. What is the classpath when running a main function within IntelliJ?
  5. How do you get an arbitary file included in a jar file or alongside it in the compiled output?

回答1:

  1. In a Maven project IDE follows Maven rules to process resources so that you will get the consistent results between the command line Maven build and IDEA build. If Maven copies resources to classpath, IDEA will do the same.

  2. From the classpath of the module and its dependencies, normally a .class file is used.

  3. In a non-Maven project resources are copied to the output according to Settings | Compiler | Resource Patterns. In a Maven project resources are handled according to pom.xml resources rules. Files from resources folder are copied automatically. Other folders need to be added to the resources manually using pom.xml.

  4. You can view the classpath in the command line that is printed in the Run console.

  5. In a Maven project use the resources folder. In a non-Maven project the file should be under a source root and its extension needs to be present in the Resource Patterns configuration.

Also note that if you are using a legacy compilation (External Build disabled), IDEA will use Resource Patterns configuration even in Maven projects and the output between IDEA and Maven will be different. The same applies to IDEA 11 and older versions.