困惑Intellij如何编译和处理资源 [英] Confused about how Intellij compiles things and deals with resources

查看:248
本文介绍了困惑Intellij如何编译和处理资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Visual Studio的IntelliJ是新的,我通常很困惑的资源文件如何工作在IntelliJ项目。我读了几个问题&关于获取资源到类路径,以及IntelliJ docs,但仍然困惑的答案。我还有其他一些问题:

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. 有些问题提到maven,有些问题不提。

  2. 当我从内部运行 main 方法时,maven项目与正常模块在编译和处理资源方面有什么不同? IntelliJ,它从哪里运行它?直接从编译的 .class 文件,或从生成的 jar

  3. 如果我只是把一个 .xml 文件放在源文件中的某个地方树,当IntelliJ中编译和运行时,IntelliJ默认使用它做什么?

  4. 在IntelliJ中运行 main 函数时,类路径是什么?

  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. 在Maven项目中,IDE遵循Maven规则来处理资源,以便在命令行Maven构建和IDEA构建之间获得一致的结果。如果Maven将资源复制到类路径,IDEA也会这样做。

  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.

从模块的类路径及其依赖关系,通常是

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

在非Maven项目中,资源根据设置 | 编译器 | 资源模式。在Maven项目中,根据 pom.xml 资源规则处理资源。来自 resources 文件夹的文件会自动复制。其他文件夹需要使用 pom.xml 手动添加到资源。

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.

在Maven项目中,使用 resources 文件夹。在非Maven项目中,文件应该在源根下并且其扩展名需要存在于资源模式配置中。

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.

另请注意,如果您使用的是旧版编译(外部版本已禁用),IDEA将使用资源模式配置,即使在Maven项目中,IDEA和Maven之间的输出也会有所不同。这同样适用于IDEA 11和旧版本。

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.

这篇关于困惑Intellij如何编译和处理资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆