1 package org.codehaus.mojo.gwt.shell;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
36
37
38
39
40
41 @Mojo(name = "i18n", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
42 public class I18NMojo
43 extends AbstractGwtShellMojo
44 {
45
46
47
48 @Parameter(alias = "i18nMessagesNames")
49 private String[] i18nMessagesBundles;
50
51
52
53
54 @Parameter
55 private String i18nMessagesBundle;
56
57
58
59
60 @Parameter(alias = "i18nConstantsNames")
61 private String[] i18nConstantsBundles;
62
63
64
65
66 @Parameter
67 private String i18nConstantsBundle;
68
69
70
71
72 @Parameter
73 private String[] i18nConstantsWithLookupBundles;
74
75
76
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
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
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
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 }