Skip to content

Commit b262638

Browse files
committed
Added the targetDirectory propert to the Maven plugin; #106
1 parent 7ceec55 commit b262638

File tree

2 files changed

+88
-21
lines changed

2 files changed

+88
-21
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,11 @@ Replace `x.y.z` with the version you want to use.
252252
Configuration items are:
253253

254254
* `File` **sourceDirectory**
255-
The directory where the CSS files reside. It must be an existing directory.
255+
The directory where the source CSS files reside. It must be an existing directory.
256256
Defaults to `${basedir}/src/main/resources`
257+
* `File` **targetDirectory**
258+
The directory where the taget CSS files reside. If the directory is not existig, it is created.
259+
Defaults to the source directory
257260
* `boolean` **recursive**
258261
Should all directories be scanned recursively for CSS files to compress?
259262
Defaults to `true`
@@ -313,9 +316,10 @@ Configuration items are:
313316

314317
* v7.0.4 - work in progress
315318
* Added additional media query features. See [#104}(https://github.com/phax/ph-css/pull/104) - thx @nhubbard
316-
* Added new setting `CSSReaderSettings.setKeepDeprecatedProperties(boolean)` to customize if they should be read or discarded. See [#107}(https://github.com/phax/ph-css/issues/107) - thx @hrozhkov1
319+
* Added new setting `CSSReaderSettings.setKeepDeprecatedProperties(boolean)` to customize if they should be read or discarded. See [#107](https://github.com/phax/ph-css/issues/107) - thx @hrozhkov1
317320
* Added the property `keepDeprecatedProperties` to the Maven plugin
318321
* Changed the default reading charset of the Maven plugin from `UTF-8` to `ISO-8859-1` to comply to the API based reading
322+
* Added the `targetDirectory` property to the Maven plugin. See [#106](https://github.com/phax/ph-css/issues/106) - thx @isochronous
319323
* v7.0.3 - 2024-09-23
320324
* Added support for the `:is`, `:has` and `:where` pseudo functions, fixing [#88](https://github.com/phax/ph-css/issues/88) (thx @brbog), [#97](https://github.com/phax/ph-css/issues/97) (thx @nafg) and [#101](https://github.com/phax/ph-css/issues/101) (thx @subbudvk)
321325
* v7.0.2 - 2024-03-28

ph-csscompress-maven-plugin/src/main/java/com/helger/maven/csscompress/CSSCompressMojo.java

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.IOException;
2121
import java.nio.charset.Charset;
2222
import java.nio.charset.StandardCharsets;
23+
import java.util.Locale;
24+
import java.util.function.Function;
2325

2426
import javax.annotation.Nonnull;
2527

@@ -30,15 +32,16 @@
3032
import com.helger.commons.charset.CharsetHelper;
3133
import com.helger.commons.io.EAppend;
3234
import com.helger.commons.io.file.FileHelper;
35+
import com.helger.commons.io.file.FileOperationManager;
3336
import com.helger.commons.io.file.FilenameHelper;
3437
import com.helger.commons.io.resource.FileSystemResource;
3538
import com.helger.commons.system.ENewLineMode;
39+
import com.helger.commons.system.EOperatingSystem;
3640
import com.helger.css.CCSS;
3741
import com.helger.css.CSSFilenameHelper;
3842
import com.helger.css.ECSSVersion;
3943
import com.helger.css.decl.CascadingStyleSheet;
4044
import com.helger.css.handler.ICSSParseExceptionCallback;
41-
import com.helger.css.parser.ParseException;
4245
import com.helger.css.reader.CSSReader;
4346
import com.helger.css.reader.CSSReaderSettings;
4447
import com.helger.css.writer.CSSWriter;
@@ -69,14 +72,24 @@ public final class CSSCompressMojo extends AbstractMojo
6972
private MavenProject project;
7073

7174
/**
72-
* The directory where the CSS files reside. It must be an existing directory.
75+
* The directory where the source CSS files reside. It must be an existing
76+
* directory.
7377
*
7478
* @required
7579
* @parameter property="sourceDirectory"
7680
* default-value="${basedir}/src/main/resources"
7781
*/
7882
private File sourceDirectory;
7983

84+
/**
85+
* The directory where the target CSS files reside. If the directory is not
86+
* existing, it is created. If no target directory is provided, the source
87+
* directory will be used.
88+
*
89+
* @parameter property="targetDirectory"
90+
*/
91+
private File targetDirectory;
92+
8093
/**
8194
* Should all directories be scanned recursively for CSS files to compress?
8295
*
@@ -228,15 +241,23 @@ public final class CSSCompressMojo extends AbstractMojo
228241
private ENewLineMode newLineMode = CSSWriterSettings.DEFAULT_NEW_LINE_MODE;
229242

230243
@SuppressFBWarnings ({ "NP_UNWRITTEN_FIELD", "UWF_UNWRITTEN_FIELD" })
231-
public void setSourceDirectory (final File aDir)
244+
public void setSourceDirectory (final File aDir) throws IOException
232245
{
233246
sourceDirectory = aDir;
234247
if (!sourceDirectory.isAbsolute ())
235-
sourceDirectory = new File (project.getBasedir (), aDir.getPath ());
248+
sourceDirectory = new File (project.getBasedir (), aDir.getPath ()).getCanonicalFile ();
236249
if (!sourceDirectory.exists ())
237250
getLog ().error ("CSS source directory '" + sourceDirectory + "' does not exist!");
238251
}
239252

253+
public void setTargetDirectory (final File aDir) throws IOException
254+
{
255+
targetDirectory = aDir;
256+
if (!targetDirectory.isAbsolute ())
257+
targetDirectory = new File (project.getBasedir (), aDir.getPath ()).getCanonicalFile ();
258+
// The creation happens below, if the prerequisites are fulfilled
259+
}
260+
240261
public void setRecursive (final boolean bRecursive)
241262
{
242263
recursive = bRecursive;
@@ -363,37 +384,57 @@ private static boolean _isAlreadyCompressed (final String sFilename)
363384
}
364385

365386
@Nonnull
366-
private String _getRelativePath (@Nonnull final File aFile)
387+
private String _getSourceRelativePath (@Nonnull final File aFile)
367388
{
368389
return aFile.getAbsolutePath ().substring (sourceDirectory.getAbsolutePath ().length () + 1);
369390
}
370391

371-
private void _compressCSSFile (@Nonnull final File aChild)
392+
private void _compressCSSFile (@Nonnull final File aSourceFile)
372393
{
394+
final String sSourceRelativePath = _getSourceRelativePath (aSourceFile);
395+
373396
// Compress the file only if the compressed file is older than the original
374397
// file. Note: lastModified on a non-existing file returns 0L
375-
final File aCompressed = new File (FilenameHelper.getWithoutExtension (aChild.getAbsolutePath ()) +
376-
targetFileExtension);
377-
if (aCompressed.lastModified () < aChild.lastModified () || forceCompress)
398+
final boolean bTargetDirEqualsSourceDir = targetDirectory == null ||
399+
targetDirectory.getAbsolutePath ()
400+
.equals (sourceDirectory.getAbsolutePath ());
401+
final File aCompressedFile;
402+
{
403+
if (bTargetDirEqualsSourceDir)
404+
{
405+
// Use the custom extension
406+
aCompressedFile = new File (FilenameHelper.getWithoutExtension (aSourceFile.getAbsolutePath ()) +
407+
targetFileExtension);
408+
}
409+
else
410+
{
411+
// Source and target are different
412+
aCompressedFile = new File (targetDirectory,
413+
FilenameHelper.getWithoutExtension (sSourceRelativePath) + targetFileExtension);
414+
}
415+
}
416+
417+
if (aCompressedFile.lastModified () < aSourceFile.lastModified () || forceCompress)
378418
{
379419
if (verbose)
380-
getLog ().info ("Start compressing CSS file " + _getRelativePath (aChild));
420+
getLog ().info ("Start compressing CSS file " + sSourceRelativePath);
381421
else
382-
getLog ().debug ("Start compressing CSS file " + _getRelativePath (aChild));
383-
final ICSSParseExceptionCallback aExHdl = (@Nonnull final ParseException ex) -> getLog ().error ("Failed to parse CSS file " +
384-
_getRelativePath (aChild),
385-
ex);
422+
getLog ().debug ("Start compressing CSS file " + sSourceRelativePath);
423+
424+
final ICSSParseExceptionCallback aExHdl = ex -> getLog ().error ("Failed to parse CSS file " +
425+
sSourceRelativePath,
426+
ex);
386427
final Charset aFallbackCharset = CharsetHelper.getCharsetFromName (sourceEncoding);
387428
final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.CSS30)
388429
.setFallbackCharset (aFallbackCharset)
389430
.setCustomExceptionHandler (aExHdl)
390431
.setBrowserCompliantMode (browserCompliantMode)
391432
.setKeepDeprecatedProperties (keepDeprecatedProperties);
392-
final CascadingStyleSheet aCSS = CSSReader.readFromFile (aChild, aSettings);
433+
final CascadingStyleSheet aCSS = CSSReader.readFromFile (aSourceFile, aSettings);
393434
if (aCSS != null)
394435
{
395436
// We read it!
396-
final FileSystemResource aDestFile = new FileSystemResource (aCompressed);
437+
final FileSystemResource aDestFile = new FileSystemResource (aCompressedFile);
397438
try
398439
{
399440
final CSSWriterSettings aWriterSettings = new CSSWriterSettings (ECSSVersion.CSS30);
@@ -415,16 +456,16 @@ private void _compressCSSFile (@Nonnull final File aChild)
415456
}
416457
catch (final IOException ex)
417458
{
418-
getLog ().error ("Failed to write compressed CSS file '" + aCompressed.toString () + "' to disk", ex);
459+
getLog ().error ("Failed to write compressed CSS file '" + aCompressedFile.toString () + "' to disk", ex);
419460
}
420461
}
421462
}
422463
else
423464
{
424465
if (verbose)
425-
getLog ().info ("Ignoring already compressed CSS file " + _getRelativePath (aChild));
466+
getLog ().info ("Ignoring already compressed CSS file " + sSourceRelativePath);
426467
else
427-
getLog ().debug ("Ignoring already compressed CSS file " + _getRelativePath (aChild));
468+
getLog ().debug ("Ignoring already compressed CSS file " + sSourceRelativePath);
428469
}
429470
}
430471

@@ -453,6 +494,28 @@ public void execute () throws MojoExecutionException
453494
{
454495
if (verbose)
455496
getLog ().info ("Start compressing CSS files in directory " + sourceDirectory.getPath ());
497+
498+
// Consistency check
499+
if (targetDirectory != null)
500+
{
501+
// Case insensitive comparison on Windows
502+
final boolean bCaseInsensitiveOS = EOperatingSystem.getCurrentOS ().isWindowsBased ();
503+
final Function <File, String> fGetPath = bCaseInsensitiveOS ? f -> f.getAbsolutePath ().toLowerCase (Locale.ROOT)
504+
: File::getAbsolutePath;
505+
506+
if (recursive && fGetPath.apply (targetDirectory).startsWith (fGetPath.apply (sourceDirectory)))
507+
{
508+
throw new IllegalStateException ("Target directory MUST NOT be a child of the source directory in recursive mode");
509+
}
510+
511+
// Make sure, target directory exists
512+
if (!targetDirectory.exists ())
513+
{
514+
getLog ().info ("CSS target directory '" + targetDirectory + "' does not exist and will be created!");
515+
FileOperationManager.INSTANCE.createDirRecursive (targetDirectory);
516+
}
517+
}
518+
456519
_scanDirectory (sourceDirectory);
457520
}
458521
}

0 commit comments

Comments
 (0)