yuicompressor maven plugin and maven-war-plugin

2020-02-17 05:39发布

I've been struggling with getting this plugin to play nicely with the maven-war-plugin for a couple of hours now and I thought it was time to ask for help. I have the plugin defined as follows:

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
            <id>compressyui</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compress</goal>
            </goals>
            <configuration>
                <nosuffix>true</nosuffix>
                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                <jswarn>false</jswarn>
            </configuration>
        </execution>
    </executions>
</plugin>

If I remove nosuffix=true then I can see the compressed/minified -min.js files get into the war as expected, but with this flag on they are being overwritten by the maven-war-plugin (I'm assuming) when it builds the war file. I really need the file names to remain the same though ... does anyone have an idea of what I need to change in order to use the same filenames and still get the minified versions into the final war?

7条回答
地球回转人心会变
2楼-- · 2020-02-17 06:11

OK. I finally figured this out. You need to define a <webappDirectory> in the yuicompressor plugin that can then be referenced as a <resource> in the maven-war-plugin. In the example below I'm using <directory>${project.build.directory}/min</directory>

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
            <id>compressyui</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compress</goal>
            </goals>
            <configuration>
                <nosuffix>true</nosuffix>
                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                <webappDirectory>${project.build.directory}/min</webappDirectory>
                <jswarn>false</jswarn>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
        <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
            <configuration>
                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                <encoding>UTF-8</encoding>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
        <encoding>UTF-8</encoding>
        <webResources>
            <resource>
                <directory>${project.build.directory}/min</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>
查看更多
神经病院院长
3楼-- · 2020-02-17 06:12

As Jakob Kruse say, you must deal with the *.js, but no *.min.js, so my configurations is below, please notice the use of %regex[] :

			<plugin>
			    <groupId>net.alchim31.maven</groupId>
			    <artifactId>yuicompressor-maven-plugin</artifactId>
			    <version>1.4.0</version>
			    <executions>
			        <execution>
			            <id>compressyui</id>
			            <phase>process-resources</phase>
			            <goals>
			                <goal>compress</goal>
			            </goals>
			            <configuration>
			                <nosuffix>true</nosuffix>
			                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
			                <webappDirectory>${project.build.directory}/min</webappDirectory>
			                <jswarn>false</jswarn>		
				        <excludes>
				            <exclude>**/*-min.js</exclude>
				            <exclude>**/*.min.js</exclude>
				            <exclude>**/*-min.css</exclude>
				            <exclude>**/*.min.css</exclude>

				            <exclude>**/jquery.window.js</exclude>
				            ......
				            <exclude>**/compile.js</exclude>
				        </excludes>
			            </configuration>
			        </execution>
			    </executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>					
					<packagingExcludes>servlet-api*.jar,target/test-classes/*</packagingExcludes>
					<warSourceExcludes>test/**,%regex[.*(!min).js],%regex[.*(!min).css]</warSourceExcludes>
				
					<webResources>
 					            <resource>
					                <directory>${project.build.directory}/min</directory>
					            </resource>
					</webResources>
				
				</configuration>
			</plugin>

查看更多
Deceive 欺骗
4楼-- · 2020-02-17 06:14

Without pom.xml change

mvn net.alchim31.maven:yuicompressor-maven-plugin:compress

To force compress every js and css files and fail if warning

mvn net.alchim31.maven:yuicompressor-maven-plugin:compress \
-Dmaven.yuicompressor.force=true \
-Dmaven.yuicompressor.failOnWarning=true \

For more options: http://davidb.github.io/yuicompressor-maven-plugin/usage_compress.html

查看更多
相关推荐>>
5楼-- · 2020-02-17 06:15

this is my configuration, and it works fine in my maven web project:

    <!-- js/css compress -->
<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <excludes>
            <exclude>**/*-min.js</exclude>
            <exclude>**/*.min.js</exclude>
            <exclude>**/*-min.css</exclude>
            <exclude>**/*.min.css</exclude>
        </excludes>
        <jswarn>false</jswarn>
        <nosuffix>true</nosuffix>
    </configuration>
    <executions>
        <execution>
            <id>compress_js_css</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compress</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<!-- war -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <webResources>
            <resource>
                <directory>${project.build.directory}/${project.build.finalName}/resources</directory>
                <targetPath>/resources</targetPath>
                <filtering>false</filtering>
            </resource>
        </webResources>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
    </configuration>
</plugin>
查看更多
ら.Afraid
6楼-- · 2020-02-17 06:23

Just configure 'warSourceExcludes' on the WAR plugin.

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
    </configuration>
</plugin>
查看更多
beautiful°
7楼-- · 2020-02-17 06:25

I would like to add the configuration which worked for me:

First, to fix m2e complaining about the 'Plugin execution not covered by lifecycle' I added the following in the parent pom taken from this post:

  <pluginManagement>
    <plugins>
        <!--This plugin's configuration is used to store Eclipse 
            m2e settings only. It has no influence on the Maven build itself. -->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>net.alchim31.maven</groupId>                               
                                <artifactId>yuicompressor-maven-plugin</artifactId>
                                <versionRange>[1.0.0,)</versionRange>
                                <goals>
                                    <goal>compress</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

Then in the war pom I put:

<build>
    <plugins>
        <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>yuicompressor-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
        <execution>
            <goals>
              <goal>compress</goal>
            </goals>
            <configuration>
               <linebreakpos>300</linebreakpos>
               <excludes>
                 <exclude>**/*-min.js</exclude>
                 <exclude>**/*.min.js</exclude>
                 <exclude>**/*-min.css</exclude>
                 <exclude>**/*.min.css</exclude>
               </excludes>              
               <nosuffix>true</nosuffix>
            </configuration>
        </execution>
        </executions>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

This generates the minified css and js files in the project build target directory while excluding the original files.

I hope this saves someone time.

查看更多
登录 后发表回答