43
43
requiresDependencyCollection = ResolutionScope .COMPILE_PLUS_RUNTIME )
44
44
public class CompileCSSMojo extends AbstractCN1Mojo {
45
45
46
+
46
47
@ Override
47
48
protected void executeImpl () throws MojoExecutionException , MojoFailureException {
49
+ File cssDirectory = findCSSDirectory ();
50
+ int themeCssLen = "theme.css" .length ();
51
+ if (cssDirectory != null && cssDirectory .isDirectory ()) {
52
+ for (File file : cssDirectory .listFiles ()) {
53
+ String fileName = file .getName ();
54
+ if (fileName .endsWith ("theme.css" )) {
55
+ executeImpl (fileName .substring (0 , fileName .length () - themeCssLen ));
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Gets the source CSS directory (src/main/css). The theme.css file should be inside this directory.
63
+ * @return
64
+ */
65
+ protected File findCSSDirectory () {
66
+ for (String dir : project .getCompileSourceRoots ()) {
67
+ File dirFile = new File (dir );
68
+ File cssSibling = new File (dirFile .getParentFile (), "css" );
69
+ File themeCss = new File (cssSibling , "theme.css" );
70
+ if (themeCss .exists ()) {
71
+ return cssSibling ;
72
+ }
73
+
74
+ }
75
+ return null ;
76
+ }
77
+
78
+ private void executeImpl (String themePrefix ) throws MojoExecutionException , MojoFailureException {
48
79
if (!isCN1ProjectDir ()) {
49
80
return ;
50
81
}
51
82
if (properties .getProperty ("codename1.cssTheme" , null ) == null ) {
52
83
getLog ().info ("CSS themes not activated for this project. Skipping CSS compilation" );
53
84
return ;
54
85
}
55
-
86
+
56
87
File cssDirectory = findCSSDirectory (); // src/main/css
57
88
if (cssDirectory == null || !cssDirectory .exists ()) {
58
89
getLog ().warn ("CSS compilation skipped because no CSS theme was found" );
59
90
return ;
60
91
}
61
- File themeResOutput = new File (project .getBuild ().getOutputDirectory () + File .separator + "theme.res" );
92
+ File themeResOutput = new File (project .getBuild ().getOutputDirectory () + File .separator + themePrefix + "theme.res" );
62
93
// target/css
63
94
File cssBuildDir = new File (project .getBuild ().getDirectory () + File .separator + "css" );
64
95
cssBuildDir .mkdirs ();
65
96
66
97
// target/css/theme.css - the merged CSS file
67
- File mergeFile = new File (cssBuildDir , "theme.css" );
98
+ File mergeFile = new File (cssBuildDir , themePrefix + "theme.css" );
68
99
mergeFile .getParentFile ().mkdirs ();
69
100
try {
70
101
if (themeResOutput .exists () && getCSSSourcesModificationTime () < themeResOutput .lastModified ()) {
@@ -80,14 +111,14 @@ protected void executeImpl() throws MojoExecutionException, MojoFailureException
80
111
// theme.css to the input list. (Codename One Library projects will include such an
81
112
// artifact if they have CSS files).
82
113
final StringBuilder inputs = new StringBuilder ();
83
-
114
+
84
115
project .getArtifacts ().forEach (artifact ->{
85
116
if (artifact .hasClassifier () && "cn1css" .equals (artifact .getClassifier ())) {
86
117
File zip = findArtifactFile (artifact );
87
118
if (zip == null || !zip .exists ()) {
88
119
return ;
89
120
}
90
-
121
+
91
122
File extracted = new File (zip .getParentFile (), zip .getName ()+"-extracted" );
92
123
getLog ().debug ("Checking for extracted CSS bundle " +extracted );
93
124
if (extracted .exists () && artifact .isSnapshot () && getLastModified (artifact ) > extracted .lastModified ()) {
@@ -112,7 +143,7 @@ protected void executeImpl() throws MojoExecutionException, MojoFailureException
112
143
if (extractedCssDir .exists ()) {
113
144
// We expect that the cn1css artifact has a theme.css file at its root
114
145
// If found, we add it to the list of inputs.
115
- File theme = new File (extractedCssDir , "theme.css" );
146
+ File theme = new File (extractedCssDir , themePrefix + "theme.css" );
116
147
if (theme .exists ()) {
117
148
if (inputs .length () > 0 ) {
118
149
inputs .append ("," );
@@ -130,18 +161,18 @@ protected void executeImpl() throws MojoExecutionException, MojoFailureException
130
161
// The project's theme.css file is added to the input list last so that it will result in it
131
162
// being last in the merged theme.css file (i.e. the application project CSS can override the
132
163
// CSS in dependent libraries.
133
- File cssTheme = new File (cssDirectory , "theme.css" );
164
+ File cssTheme = new File (cssDirectory , themePrefix + "theme.css" );
134
165
if (cssTheme .exists ()) {
135
166
if (inputs .length () > 0 ) {
136
167
inputs .append ("," );
137
168
}
138
169
inputs .append (cssTheme .getAbsolutePath ());
139
170
} else {
140
- if (inputs .length () > 0 ) {
141
- throw new MojoFailureException ("Cannot compile CSS for this project. The project does not include a theme.css file in " +cssTheme +", but it includes dependencies that require CSS. Please add a CSS file at " +cssTheme );
171
+ if (themePrefix . isEmpty () && inputs .length () > 0 ) {
172
+ throw new MojoFailureException ("Cannot compile CSS for this project. The project does not include a " + themePrefix + "- theme.css file in " +cssTheme +", but it includes dependencies that require CSS. Please add a CSS file at " +cssTheme );
142
173
143
174
}
144
- getLog ().info ("Skipping CSS compilation because " +cssTheme +" does not exist" );
175
+ getLog ().info ("Skipping CSS compilation for because " +themePrefix + cssTheme +" does not exist" );
145
176
return ;
146
177
}
147
178
@@ -164,36 +195,17 @@ protected void executeImpl() throws MojoExecutionException, MojoFailureException
164
195
java .createArg ().setValue ("-css" );
165
196
java .createArg ().setValue ("-input" );
166
197
java .createArg ().setValue (inputs .toString ());
167
-
198
+
168
199
java .createArg ().setValue ("-output" );
169
200
java .createArg ().setFile (themeResOutput );
170
-
201
+
171
202
java .createArg ().setValue ("-merge" );
172
203
java .createArg ().setFile (mergeFile );
173
204
int res = java .executeJava ();
174
205
if (res != 0 ) {
175
- throw new MojoExecutionException ("An error occurred while compiling the CSS files. Inputs: " +inputs +", output: " + new File (project .getBuild ().getOutputDirectory () + File .separator + "theme.res" ) +", merge file: " +mergeFile );
176
- }
177
- }
178
-
179
- /**
180
- * Gets the source CSS directory (src/main/css). The theme.css file should be inside this directory.
181
- * @return
182
- */
183
- protected File findCSSDirectory () {
184
- for (String dir : project .getCompileSourceRoots ()) {
185
- File dirFile = new File (dir );
186
- File cssSibling = new File (dirFile .getParentFile (), "css" );
187
- File themeCss = new File (cssSibling , "theme.css" );
188
- if (themeCss .exists ()) {
189
- return cssSibling ;
190
- }
191
-
206
+ throw new MojoExecutionException ("An error occurred while compiling the CSS files. Inputs: " +inputs +", output: " + new File (project .getBuild ().getOutputDirectory () + File .separator + themePrefix + "theme.res" ) +", merge file: " +mergeFile );
192
207
}
193
- return null ;
194
208
}
195
-
196
-
197
209
198
210
199
211
0 commit comments