views:

47

answers:

1

Hello,

I want to know how one can compress javascriptfiles using maven. I have lready visited webistes like http://mojo.codehaus.org/javascript-maven-tools/guide-webapp-development.html but there is no implementation expained.

I want to know the dependency for executing the maven plugin.

Thanks.

+2  A: 

I suggest using the YUI Compressor Maven Mojo and its yuicompressor:compress goal instead. It is well documented, it just works.

To use it, add the following pluginRepository:

  <pluginRepositories>
    <pluginRepository>
      <name>oss.sonatype.org - github-releases</name>
      <id>oss.sonatype.org-github-releases</id>
      <url>http://oss.sonatype.org/content/repositories/github-releases&lt;/url&gt;
    </pluginRepository>
  </pluginRepositories>

And declare the plugin:

<project>
  ...
  <build>
    <!-- To define the plugin version in your parent POM -->
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>net.alchim31.maven</groupId>
          <artifactId>yuicompressor-maven-plugin</artifactId>
          <version>0.9</version>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
    <!-- To use the plugin goals in your POM or parent POM -->
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>yuicompressor-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compress</goal>
            </goals>
          </execution>
        </executions>   
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

Refer to the Usage page and the parameters of yuicompressor:compress for precise configuration.

Pascal Thivent
Hello Pascal, Thank you for replying and giving the solution. The plugin works fine for me and I can compress the files required for the project. In the mean time I have 2 more related questions. Original yuicompressor was capable of removing new line for JS file. Is there way that I can remove new line from the compressed js using the plugin ? e.g. test.namespace("com.grid");com.grid.Menu=function(a){this.m_oMenu=new com.grid.Menu(a,this);this.m_oMenu=a;};As a single line test.namespace("com.grid");com.grid.Menu=function(a){this.m_oMenu=new com.grid.Menu(a,this);this.m_oMenu=a;};
AmbGup
@AmbGup I don't know, I'm not a hardcore user of this plugin. But it looks like the plugin removes unnecessary semicolumn by default. Maybe this should be turned off (using `preserveAllSemiColons`) so that it can remove new lines.
Pascal Thivent