Dependencies 具有外部JS依赖项的Maven项目中的RequireJS编译

Dependencies 具有外部JS依赖项的Maven项目中的RequireJS编译,dependencies,maven,javascript,requirejs,Dependencies,Maven,Javascript,Requirejs,我有一个用Maven构建的web项目,我正试图找出用RequireJS编译器编译JavaScript文件的最佳方法(这个问题也适用于任何编译器/小型化程序) 我有一个可行的设置,但需要改进: 我已经打包了第三方JavaScript库,它们作为依赖项下载,然后添加WAR覆盖插件 我有一个Exec插件任务,它在目标目录中运行RequireJS编译器。当前,我在包目标运行后手动运行exec:exec(因此WAR内容被放置在目标目录中) 相反,我想让JS编译成为主(Java)编译的一部分。JS编译器本身

我有一个用Maven构建的web项目,我正试图找出用RequireJS编译器编译JavaScript文件的最佳方法(这个问题也适用于任何编译器/小型化程序)

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

我已经打包了第三方JavaScript库,它们作为依赖项下载,然后添加WAR覆盖插件

我有一个Exec插件任务,它在目标目录中运行RequireJS编译器。当前,我在包目标运行后手动运行
exec:exec
(因此WAR内容被放置在目标目录中)

相反,我想让JS编译成为主(Java)编译的一部分。JS编译器本身(requireJS)在编译后的WAR覆盖阶段作为依赖项下载。因此,我需要下载并解压缩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>
因此,有些问题是:

  • 如何为编译和WAR打包步骤提取单个压缩依赖项的不同部分?或者我必须创建两个zip文件,一个只包含运行时内容,另一个用于编译脚本
  • 我希望在编译期间触发
    exec:exec
    ,如果不是,则在WAR打包之前触发。我该怎么做

  • 实际上,我已经尝试了很多东西,但都没有成功,所以我希望我不要表现得像懒散地发布大量代码并等待答案:)只是我还没有完全了解Maven目标/阶段等是如何工作的。

    我已经想出了以下适合我的解决方案:

    我没有依赖WAR覆盖来解包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>
    
    
    org.apache.maven.plugins
    maven依赖插件
    编译
    打开
    org.requirejs
    安魂曲
    0.22.0
    拉链
    重新包装
    真的
    
    该阶段设置为“编译”。这允许我在编译期间将RequireJS包的全部内容放在“dependency”文件夹下。所以,接下来我要做的是:

          <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>
    
    
    org.codehaus.mojo
    execmaven插件
    1.1
    编译
    执行官
    ${project.build.directory}/dependency/requirejs/build/build.bat
    ${basedir}/src/main/webapp
    名称=引导
    out=scripts/compiled.js
    baseUrl=脚本
    优化=关闭
    excludeshalf=插件/清单
    
    这将触发“dependency”文件夹中的RequireJS编译,而不涉及我的源目录或WAR目录。然后,我继续使用WAR覆盖插件来挑选想要加入战争的RequireJS文件

    更新

    自从编写这个解决方案以来,我已经转而使用“java”目标而不是“exec”,因为我在通过Hudson使用RequireJS编译器的shell脚本+批处理文件时遇到了问题。我基本上是在运行最终的Java命令(由脚本生成),该命令通过Rhino运行编译器。节点解决方案将略有不同。对于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>
    
    
    org.codehaus.mojo
    execmaven插件
    1.1
    编译js
    编译
    执行官
    JAVA
    ${basedir}/src/main/webapp
    -类路径
    ${project.build.directory}/dependency/requirejs/build/lib/rhino/js.jar
    org.mozilla.javascript.tools.shell.Main
    ${project.build.directory}/dependency/requirejs/bin/x.js
    ${project.build.directory}/dependency/requirejs/bin/
    ${project.build.directory}/dependency/requirejs/build/build.js
    名称=引导
    out=scripts/compiled.js
    baseUrl=脚本
    优化=无
    
    有一个专门针对RequireJS优化的maven插件,位于:

    它应该始终与最新版本的RequireJS一起提供,通过向项目中添加不同的版本并在插件配置中指定其路径,很容易覆盖它


    完全公开:我编写了插件。

    也许你可以发布相关的pom代码段?这会尝试运行node,因此我在执行编译器时会收到一条错误消息。节点不应该是requirejs编译器的要求,不是吗?@adam:这是requirejs 0.22.0的要求。你用0.23.0试过这个吗?这两个版本的构建脚本有很大不同。是的,我使用的是0.23.0,它执行build.bat或其他版本中的node命令。bat it executes我将其移动到了其中,增加了对编译coffeescript和haml模板的支持。brew插件有一些在requirejs maven中不可用的错误修复。Brew仍然不稳定,但正在积极开发并在生产中使用。如果项目是独立的,但它不是独立的,这将非常有效
          <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>
    
    <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>