
org.nuiton.i18n.plugin.CheckBundlesIntegrityMojo Maven / Gradle / Ivy
/*
* #%L
* I18n :: Maven Plugin
* %%
* Copyright (C) 2007 - 2017 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.i18n.plugin;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.SortedSetMultimap;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.nuiton.i18n.plugin.bundle.BundleValidation;
import org.nuiton.io.SortedProperties;
/**
* Check bundles integrity. That all keys have a value in all bundles.
*
* @since 3.5
*/
@Mojo(name = "check-bundles-integrity", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
public class CheckBundlesIntegrityMojo extends I18nMojoSupport {
/** Directory where to find project i18n files. */
@Parameter(property = "i18n.sourceDirectory", defaultValue = "${basedir}/src/main/i18n", required = true)
private File sourceDirectory;
/**
* A flag to show empty i18n translation.
*
* Note : Need the {@link #checkBundle} to be activated).
*
* @since 1.0.0
*/
@Parameter(property = "i18n.showEmpty", defaultValue = "false", required = true)
private boolean showEmpty;
/**
* A flag to show missing i18n translation.
*
* @since 3.5
*/
@Parameter(property = "i18n.showMissing", defaultValue = "false", required = true)
private boolean showMissing;
/**
* A flag to make the build fails if there is some warnings while generating
* bundle, says when it misses some translations.
*
* Note : This parameter should be used in a release profile to
* ensure bundles are complete.
*
* @since 2.0
*/
@Parameter(property = "i18n.failsIfWarning", defaultValue = "false")
private boolean failsIfWarning;
@Override
protected boolean checkSkip() {
boolean result = super.checkSkip();
if (result && !needInvoke(true, false, getProjectCacheKey())) {
getLog().info("Skip - already executed.");
result = false;
}
return result;
}
@Override
protected void doAction() throws Exception {
if (!silent) {
getLog().info(String.format("config - sourceDirectory : %s", sourceDirectory.getAbsolutePath()));
getLog().info(String.format("config - locales : %s", Arrays.toString(locales)));
}
BundleValidation bundleValidation = new BundleValidation(locales);
for (Locale locale : locales) {
File bundleSrc = getI18nFile(sourceDirectory, artifactId, locale, false);
SortedProperties propertiesSrc = new SortedProperties(encoding);
if (bundleSrc.exists()) {
propertiesSrc.load(bundleSrc);
}
checkBundle(locale, propertiesSrc, showEmpty, bundleValidation);
ImmutableSet keys = Maps.fromProperties(propertiesSrc).keySet();
bundleValidation.getKeysPerLocale().putAll(locale, keys);
}
SortedSetMultimap missingKeysPerLocale = bundleValidation.getMissingKeysPerLocale();
if (missingKeysPerLocale.isEmpty()) {
if (!silent) {
getLog().info(String.format("all locales for bundle %s are consistent", artifactId));
}
} else {
if (showMissing) {
for (Map.Entry entry : missingKeysPerLocale.entries()) {
Locale locale = entry.getKey();
String missingKey = entry.getValue();
getLog().warn(String.format("bundle %s for locale %s misses key %s", artifactId, locale, missingKey));
}
} else {
for (Map.Entry> entry : missingKeysPerLocale.asMap().entrySet()) {
Locale locale = entry.getKey();
int numberOfMissingKeys = entry.getValue().size();
getLog().warn(String.format("bundle %s for locale %s misses %d keys! (use -Di18n.showMissing to see these entries)", artifactId, locale, numberOfMissingKeys));
}
}
}
if (failsIfWarning && bundleValidation.isFail()) {
// there is at least one not complete bundle, fail the build
throw new MojoFailureException("Bundles validation failed, see warning above for details about how to fix");
}
}
}