Archive for December, 2011

Transparent javascript and css compression with Maven3 and Yuicompressor

December 23rd, 2011

There are a lot of ressources on how to use YUIcompressor on a Maven project, but several examples don’t work with Maven3, or don’t allow to replace javascript and css by their compressed version transparently.

The following example will compress files transparently when packaging the webapp in a WAR file, and keep original files for development.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.1.1</version>            
   <configuration>
      <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
   </configuration>
</plugin>


<plugin>
   <groupId>net.alchim31.maven</groupId>
   <artifactId>yuicompressor-maven-plugin</artifactId>
   <executions>
      <execution>
         <id>compressyui</id>
         <phase>prepare-package</phase>
         <goals>
            <goal>compress</goal>
         </goals>
      </execution>

   </executions>
   <configuration>
      <nosuffix>true</nosuffix>
   </configuration>
</plugin>

XmlField : new validation API

December 21st, 2011

XmlField 0.7 which was just released provides a new validation API, based on JSR 303, with some additions borrowed from Hibernate validation.

It is now very simple to ensure objects are valid, without complex XPath expressions or XPath duplication.

Add constraints to your interfaces :
@ResourceXPath("/database")
public interface IDatabase {
   @NotEmpty
   @FieldXPath("@name")
   String getName();

   @Size(min=1)
   @FieldXPath("tables/table")
   ITable[] getTables();
}

Validate :
IDatabase db = new XmlField().xmlToObject( xmlString, IDatabase.class );

Set<ConstraintViolation<Object>> violations = new XmlFieldValidator().validate( db );

Supported annotations :

  • @NotEmpty : value exists and is not blank
  • @Size : string length, array size
  • @Range : numbers between min and max
  • @Values : check for accepted values

Enjoy :-)

See also : Introducing XmlField : Java xml/object mapping framework