如何使用yuicompressor-maven-plugin让maven与缩小的文件建立战争

Zip*_*per 10 java minify maven

所以我正在尝试一些我认为相当简单的东西,我基本上希望maven在构建战争之前为我缩小所有js和css文件.我的插件看起来像这样:

         <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <version>1.0.0</version>
            <configuration>
                <manifestLocation>META-INF</manifestLocation>
                <instructions>
                    <Export-Package>!test.impl,test*</Export-Package>
                    <Import-Package>*</Import-Package>
                    <!--
                       add ,plugin.xml if it's present i.e.
                       src/main/resources,plugin.xml
                    -->
                    <Include-Resource>src/main/resources</Include-Resource>
                </instructions>
            </configuration>
        </plugin>

        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <nosuffix>true</nosuffix>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

问题是YUI插件确实正确地缩小了文件,但是在构建战争之前,它看起来像是复制了我的主源目录中的所有文件,从而消除了YUI插件所做的更改.

我通过以下方式打电话给maven : mvn compile war:war. 我已经玩了一段时间不同的设置,但到目前为止我还没有找到解决方法.

我想要的是刚刚战争之后从src目录复制了所需的文件,它将运行YUI插件,但我尝试了YUI插件上的所有阶段的排列,但这似乎没有任何区别.

我已经google了一下,但到目前为止,我所阅读的几乎所有内容似乎都表明我应该像我一样放弃YUI插件,一切都应该神奇地工作.到目前为止,我似乎没有找到魔法.

Kri*_*nck 22

接受的答案不起作用.

这个答案要好得多(正如koga在他的评论中指出的那样):https: //stackoverflow.com/a/11495021/11451

这是我最终做的事情:

第1步:缩小css和js

<plugin>
    <groupId>com.samaxes.maven</groupId>
    <artifactId>minify-maven-plugin</artifactId>
    <version>1.7.2</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>minify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <charset>UTF-8</charset>
        <skipMerge>true</skipMerge>
        <cssSourceDir>resources</cssSourceDir>
        <jsSourceDir>resources</jsSourceDir>
        <jsEngine>CLOSURE</jsEngine>
        <nosuffix>true</nosuffix>
        <webappTargetDir>${project.build.directory}/minify</webappTargetDir>
        <cssSourceIncludes>
            <cssSourceInclude>**/*.css</cssSourceInclude>
        </cssSourceIncludes>
        <cssSourceExcludes>
            <cssSourceExclude>**/*.min.css</cssSourceExclude>
        </cssSourceExcludes>
        <jsSourceIncludes>
            <jsSourceInclude>**/*.js</jsSourceInclude>
        </jsSourceIncludes>
        <jsSourceExcludes>
            <jsSourceExclude>**/*.min.js</jsSourceExclude>
        </jsSourceExcludes>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

第2步:使用缩小的文件覆盖战争中的css和js

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <webResources>
            <resource>
                <directory>${project.build.directory}/minify</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)


kog*_*oga 6

使用war生成器并添加配置以排除源文件.例:

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>${war-plugin.version}</version>
              <configuration> 
                <warSourceExcludes>js/**/*.js</warSourceExcludes> 
            </configuration> 
    </plugin>
Run Code Online (Sandbox Code Playgroud)

之后,您需要将目标文件包含在战争中.你可以通过设置maven生命周期的"prepare-package"阶段(我正在使用Minify插件)并在Minify配置上添加文件(jsSourceIncludes,cssSourceIncludes,...)来实现这一点.

例如:

         <plugin>
            <groupId>com.samaxes.maven</groupId>
            <artifactId>minify-maven-plugin</artifactId>
            <version>1.7.2</version>
            <executions>
              <execution>
                <id>default-minify</id>
                <goals>
                    <goal>minify</goal>
                </goals>
               <phase>prepare-package</phase>
                <configuration>
                     <jsSourceDir>/js</jsSourceDir>
                     <jsSourceIncludes>
                        <jsSourceInclude>/**/*.js</jsSourceInclude>
                     </jsSourceIncludes>
                </configuration>
              </execution>
            </executions>
          </plugin>
Run Code Online (Sandbox Code Playgroud)


Ang*_*ity 5

会发生什么是上面的配置在进程资源阶段运行压缩器,但是包阶段会用原始文件覆盖这些文件.

通过将阶段更改为包,它应该工作:

<execution>
    <phase>package</phase>
    <goals>
        <goal>compress</goal>
    </goals>
Run Code Online (Sandbox Code Playgroud)

现在压缩是在复制到目标的文件之后完成的,以便构建WAR内容.

发生这种情况的原因是,只压缩文件而不连接它们或用后缀重命名它们不是插件最常见的用例.

通常我们希望将文件压缩并连接到一个文件中,并为其指定一个新名称.

新名称通常类似于originalname-min.css/original.name-min.js,其中.min是后缀,因此删除上面配置中的nosuffix选项也可以.

编辑:日志示例

[INFO] --- yuicompressor-maven-plugin:1.1:compress (default) @ yui-compressor-test -
[INFO] prettify.css (817b) -> prettify.css (617b)[75%]
[INFO] total input (1510b) -> output (1134b)[75%]
Run Code Online (Sandbox Code Playgroud)

  • 这实际上并没有最终起作用,它确实正确地缩小了文件系统上的文件,但它在它缩小之前构建了war文件.我尝试将阶段更改为prepare-package,但这也不起作用. (2认同)