简体   繁体   English

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

[英]Maven plugin for versioning and minifying javascript

Single Page Javascript Application 单页Javascript应用程序

I have built a sophisticated ajax-driven single page webapp that uses a RESTful backend web service serving JSON. 我已经构建了一个复杂的ajax驱动的单页webapp,它使用一个服务于JSON的RESTful后端Web服务。 The javascript is split into many different files, each file representing some sort of feature or component. javascript被分成许多不同的文件,每个文件代表某种特征或组件。

While the service has been in alpha testing, I've just be serving all these files separately without minification. 虽然该服务已经进行了alpha测试,但我只是在没有缩小的情况下单独提供所有这些文件。 But now that I'd like to launch a beta version, I really need to combine files, minify them, and version them. 但是现在我想推出测试版,我真的需要组合文件,缩小它们并对它们进行版本化。 I want to add this to my build process, using Maven. 我想使用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. 外部文件,例如来自Google CDN的jquery和jquery-ui。 Rarely change these versions, can be handled manually. 很少更改这些版本,可以手动处理。
  2. Jquery plugins that I'm hosting myself, such as fullcalendar or ui-layout. 我自己托管的Jquery插件,例如fullcalendar或ui-layout。 Again, I rarely update these to new versions and can handle it manually. 同样,我很少将这些更新到新版本并可以手动处理它。
  3. Application-wide javascript code. 应用程序范围的JavaScript代码。 Custom javascript that is spread across many files and can change occasionally. 自定义javascript,分布在许多文件中,偶尔可以更改。 All of these files need to be loaded for the app to work. 需要加载所有这些文件才能使应用程序正常工作。
  4. Feature-specific javascript code. 特定于功能的JavaScript代码。 Custom javascript that is loaded on demand when a specific feature is requested. 在请求特定功能时按需加载的自定义JavaScript。 This code can change quite frequently and is not loaded at startup. 此代码可能会频繁更改,并且在启动时不会加载。

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

Questions 问题

  • Is there a maven plugin that will assist with the concatenation? 是否有maven插件可以协助连接?

  • Is there a maven plugin that will assist with the minification? 有没有maven插件可以帮助缩小? Ideally, I'd like to use Google Closure Compiler, but would work with others if simpler. 理想情况下,我想使用Google Closure Compiler,但如果更简单,可以与其他人合作。

  • Is there a maven plugin that will assist with the versioning? 是否有maven插件可以帮助进行版本控制?

  • Is there a way to have the type 4 javascript files have independent version numbers? 有没有办法让4型javascript文件具有独立的版本号? 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. 理想情况下,如果文件在版本2.0.5和2.0.6之间没有变化,则用户无需下载新版本,并且其缓存版本可以正常工作。 I'm using GIT for source control, so would there be a way to use a file's GIT hashcode for versioning? 我正在使用GIT进行源代码控制,那么有没有办法使用文件的GIT哈希码进行版本控制?

  • Is there a solution that will compress the javascript that is inline in regular HTML files without killing the HTML? 有没有一种解决方案可以压缩常规HTML文件中内联的javascript而不会破坏HTML?

  • Is there a solution that will compress and version my CSS files as well? 是否有压缩和版本化我的CSS文件的解决方案?

Take a look at the yuicompressor-maven-plugin . 看看yuicompressor-maven-plugin It can aggregate various js (as well as css) files as well as minify and obfuscate them. 它可以聚合各种js(以及css)文件以及缩小和混淆它们。

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

I've successfully incorporated RequireJS optimization (uses Google Closure compiler + does concatenation) in a Maven environment (for single page JS app). 我在Maven环境中成功地整合了RequireJS优化(使用Google Closure编译器+进行连接)(对于单页JS应用程序)。 See my question and the follow up answer for details: RequireJS Compilation in Maven project with external JS dependencies 有关详细信息,请参阅我的问题和后续答案: Maven项目中的RequireJS编译与外部JS依赖项

You could probably expand on that to version and archive the minified JS artifacts. 您可以将其扩展到版本并归档缩小的JS工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM