将 CSS 和 JS 文件放在 Mavenized Java Web 应用程序中的最佳位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2238973/
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-13 05:07:04  来源:igfitidea点击:

Best location to put your CSS and JS files in a Mavenized Java Web app?

javajavascriptcssmaven-2war

提问by MexicanHacker

I have a Web application in maven that follows this structure:

我在 maven 中有一个遵循以下结构的 Web 应用程序:

src
`-- main
    |-- java
    |-- resources
    `-- webapp

Is is better to put it in the main folder and then the maven plug in will try to move it or I should put it inside the webapp folder and why?

是将它放在主文件夹中,然后 maven 插件会尝试移动它还是我应该将它放在 webapp 文件夹中,为什么?

采纳答案by Pascal Thivent

If you don't need to filter CSS and JS files, I would simply put them in src/main/webapp(if you put them in src/main/resources, they will end up in target/classesand in WEB-INF/classesin the WAR which is very unlikely what you want). If you need to filter them, additional resources can be included in the WAR using the webResourcesparameter. Have a look at Adding and Filtering External Web Resourcesfor more details.

如果您不需要过滤器CSS和JS文件,我只想把他们src/main/webapp(如果你把他们src/main/resources,他们最终会在target/classesWEB-INF/classes你想要的东西,这是非常不可能的WAR)。如果您需要过滤它们,可以使用webResources参数将其他资源包含在 WAR 中。有关更多详细信息,请查看添加和过滤外部 Web 资源

回答by lucrussell

I think they should be under src\main\webapp. The main rationale for saying this is that this is the standard suggestedby Maven, and the Maven philosophy is that all projects should follow the same structure as much as possible.

我认为他们应该在src\main\webapp. 之所以这么说,主要是因为这是Maven建议的标准,而 Maven 的理念是所有项目都应尽可能遵循相同的结构。

回答by TMarin

Taken from Introduction to the Standard Directory Layout:

取自标准目录布局简介

"If there are other contributing sources to the artifact build, they would be under other subdirectories: for example src/main/antlrwould contain Antlr grammar definition files."

“如果工件构建有其他贡献源,它们将位于其他子目录下:例如src/main/antlr将包含 Antlr 语法定义文件。”

So I think src/main/javascriptand src/main/cssis the right place...

因此,我认为src/main/javascript并且src/main/css是正确的地方...

What do you think?

你怎么认为?

回答by Dave McLure

I am seeing quite a variation of answers to this question. One set of threads (like some people in this thread) seem to think that Maven apps should place css/images/js files into the src\main\webapp folder - similar to the traditional J2EE pattern. Others feel that the resources (or another similar peer) folder outside of webapps should be used instead:

我看到这个问题的答案多种多样。一组线程(如该线程中的某些人)似乎认为 Maven 应用程序应该将 css/images/js 文件放入 src\main\webapp 文件夹 - 类似于传统的 J2EE 模式。其他人认为应该使用 webapps 之外的资源(或另一个类似的对等)文件夹:

What are the conventional locations for for JSPs, JavaScript, CSS, Images in Maven web projects?

Maven Web 项目中 JSP、JavaScript、CSS、图像的常规位置是什么?

I have yet to form an opinion, but it would seem to make sense to separate these client side types of files from the server side - if only because in high performance focused production situations, the static client side files such as these are typically separated out and placed into a cacheable front end web server for faster access anyway, so keeping them in their own separate resources folder would seem to make this job easier.

我还没有形成意见,但将这些客户端类型的文件与服务器端分开似乎是有意义的——如果仅仅是因为在专注于高性能的生产情况下,像这样的静态客户端文件通常被分开并放置在可缓存的前端 Web 服务器中以便更快地访问,因此将它们保存在自己单独的资源文件夹中似乎会使这项工作更容易。