在战争中打包 Javascript 文件?

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

Packaging Javascript files in a war?

javajavascriptmaven-2

提问by sheki

I am developing a Servlet based Java project which is to be packaged as a war using Maven. Is there a way I can include JavaScript (JS) files along with this project (they should be available at some url when the project loads on a Tomcat Server).

我正在开发一个基于 Servlet 的 Java 项目,该项目将使用 Maven 打包为一场War。有没有办法可以将 JavaScript (JS) 文件与这个项目一起包含(当项目加载到 Tomcat 服务器上时,它们应该在某个 url 上可用)。

I have looked around but have not found any working solutions.

我环顾四周,但没有找到任何可行的解决方案。

回答by Romain Linsolas

Maybe the better solution is to stick to Maven convention, which specifies that the root directory of your Web application is src/main/webapp.

也许更好的解决方案是坚持 Maven 约定,它指定您的 Web 应用程序的根目录是src/main/webapp.

So if you put all your Javascript files in src/main/webapp/javascript(or src/main/webapp/js), they will be integrated in your final war package.

因此,如果您将所有 Javascript 文件放在src/main/webapp/javascript(或src/main/webapp/js) 中,它们将被集成到您的最终 war 包中。

In the Maven WAR plugin, they give some descriptions (see herefor example) about the content of the directories. For example:

在 Maven WAR 插件中,他们给出了一些关于目录内容的描述(例如参见此处)。例如:

 |-- pom.xml
 `-- src
     `-- main
         |-- java
         |   `-- com
         |       `-- example
         |           `-- projects
         |               `-- SampleAction.java
         |-- resources
         |   `-- images
         |       `-- sampleimage.jpg
         `-- webapp
             |-- WEB-INF
             |   `-- web.xml
             |-- index.jsp
             `-- jsp
                 `-- websource.jsp

As you can see, you can put resources in webapp/xxxdirectory, such as jspfile here.

如您所见,您可以将资源放在webapp/xxx目录中,例如jsp此处的文件。

As stated by cuh, you can also configure the Maven WAR plugin if your directory structure is different.

如 cuh 所述,如果您的目录结构不同,您还可以配置 Maven WAR 插件。

回答by cuh

You can use the maven-war-pluginand configure it something like this:

您可以使用maven-war-plugin并像这样配置它:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-war-plugin</artifactId>
       <version>2.0.2</version>
       <configuration>
           <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
           <webResources>
                <resource>
                    <directory>your/path/to/js</directory>
                    <targetPath>js</targetPath>
                </resource>
           </webResources>
       </configuration>
</plugin>

See here.

这里

edit: You probably don't want to include the jsinto the classesFolder.

编辑:您可能不想将 包含jsclasses文件夹中。

回答by Powerlord

Put them in directories under src/main/webapp. That is, assuming you generated it using the maven-archetype-webapparchetype.

将它们放在 src/main/webapp 下的目录中。也就是说,假设您使用maven-archetype-webapp原型生成它。

My current app has them under src/main/webapp/javascript, and accesses them in JSPs like this:

我当前的应用程序在 src/main/webapp/javascript 下有它们,并在 JSP 中访问它们,如下所示:

<script type="text/javascript" src="javascript/jquery-1.4.2.min.js"></script>

(for jsp files in src/main/webapp that is)

(对于 src/main/webapp 中的 jsp 文件)