View Javadoc
1   package org.codehaus.mojo.gwt.shell;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.io.FilenameUtils;
23  import org.apache.commons.lang.SystemUtils;
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.model.Resource;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.cli.StreamConsumer;
34  import org.sonatype.plexus.build.incremental.BuildContext;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.OutputStreamWriter;
39  import java.nio.charset.Charset;
40  
41  /**
42   * Creates CSS interfaces for css files.
43   * Will use the utility tool provided in gwt sdk which create a corresponding Java interface for accessing 
44   * the classnames used in the file.
45   *
46   * @author <a href="mailto:undheim@corporater.com">Stale Undheim</a>
47   * @author olamy
48   * @since 2.1.0-1
49   */
50  @Mojo(name = "css", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
51  public class CSSMojo
52      extends AbstractGwtShellMojo
53  {
54      /**
55       * List of resourceBundles that should be used to generate CSS interfaces.
56       */
57      @Parameter
58      private String[] cssFiles;
59  
60      /**
61       * Shortcut for a single cssFile
62       */
63      @Parameter
64      private String cssFile;
65  
66      @Parameter(property = "project.build.sourceEncoding")
67      private String encoding;
68  
69      @Component
70      private BuildContext buildContext;
71  
72      @Override
73      protected boolean isGenerator() {
74          return true;
75      }
76  
77      public void doExecute()
78          throws MojoExecutionException, MojoFailureException
79      {
80          setup();
81  
82          // java -cp gwt-dev.jar:gwt-user.jar
83          // com.google.gwt.resources.css.InterfaceGenerator -standalone -typeName some.package.MyCssResource -css
84          // input.css
85          if ( cssFiles != null )
86          {
87              for ( String file : cssFiles )
88              {
89                  final String typeName = FilenameUtils.separatorsToSystem( file ).
90                      substring( 0, file.lastIndexOf( '.' ) ).replace( File.separatorChar, '.' );
91                  final File javaOutput =
92                      new File( getGenerateDirectory(), typeName.replace( '.', File.separatorChar ) + ".java" );
93                  for ( Resource resource : getProject().getResources() )
94                  {
95                      final File candidate = new File( resource.getDirectory(), file );
96                      if ( candidate.exists() )
97                      {
98                          if ( buildContext.isUptodate( javaOutput, candidate ) )
99                          {
100                             getLog().debug( javaOutput.getAbsolutePath() + " is up to date. Generation skipped" );
101                             break;
102                         }
103 
104                         getLog().info( "Generating " + javaOutput + " with typeName " + typeName );
105                         ensureTargetPackageExists( getGenerateDirectory(), typeName );
106 
107                         
108                         try
109                         {
110                             final StringBuilder content = new StringBuilder();
111                             createJavaCommand()
112                                 .setMainClass( "com.google.gwt.resources.css.InterfaceGenerator" )
113                                 .addToClasspath( getClasspath( Artifact.SCOPE_COMPILE ) )
114                                 .arg( "-standalone" )
115                                 .arg( "-typeName" )
116                                 .arg( typeName )
117                                 .arg( "-css" )
118                                 .arg( candidate.getAbsolutePath() )
119                                 .addToClasspath( getJarFiles(GWT_DEV, false) )
120                                 .addToClasspath( getJarFiles(GWT_USER, false) )
121                                 .setOut( new StreamConsumer()
122                                     {
123                                         public void consumeLine( String line )
124                                         {
125                                             content.append( line ).append( SystemUtils.LINE_SEPARATOR );
126                                         }
127                                     } )
128                                 .execute();
129                             if ( content.length() == 0 )
130                             {
131                                 throw new MojoExecutionException( "cannot generate java source from file " + file + "." );
132                             }
133                             final OutputStreamWriter outputWriter =
134                                 new OutputStreamWriter( buildContext.newFileOutputStream( javaOutput ) , encoding );
135                             try {
136                                 outputWriter.write( content.toString() );
137                             } finally {
138                                 IOUtil.close( outputWriter );
139                             }
140                         }
141                         catch ( IOException e )
142                         {
143                             throw new MojoExecutionException( "Failed to write to file: " + javaOutput, e );
144                         }
145                         catch ( JavaCommandException e )
146                         {
147                             throw new MojoExecutionException( e.getMessage(), e );
148                         }
149                         break;
150                     }
151                 }
152             }
153         }
154     }
155 
156     private void setup()
157     {
158         setupGenerateDirectory();
159 
160         if ( encoding == null )
161         {
162             getLog().warn( "Encoding is not set, your build will be platform dependent" );
163             encoding = Charset.defaultCharset().name();
164         }
165 
166         if ( cssFiles == null && cssFile != null )
167         {
168             cssFiles = new String[] { cssFile };
169         }
170     }
171 
172     private void ensureTargetPackageExists( File generateDirectory, String targetName )
173     {
174         targetName = targetName.contains( "." ) ? targetName.substring( 0, targetName.lastIndexOf( '.' ) ) : targetName;
175         String targetPackage = targetName.replace( '.', File.separatorChar );
176         getLog().debug( "ensureTargetPackageExists, targetName : " + targetName + ", targetPackage : " + targetPackage );
177         File targetPackageDirectory = new File( generateDirectory, targetPackage );
178         if ( !targetPackageDirectory.exists() )
179         {
180             targetPackageDirectory.mkdirs();
181         }
182     }
183 
184 }