Java Eclipse 无法识别 src/main/webapp 目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1661889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:50:54  来源:igfitidea点击:

src/main/webapp directory not recognized by Eclipse

javamaven-2directory-structurem2eclipse

提问by KLE

I use m2eclipseto import Maven Java projects in Eclipse.

我使用m2eclipse在 Eclipse 中导入 Maven Java 项目。

It fails to recognize src/main/webappas a source directory.

它无法识别src/main/webapp为源目录。

Graphically in the package explorer (or when I look into Java-Build-Pathin the project's properties),
this directory isn't in the list of sources folder (while src/main/javaor src/main/resourcesdo).

以图形方式在包资源管理器中(或当我查看项目属性中的Java-Build-Path时),
此目录不在源文件夹列表中(whilesrc/main/javasrc/main/resourcesdo)。

To access it, I have to look directly into the src/ directory, and start unfolding... Not very convenient!

要访问它,我必须直接查看 src/ 目录,然后开始展开......不是很方便!

However, if I run maven install, the resources are copied to the correct directory.
(example : src/main/webapp/index.jspto target/mywar/index.jsp)

但是,如果我运行maven install,资源将被复制到正确的目录。
(例如: src/main/webapp/index.jsptarget/mywar/index.jsp

Questions

问题

  1. Is this a correct behavior ? (I was thinking it could be treated as other resources ...)
  2. Could I fix it?
  3. I wonder if there are other drawbacks for this situation, that I would not be aware of right now?
  1. 这是正确的行为吗?(我以为它可以被视为其他资源......)
  2. 我能修好吗?
  3. 我想知道这种情况是否还有其他缺点,我现在不知道?

采纳答案by Pascal Thivent

  1. Is this a correct behavior ? (I was thinking it could be treated as other resources ...)
  2. Could I fix it?
  3. I wonder if there are other drawbacks for this situation, that I would not be aware of right now?
  1. 这是正确的行为吗?(我以为它可以被视为其他资源......)
  2. 我能修好吗?
  3. 我想知道这种情况是否还有其他缺点,我现在不知道?
  1. Yes, to me this is the correct behavior.
  2. It doesn't really make sense for src/main/webapp to be a source folder, it doesn't contain compilable source files.
  3. I don't know. I guess it depends on your expectations :)
  1. 是的,对我来说这是正确的行为。
  2. 将 src/main/webapp 作为源文件夹实际上没有意义,它不包含可编译的源文件。
  3. 我不知道。我想这取决于您的期望:)

That said, m2eclipse made a contribution that allows to make src/main/webappavailable at the top level with a specific "Web Resources" label, something like this:

也就是说,m2eclipse 做出了贡献,允许src/main/webapp使用特定的“Web 资源”标签在顶层可用,如下所示:

alt text

替代文字

This could be a solution for your concern (the not convenient folding).

这可能是您关注的解决方案(不方便折叠)。

回答by NickDK

Well you just have to mark the src/main/webapp as an Eclipse src folder:

好吧,您只需将 src/main/webapp 标记为 Eclipse src 文件夹:

right click webapp folder --> build path --> use as source folder

右键单击 webapp 文件夹 --> 构建路径 --> 用作源文件夹

Don't think you can easily change this behavior without any ugly hacks, the src marking is just something Eclipse specific that it uses to configure its classpath etc.

不要认为您可以在没有任何丑陋黑客的情况下轻松更改此行为,src 标记只是 Eclipse 特定的东西,它用于配置其类路径等。

回答by Jim Downing

As far as maven is concerned, src/main/webappisn't a source folder in the sense that it's contents aren't compiled / copied to target/classes, so from m2eclipse's point of view, this is correct behaviour. Is there a particular reason you need src/main/webapp to be marked as an Eclipse source folder?

就 maven 而言,src/main/webapp它不是源文件夹,因为它的内容没有被编译/复制到target/classes,所以从 m2eclipse 的角度来看,这是正确的行为。是否有特殊原因需要将 src/main/webapp 标记为 Eclipse 源文件夹?

回答by Nestor Rodriguez

I can think of a reason.

我能想到一个原因。

I'm using Maven for developing a GWT project in Eclipse. In my configuration, the src/main/webapp directory contains the HTML and CSS files that I have to refer to as part of my development effort, even though they're not "compilable" files.

我正在使用 Maven 在 Eclipse 中开发 GWT 项目。在我的配置中,src/main/webapp 目录包含 HTML 和 CSS 文件,我必须在开发工作中引用这些文件,即使它们不是“可编译”文件。

Being able to get to them directly as opposed to diving into the src/ folder is a plus. And defining it in the POM so that it gets set up that way means I don't have to manually keep adding the folder every time I run eclipse:eclipse.

能够直接访问它们而不是潜入 src/ 文件夹是一个加分项。并在 POM 中定义它以便以这种方式进行设置意味着我不必每次运行 eclipse:eclipse 时都手动添加文件夹。

回答by campbell

To Nestor's point, no kidding. It's an end-to-end development environment and not just a compiler. It's intended as an interface for all source artifacts contributing to a build from JSP's to build configuration such as POM files. Why would eclipse even bother having XML and JSP editors if these aren't compile-able? --Which by the way, are super-useful.

就内斯特而言,这不是开玩笑。它是一个端到端的开发环境,而不仅仅是一个编译器。它旨在作为所有源工件的接口,这些工件有助于从 JSP 构建配置,例如 POM 文件。如果 XML 和 JSP 编辑器不可编译,那么为什么 Eclipse 还要费心呢?——顺便说一句,超级有用。

The maven plugin for eclipse (or vice-versa) is terrific because it builds the .classpath, .project, and other configuration needed to immediately begin working with maven projects in eclipse. However, each time the eclipse config is generated (i.e. mvn eclipse:eclipse), unsupported directories must be added manually. That's a major suck.

Eclipse 的 Maven 插件(或反之亦然)非常棒,因为它构建了 .classpath、.project 和其他配置,以便立即开始在 Eclipse 中使用 Maven 项目。但是,每次生成 eclipse 配置(即 mvn eclipse:eclipse)时,必须手动添加不受支持的目录。这是一个重大的问题。

The fact that these plugins neglect the ability to easily add new source folders viewable from within the IDE is a mystery to me. One maven/eclipse plugin project page even cites work-arounds for making these sources temporarily available by switching the project types and fooling the plugin--of course, you need to change it back immediately or corrupt the build.

这些插件忽略了轻松添加可从 IDE 中查看的新源文件夹的能力,这一事实对我来说是个谜。一个 maven/eclipse 插件项目页面甚至引用了通过切换项目类型和欺骗插件来使这些源暂时可用的变通方法——当然,您需要立即将其改回或破坏构建。

Why don't they just create the ability to independently control the source declarations in .classpath???

他们为什么不创建独立控制 .classpath 中的源声明的能力???

回答by Archie

In my case, Eclipse failed to recognise src/main/webappas the Web Resources folder. I resolved it by:

就我而言,Eclipse 无法识别src/main/webapp为 Web 资源文件夹。我通过以下方式解决了它:

  1. Deleting the project
  2. Restarting eclipse
  3. Taking a fresh update of the Maven project form the SVN.
  1. 删除项目
  2. 重启日食
  3. 从 SVN 对 Maven 项目进行全新更新。

回答by thetoolman

Instead of adding /src/main/webapp as a source folder in the java build path, add it as a folder to include in the deployment assembly:

不是将 /src/main/webapp 添加为 java 构建路径中的源文件夹,而是将其添加为文件夹以包含在部署程序集中:

MyProject -> properties -> Deployment Assembly: Add "/src/main/webapp" deploys to "/"

MyProject -> 属性 -> 部署程序集:添加“/src/main/webapp”部署到“/”

回答by Shiva Prasad

I found a whole different reason for the same problem I was facing - I would not see src/main/webapp in the package explorer.

对于我面临的同一问题,我发现了一个完全不同的原因 - 我不会在包资源管理器中看到 src/main/webapp。

Turned out it had nothing to do with either POM or with Eclipse (using SpringSource Suite 3.5.0) being able to load up maven project.

原来它与 POM 或 Eclipse(使用 SpringSource Suite 3.5.0)能够加载 maven 项目无关。

The problem was that inside my src/main/webapp/Scripts/ folder there were .git and .gitignore files. I suspect that may be Eclipse was not able to deal with file names begining with a "." in the webapp folder. I tested this theory across multiple projects and it seems to hold ground so far.

问题是在我的 src/main/webapp/Scr​​ipts/ 文件夹中有 .git 和 .gitignore 文件。我怀疑可能是 Eclipse 无法处理以“.”开头的文件名。在 webapp 文件夹中。我在多个项目中测试了这个理论,到目前为止它似乎站得住脚。

I hope this helps.

我希望这有帮助。

回答by user1050755

One solution is to exploit m2e support for build helper maven plugin and declare src/main/webapp as an additioanl resource path. This also makes eclipse copy saved resources to the build target folder structure.

一种解决方案是利用 m2e 对构建助手 maven 插件的支持,并将 src/main/webapp 声明为附加资源路径。这也使得 eclipse 将保存的资源复制到构建目标文件夹结构中。

        <!-- help eclipse identify the webapp folder as a resource folder: -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/webapp</directory>
                                <targetPath>${project.build.directory}/${project.build.finalName}</targetPath>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

回答by Maxime Flament

Many answers claim that it doesn't make sens to have src/main/webappas a source folder because JSP files are compiled by the container and not maven during build cycle.

许多答案声称将其src/main/webapp作为源文件夹没有意义,因为 JSP 文件是由容器编译的,而不是在构建周期中由 Maven 编译的。

However, it does make sensewhen you're doing security testing:

但是,当您进行安全测试确实有意义

Some security scanning tools provide plugins that can be integrated into IDEs so that development teams can locally scan their code and fix basic vulnerabilities on their machine, thus reducing the amount of work for security teams that can therefore work on more advanced testing.

一些安全扫描工具提供可以集成到 IDE 中的插件,以便开发团队可以在本地扫描他们的代码并修复他们机器上的基本漏洞,从而减少安全团队的工作量,从而可以进行更高级的测试。

The fact that Eclipse excludes this specific directory is a huge problem: by doing that, security tools won't include by default the webapp/directory into the scan process, which results in false negatives(actual vulnerabilities that aren't reported by the tool). This behavior then leads development teams into thinking that their JSP pages (for example) are safe from a security point of view.

Eclipse 排除这个特定目录的事实是一个巨大的问题:通过这样做,安全工具默认情况下不会将该webapp/目录包含在扫描过程中,这会导致漏报(该工具未报告的实际漏洞)。这种行为会导致开发团队认为他们的 JSP 页面(例如)从安全的角度来看是安全的。

If security testing teams don't pay attention to the scans conducted by development teams (sometimes we can't even check which scans have been performed locally), the product is more likely to be released with vulnerabilities in code contained in the webapp/folder.

如果安全测试团队不关注开发团队进行的扫描(有时我们甚至无法检查本地执行了哪些扫描),则产品更有可能在webapp/文件夹中包含的代码中存在漏洞。

Thus, this behavior doesn't make sense from a security point of view, and people claiming it does are missing some points.

因此,从安全的角度来看,这种行为没有意义,声称这样做的人缺少一些要点。

In any case, I solved this problem by adding the src/main/webapp/folder to the build path by right clicking it in the package explorer--> build path--> use as source folder

无论如何,我通过src/main/webapp/package explorer--> build path--> 中右键单击将文件夹添加到构建路径来解决此问题use as source folder