我有一个用Maven构建的Web项目,我正在尝试找出使用RequireJS编译器编译JavaScript文件的最佳方法(这个问题也适用于任何编译器/缩小器).
我有一个有效的设置,但它需要改进:
我打包了第三方JavaScript库,它们作为依赖项被下载,然后添加了WAR Overlay插件.
我有一个Exec插件任务,在目标目录中运行RequireJS编译器.我目前exec:exec
在程序包目标运行后手动运行(因此WAR内容放在目标目录中).
我想要的是使JS编译成为main(Java)编译的一部分.在编译后发生的WAR覆盖阶段,JS编译器本身(Require JS)作为依赖项下载.所以,我需要下载和解压缩Require JS文件,我需要在Java编译之前/期间/之后使用这些文件运行JS编译.
我相信可以有几种方法来实现这一目标.我正在寻找最优雅的解决方案.
更新:现有的POM代码段
我有JavaScript依赖项,我已压缩并添加到我们的存储库管理器:
<dependency>
<groupId>org.requirejs</groupId>
<artifactId>requirejs</artifactId>
<version>0.22.0</version>
<classifier>repackaged</classifier>
<type>zip</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.jqueryui</groupId>
<artifactId>jquery-ui</artifactId>
<version>1.8.7</version>
<classifier>repackaged</classifier>
<type>zip</type>
<scope>runtime</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
请注意,RequireJS本身(编译其余库所需的)也作为外部依赖项加载.所以首先,我需要在开始使用RequireJS编译之前下载并解压缩此依赖项.
使用WAR Overlay插件将这些依赖项添加到WAR:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>org.requirejs</groupId>
<artifactId>requirejs</artifactId>
<classifier>repackaged</classifier>
<type>zip</type>
<targetPath>lib</targetPath>
<includes>
<include>requirejs/require.js</include>
<include>requirejs/require/*</include>
<include>requirejs/build/**</include>
</includes>
</overlay>
<overlay>
<groupId>com.jqueryui</groupId>
<artifactId>jquery-ui</artifactId>
<classifier>repackaged</classifier>
<type>zip</type>
<targetPath>lib</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
尽管我不需要requirejs/build/**
在WAR中结束,但我将它作为覆盖的一部分包含在内以获得解压缩的RequireJS构建脚本,仅仅因为我没有想出更好的方法.
然后我有一个执行编译的Exec插件任务.但请注意,此任务尚未添加到正常的编译工作流程中:我必须在WAR打包完成mvn exec:exec
后手动调用它:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration> …
Run Code Online (Sandbox Code Playgroud)