用于版本控制和缩小 javascript 的 Maven 插件

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

Maven plugin for versioning and minifying javascript

javascriptgitbuildmavenminify

提问by Tauren

Single Page Javascript Application

单页 Javascript 应用程序

I have built a sophisticated ajax-driven single page webapp that uses a RESTful backend web service serving JSON. The javascript is split into many different files, each file representing some sort of feature or component.

我已经构建了一个复杂的 ajax 驱动的单页 web 应用程序,它使用一个 RESTful 后端 web 服务来提供 JSON。javascript 被分成许多不同的文件,每个文件代表某种功能或组件。

While the service has been in alphatesting, I've just be serving all these files separately without minification. But now that I'd like to launch a betaversion, I really need to combine files, minify them, and version them. I want to add this to my build process, using Maven.

虽然该服务一直处于alpha测试阶段,但我只是在没有缩小的情况下分别提供所有这些文件。但是现在我想推出一个测试版,我真的需要合并文件,缩小它们并对其进行版本控制。我想使用 Maven 将此添加到我的构建过程中。

Javascript File Types

Javascript 文件类型

I'm using the following "types" of javascript files, of which #3 and #4 are my concerns:

我正在使用以下“类型”的 javascript 文件,其中 #3 和 #4 是我关注的问题:

  1. External files, such a jquery and jquery-ui served from the Google CDN. Rarely change these versions, can be handled manually.
  2. Jquery plugins that I'm hosting myself, such as fullcalendar or ui-layout. Again, I rarely update these to new versions and can handle it manually.
  3. Application-wide javascript code. Custom javascript that is spread across many files and can change occasionally. All of these files need to be loaded for the app to work.
  4. Feature-specific javascript code. Custom javascript that is loaded on demand when a specific feature is requested. This code can change quite frequently and is not loaded at startup.
  1. 外部文件,例如从 Google CDN 提供的 jquery 和 jquery-ui。很少更改这些版本,可以手动处理。
  2. 我自己托管的 Jquery 插件,例如 fullcalendar 或 ui-layout。同样,我很少将这些更新到新版本,并且可以手动处理。
  3. 应用程序范围的 javascript 代码。分布在许多文件中并且可以偶尔更改的自定义 javascript。应用程序需要加载所有这些文件才能工作。
  4. 特定于功能的 javascript 代码。请求特定功能时按需加载的自定义 javascript。此代码可能会经常更改,并且不会在启动时加载。

Build Objectives

构建目标

I'd like to do the following during my build process:

我想在构建过程中执行以下操作:

  • Concatenate all type 3 javascript files together, minify them, and save as a single file with a version number. For instance: app-2.0.6.min.js, where 2.0.6 is the maven project version.
  • All type 4 files should be individually minified and saved as separate files with version numbers in the name. For instance: feature-abc-56ab32de29.min.js, where 56ab32de29 is the version number of that specific file.
  • Update HTML files with <script>tags to point to javascript files with the correct version numbers.
  • Update Javscript files that load type 4 feature javascript files to point to the right versions.
  • 将所有类型 3 javascript 文件连接在一起,缩小它们,并保存为带有版本号的单个文件。例如:app-2.0.6.min.js,其中 2.0.6 是 maven 项目版本。
  • 所有类型 4 文件都应单独缩小并保存为名称中带有版本号的单独文件。例如:feature-abc-56ab32de29.min.js,其中 56ab32de29 是该特定文件的版本号。
  • 使用<script>标签更新 HTML 文件以指向具有正确版本号的 javascript 文件。
  • 更新加载类型 4 功能 javascript 文件的 javascript 文件以指向正确的版本。

Questions

问题

  • Is there a maven plugin that will assist with the concatenation?

  • Is there a maven plugin that will assist with the minification? Ideally, I'd like to use Google Closure Compiler, but would work with others if simpler.

  • Is there a maven plugin that will assist with the versioning?

  • Is there a way to have the type 4 javascript files have independent version numbers? Ideally, if a file doesn't change between version 2.0.5 and 2.0.6, there is no need for users to download a new version and their cached version would work fine. I'm using GIT for source control, so would there be a way to use a file's GIT hashcode for versioning?

  • Is there a solution that will compress the javascript that is inline in regular HTML files without killing the HTML?

  • Is there a solution that will compress and version my CSS files as well?

  • 是否有帮助连接的 Maven 插件?

  • 是否有帮助缩小的 Maven 插件?理想情况下,我想使用 Google Closure Compiler,但如果更简单,我会与其他人合作。

  • 是否有帮助版本控制的 Maven 插件?

  • 有没有办法让类型 4 的 javascript 文件具有独立的版本号?理想情况下,如果文件在 2.0.5 和 2.0.6 版本之间没有变化,则用户无需下载新版本,他们的缓存版本就可以正常工作。我使用 GIT 进行源代码控制,那么有没有办法使用文件的 GIT 哈希码进行版本控制?

  • 有没有一种解决方案可以压缩常规 HTML 文件中内联的 javascript 而不会杀死 HTML?

  • 是否有解决方案可以压缩和版本化我的 CSS 文件?

采纳答案by Taylor Leese

Take a look at the yuicompressor-maven-plugin. It can aggregate various js (as well as css) files as well as minify and obfuscate them.

看看yuicompressor-maven-plugin。它可以聚合各种 js(以及 css)文件以及缩小和混淆它们。

回答by Chris Dolan

Here's a brand-new Maven plugin that targets this task: http://mojo.codehaus.org/webminifier-maven-plugin/

这是针对此任务的全新 Maven 插件:http: //mojo.codehaus.org/webminifier-maven-plugin/

回答by Ates Goral

I've successfully incorporated RequireJS optimization (uses Google Closure compiler + does concatenation) in a Maven environment (for single page JS app). See my question and the follow up answer for details: RequireJS Compilation in Maven project with external JS dependencies

我已经成功地将 RequireJS 优化(使用 Google Closure 编译器 + 进行连接)合并到 Maven 环境中(用于单页 JS 应用程序)。有关详细信息,请参阅我的问题和后续答案:RequireJS Compilation in Maven project with external JS dependencies

You could probably expand on that to version and archive the minified JS artifacts.

您可能可以将其扩展到版本和存档缩小的 JS 工件。