我有一个由Maven构建的Web项目,我正在尝试找出使用requienjs编译器编译JavaScript文件的最佳方法(此问题也可以适用于任何编译器/缩影)。

我有一个可行的设置,但需要改进:

我已经打包了第三方JavaScript库,并将其下载为依赖项,然后加上War Overlay插件。

我有一个运行requirejs编译器的Exec插件任务 目标内部 目录。我目前手动运行 exec:exec 包装目标运行后(因此,战争内容放在目标目录中)。

相反,我想要的是将JS汇编的一部分(Java)汇编的一部分。 JS编译器本身(要求JS)在战争覆盖阶段下载为依赖性,该阶段发生后发生。因此,我需要下载和解开“要求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>

请注意,requienjs本身(这是其余库的汇编所需的)也被加载为外部依赖关系。因此,第一件事是,我需要下载和解压缩此依赖项,然后才能开始使用requirejs汇编。

这些依赖项正在使用战争覆盖插件添加到战争中:

        <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>

即使我不需要 requirejs/build/** 为了结束战争,我将其作为覆盖层的一部分,以获取Unight Requiendjs构建脚本,这仅仅是因为我还没有找到更好的方法。

然后,我有执行汇编的Exec插件任务。但是请注意,此任务尚未添加到正常的编译工作流中:我必须手动调用它 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>
                <executable>lib/requirejs/build/build.bat</executable>
                <workingDirectory>${project.build.directory}/${project.artifactId}</workingDirectory>
                <arguments>
                    <argument>name=bootstrap</argument>
                    <argument>out=combined.js</argument>
                    <argument>baseUrl=scripts</argument>
                    <argument>optimize=closure</argument>
                    <argument>excludeShallow=plugins/manifest</argument>
                </arguments>
            </configuration>
        </plugin>

因此,有些问题是:

  1. 如何为编译和战争包装步骤提取单个拉链依赖性的不同部分?还是我必须创建两个ZIP文件,一个仅具有运行时的内容,另一个用于编译脚本?
  2. 我想触发 exec:exec 理想情况下,在汇编期间,或者如果没有,就在战争包装之前。我怎么做?

我实际上已经尝试了一堆没有成功的东西,所以我希望我不会懒惰地发布大量的代码并等待答案:)只是我并没有完全关心Maven目标/阶段等等。

有帮助吗?

解决方案 3

我想出了以下对我有用的解决方案:

我不依赖于拆卸requirejs文件的战争覆盖层,而是使用依赖项插件明确解开它们:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <phase>compile</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>org.requirejs</groupId>
              <artifactId>requirejs</artifactId>
              <version>0.22.0</version>
              <type>zip</type>
              <classifier>repackaged</classifier>
              <overWrite>true</overWrite>
            </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
  </plugin>

该阶段设置为“编译”。这使我可以在编译期间将“依赖关系”文件夹下的requienjs软件包的整个内容。因此,我接下来的事情是:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>
              ${project.build.directory}/dependency/requirejs/build/build.bat</executable>
              <workingDirectory>
              ${basedir}/src/main/webapp</workingDirectory>
              <arguments>
                <argument>name=bootstrap</argument>
                <argument>out=scripts/compiled.js</argument>
                <argument>baseUrl=scripts</argument>
                <argument>optimize=closure</argument>
                <argument>
                excludeShallow=plugins/manifest</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>

这触发了“依赖关系”文件夹内的requienjs编译,而无需触摸我的源目录或战争目录。然后,我继续使用战争覆盖插件来樱桃选择想要进入战争的启动文件。

更新

自编写此解决方案以来,我切换到使用“ Java”目标而不是“ Exec”,因为我在使用Hudson使用requienjs编译器的Shell Script + Batch文件时遇到了问题。我基本上运行了通过Rhino运行编译器的最终Java命令(由脚本生成)。节点解决方案将略有不同。对于requirejs 0.23.0,它是这样的:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <id>compile-js</id>
            <phase>compile</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <workingDirectory>${basedir}/src/main/webapp</workingDirectory>
                <arguments>
                    <argument>-classpath</argument>
                    <!--argument>${project.build.directory}/dependency/requirejs/build/lib/rhino/js.jar${path.separator}${project.build.directory}/dependency/requirejs/build/lib/closure/compiler.jar</argument -->
                    <argument>${project.build.directory}/dependency/requirejs/build/lib/rhino/js.jar</argument>
                    <argument>org.mozilla.javascript.tools.shell.Main</argument>
                    <argument>${project.build.directory}/dependency/requirejs/bin/x.js</argument>
                    <argument>${project.build.directory}/dependency/requirejs/bin/</argument>
                    <argument>${project.build.directory}/dependency/requirejs/build/build.js</argument>
                    <argument>name=bootstrap</argument>
                    <argument>out=scripts/compiled.js</argument>
                    <argument>baseUrl=scripts</argument>
                    <argument>optimize=none</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

其他提示

有一个专门针对requirejs优化的Maven插件:

https://github.com/mcheely/requirejs-maven-plugin

它应该始终使用最新版本的requirejs发货,并且很容易覆盖该版本,通过在项目中添加不同的版本并在插件配置中指定其路径。

全面披露:我写了插件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top