View Javadoc
1   package org.codehaus.mojo.gwt.shell;
2   
3   /*
4    * I18NMojo.java
5    *
6    * Created on August 19th, 2008
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21   *
22   */
23  
24  import java.io.File;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
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.apache.maven.plugins.annotations.ResolutionScope;
33  
34  /**
35   * Creates I18N interfaces for constants and messages files.
36   *
37   * @author <a href="mailto:sascha@kulawik.de">Sascha-Matthias Kulawik</a>
38   * @author ccollins
39   * @version $Id$
40   */
41  @Mojo(name = "i18n", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
42  public class I18NMojo
43      extends AbstractGwtShellMojo
44  {
45      /**
46       * List of resourceBundles that should be used to generate i18n Messages interfaces.
47       */
48      @Parameter(alias = "i18nMessagesNames")
49      private String[] i18nMessagesBundles;
50  
51      /**
52       * Shortcut for a single i18nMessagesBundle
53       */
54      @Parameter
55      private String i18nMessagesBundle;
56  
57      /**
58       * List of resourceBundles that should be used to generate i18n Constants interfaces.
59       */
60      @Parameter(alias = "i18nConstantsNames")
61      private String[] i18nConstantsBundles;
62  
63      /**
64       * Shortcut for a single i18nConstantsBundle
65       */
66      @Parameter
67      private String i18nConstantsBundle;
68  
69      /**
70       * List of resourceBundles that should be used to generate i18n ConstantsWithLookup interfaces.
71       */
72      @Parameter
73      private String[] i18nConstantsWithLookupBundles;
74  
75      /**
76       * Shortcut for a single i18nConstantsWithLookupBundle
77       */
78      @Parameter
79      private String i18nConstantsWithLookupBundle;
80  
81      
82      @Override
83      protected boolean isGenerator() {
84          return true;
85      }
86      
87      public void doExecute( )
88          throws MojoExecutionException, MojoFailureException
89      {
90          setup();
91  
92          try {
93              // constants with lookup
94              if ( i18nConstantsWithLookupBundles != null )
95              {
96                  for ( String target : i18nConstantsWithLookupBundles )
97                  {
98                      ensureTargetPackageExists( getGenerateDirectory(), target );
99                      createJavaCommand()
100                         .setMainClass( "com.google.gwt.i18n.tools.I18NSync" )
101                         .addToClasspath( getClasspath( Artifact.SCOPE_COMPILE ) )
102                         .addToClasspath( getGwtUserJar() )
103                         .addToClasspath( getGwtDevJar() )
104                         .arg( "-out", getGenerateDirectory().getAbsolutePath() )
105                         .arg( "-createConstantsWithLookup" )
106                         .arg( target )
107                         .execute();
108                 }
109             }
110 
111             // constants
112             if ( i18nConstantsBundles != null )
113             {
114                 for ( String target : i18nConstantsBundles )
115                 {
116                     ensureTargetPackageExists( getGenerateDirectory(), target );
117                     createJavaCommand()
118                         .setMainClass( "com.google.gwt.i18n.tools.I18NSync" )
119                         .addToClasspath( getClasspath( Artifact.SCOPE_COMPILE ) )
120                         .addToClasspath( getGwtUserJar() )
121                         .addToClasspath( getGwtDevJar() )
122                         .arg( "-out", getGenerateDirectory().getAbsolutePath() )
123                         .arg( target )
124                         .execute();
125                 }
126             }
127 
128             // messages
129             if ( i18nMessagesBundles != null )
130             {
131                 for ( String target : i18nMessagesBundles )
132                 {
133                     ensureTargetPackageExists( getGenerateDirectory(), target );
134                     createJavaCommand()
135                         .setMainClass( "com.google.gwt.i18n.tools.I18NSync" )
136                         .addToClasspath( getClasspath( Artifact.SCOPE_COMPILE ) )
137                         .addToClasspath( getGwtUserJar() )
138                         .addToClasspath( getGwtDevJar() )
139                         .arg( "-out", getGenerateDirectory().getAbsolutePath() )
140                         .arg( "-createMessages" )
141                         .arg( target )
142                         .execute();
143                 }
144             }
145         }
146         catch (JavaCommandException e)
147         {
148             throw new MojoExecutionException( e.getMessage(), e );
149         }
150     }
151 
152 
153     private void setup()
154         throws MojoExecutionException
155     {
156         if ( i18nConstantsWithLookupBundles == null && i18nConstantsWithLookupBundle != null )
157         {
158             i18nConstantsWithLookupBundles = new String[] { i18nConstantsWithLookupBundle };
159         }
160 
161         if ( i18nConstantsBundles == null && i18nConstantsBundle != null )
162         {
163             i18nConstantsBundles = new String[] { i18nConstantsBundle };
164         }
165 
166         if ( i18nMessagesBundles == null && i18nMessagesBundle != null )
167         {
168             i18nMessagesBundles = new String[] { i18nMessagesBundle };
169         }
170 
171         if ( i18nMessagesBundles == null && i18nConstantsBundles == null && i18nConstantsWithLookupBundles == null )
172         {
173             throw new MojoExecutionException(
174                 "neither i18nConstantsBundles, i18nMessagesBundles nor i18nConstantsWithLookupBundles present. \n"
175                 + "Cannot execute i18n goal" );
176         }
177 
178         setupGenerateDirectory();
179     }
180 
181 
182     private void ensureTargetPackageExists( File generateDirectory, String targetName )
183     {
184         targetName = targetName.substring( 0, targetName.lastIndexOf( '.' ) );
185         String targetPackage = targetName.replace( '.', File.separatorChar );
186         getLog().debug( "ensureTargetPackageExists, targetName : " + targetName + ", targetPackage : " + targetPackage );
187         File targetPackageDirectory = new File( generateDirectory, targetPackage );
188         if ( !targetPackageDirectory.exists() )
189         {
190             targetPackageDirectory.mkdirs();
191         }
192     }
193 
194 }