yuicompressor maven plugin and maven-war-plugin

前端 未结 7 1363
走了就别回头了
走了就别回头了 2020-12-24 03:34

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 plugi

相关标签:
7条回答
  • 2020-12-24 03:52

    The approach I use is a bit different.

    First, I've configured my IDE to run mvn process-resources before the compilation/packaging. This way the files are created before the war is assembled.

    It is very important to set <nosuffix>false</nosuffix> and <outputDirectory>${basedir}/src/main/resources/</outputDirectory> so the files can be created in the same directory without replacing your original source files.

     <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>yuicompressor-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
            <preProcessAggregates>false</preProcessAggregates>
            <excludes>
                <exclude>**/*-min.js</exclude>
                <exclude>**/*.min.js</exclude>
                <exclude>**/*-min.css</exclude>
                <exclude>**/*.min.css</exclude>
            </excludes>
            <jswarn>false</jswarn>
            <nosuffix>false</nosuffix> <!-- VERY IMPORTANT WILL REPLACE YOUR FILES IF YOU SET nosuffix TO TRUE OR DONT SET IT AT ALL -->
            <outputDirectory>${basedir}/src/main/resources/</outputDirectory> <!-- by default the plugin will copy the minimized version to target directory -->
            <failOnWarning>false</failOnWarning>
        </configuration>
    
        <executions>
            <execution>
                <id>compress_js_css</id>
                    <phase>process-resources</phase>
                <goals>
                    <goal>compress</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-12-24 03:54

    Just configure 'warSourceExcludes' on the WAR plugin.

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
        </configuration>
    </plugin>
    
    0 讨论(0)
  • 2020-12-24 03:57

    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>
    
    0 讨论(0)
  • 2020-12-24 04:00

    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>

    0 讨论(0)
  • 2020-12-24 04:01

    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.

    0 讨论(0)
  • 2020-12-24 04:05

    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>
    
    0 讨论(0)
提交回复
热议问题