All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.i18n.plugin.GenerateMojo 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.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
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.i18n.spi.GetterFile;
import org.nuiton.io.SortedProperties;
import org.nuiton.plugin.PluginHelper;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * Merge new generated i18n bundles with older existing ones.
 *
 * @author Julien Ruchaud - [email protected]
 * @author Tony Chemit - [email protected]
 */
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
@Execute(goal = "collect-i18n-artifacts")
public class GenerateMojo extends I18nMojoSupport {

    /**
     * Strict mode to only keep in user i18n detected i18n keys and remove obsolete keys.
     * 

* Note : By default not active. Use this with care since it can * delete keys. Moreover if this flag is activated, then all files will be parsed. */ @Parameter(property = "i18n.strictMode", defaultValue = "false") private boolean strictMode; /** * A flag to check that bundles are complete (no missing i18n translations). * * @since 1.0.0 */ @Parameter(property = "i18n.checkBundle", defaultValue = "true", required = true) private boolean checkBundle; /** * A flag to show missing 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 make the build fails if there is some missing key values. *

* Note : This parameter should be used in a release profile to ensure bundles are complete. * * @since 3.5.1 */ @Parameter(property = "i18n.failsIfAnyKeyMissingValue", defaultValue = "false") private boolean failsIfAnyKeyMissingValue; /** * A flag to make the build fails if there is some missing keys. *

* Note : This parameter should be used in a release profile to ensure bundles are complete. * * @since 3.5.1 */ @Parameter(property = "i18n.failsIfAnyKeyMissingInBundle", defaultValue = "false") private boolean failsIfAnyKeyMissingInBundle; /** * To remove any keys already existing in the module i18n dependencies and whic values diverge. *

* Note: Be ware, you could loose some overriden translations using this option. *

* Note: By default, the property is not active since we do not want to remove any keys. * * @since 4.0 */ @Parameter(property = "i18n.removeDuplicatedKeys", defaultValue = "false") private boolean removeDuplicatedKeys; /** * To fail build if there is any keys already existing in the module i18n dependencies. *

* Note: By default, the property is active: this is the default behaviour to not override some i18n * translations. *

* For projects migrating from version 3.0, your build could breaks with this new options, but for the best I suppose. * * @since 4.0 */ @Parameter(property = "i18n.failsIfDuplicatedKeys", defaultValue = "true") private boolean failsIfDuplicatedKeys; /** * To keep getters at the end. * Note: this is more for test purpose, be ware this can then break the caching mechanism... * * @since 4.0 */ @Parameter(property = "i18n.keepGetters", defaultValue = "false") private boolean keepGetters; /** * Directory where to get dependencies bundles. * * @since 4.0 */ @Parameter(property = "i18n.collectDirectory", defaultValue = "${project.build.directory}/i18n/collect", required = true) private File collectDirectory; /** * Base name of file which contains bundles locations. * * @since 1.0.2 */ @Parameter(property = "i18n.collectBundleName", defaultValue = "${project.artifactId}-collect", required = true) private String collectBundleName; /** * To copy i18n bundle to classes. * Note: The default behaviour is to do this copy, in some special cases you may want to override this * (for example for a final application using the bundle mojo). * * @since 4.0 */ @Parameter(property = "i18n.copyToClasses", defaultValue = "false") private boolean copyToClasses; /** All getters files detected. */ private List getterFiles; @Override public void init() throws Exception { super.init(); getterFiles = GetterFile.load(gettersDirectory); for (GetterFile getterFile : getterFiles) { getterFile.load(); } } @Override protected boolean checkSkip() { boolean result = true; if (!needInvoke(true, false, getProjectCacheKey())) { getLog().info("Skip - already executed."); result = false; } if (getterFiles.isEmpty()) { getLog().info("No getter detected - all files are up to date."); result = false; } if (!result) { copyI18nBundleToClasses(); } return result; } @Override protected void doAction() throws Exception { if (!silent) { getLog().info("config - sourceDirectory : " + sourceDirectory.getAbsolutePath()); getLog().info("config - locales : " + Arrays.toString(locales)); } BundleValidation bundleValidation = new BundleValidation(locales); Set detectedKeys = getterFiles.stream().flatMap(g -> g.getKeys().stream()).collect(Collectors.toSet()); for (Locale locale : locales) { if (!silent) { getLog().info("prepare bundle for locale " + locale); } File bundleSrc = getI18nFile(sourceDirectory, artifactId, locale, false); SortedProperties propertiesSrc = new SortedProperties(encoding); if (bundleSrc.exists()) { propertiesSrc.load(bundleSrc); } SortedProperties propertiesOut = new SortedProperties(encoding); Set keysToKeep = new TreeSet<>(detectedKeys); if (!strictMode) { // Also add source keys keysToKeep.addAll(propertiesSrc.stringPropertyNames()); } for (String key : keysToKeep) { String value = propertiesSrc.getProperty(key); if (value == null) { // new key value = ""; } propertiesOut.put(key, value); } getLog().info(String.format("For locale %s got %d key(s).", locale, propertiesOut.size())); Properties dependenciesBundle = loadDependenciesKeys(locale, bundleSrc); Set dependenciesKeys = new TreeSet<>(); dependenciesBundle.keySet().forEach(e -> dependenciesKeys.add((String) e)); Set allKeys = new TreeSet<>(); propertiesOut.keySet().forEach(e -> allKeys.add((String) e)); allKeys.retainAll(dependenciesKeys); Properties moduleDivergence = new Properties(); if (!allKeys.isEmpty()) { Iterator iterator = allKeys.iterator(); while (iterator.hasNext()) { String key = iterator.next(); String moduleValue = propertiesOut.getProperty(key); String dependenciesValue = dependenciesBundle.getProperty(key); if (StringUtils.isEmpty(moduleValue) || Objects.equals(moduleValue, dependenciesValue)) { if (verbose) { getLog().info(String.format("For locale %s, removing duplicated key %s", locale, key)); } iterator.remove(); propertiesOut.remove(key); } else { moduleDivergence.put(key, Pair.of(dependenciesValue, moduleValue)); } } } if (!allKeys.isEmpty()) { // Got overrides keys in this module if (failsIfDuplicatedKeys) { throw new MojoFailureException( String.format("For locale %s found %d duplicated key(s):\n%s\n\nDivergence values:\n%s", locale, allKeys.size(), Joiner.on("\n").join(allKeys), Joiner.on("\n").join(moduleDivergence.entrySet()))); } getLog().warn(String.format("For locale %s found %d duplicated key(s):\n%s\n\nDivergence values:\n%s", locale, allKeys.size(), Joiner.on("\n").join(allKeys), Joiner.on("\n").join(moduleDivergence.entrySet()))); if (removeDuplicatedKeys) { allKeys.forEach(propertiesOut::remove); getLog().info(String.format("For locale %s, remove %d duplicated key(s).", locale, allKeys.size())); } } if (!silent) { getLog().info(String.format("merge bundle %s to outputDirectory", locale)); } if (checkBundle) { ImmutableSet keys = Maps.fromProperties(propertiesOut).keySet(); bundleValidation.getKeysPerLocale().putAll(locale, keys); checkBundle(locale, propertiesOut, showEmpty, bundleValidation); } PluginHelper.createDirectoryIfNecessary(bundleSrc.getParentFile()); propertiesOut.store(bundleSrc); if (!silent) { getLog().info(String.format("copy bundle %s to sourceDirectory", locale)); } } if (!keepGetters) { getterFiles.forEach(GetterFile::delete); deleteFile(gettersDirectory); } failsIfAnyKeyMissingValue(failsIfAnyKeyMissingValue, bundleValidation); failsIfAnyKeyMissingInBundle(failsIfAnyKeyMissingInBundle, bundleValidation); copyI18nBundleToClasses(); } private Properties loadDependenciesKeys(Locale locale, File bundleOut) throws IOException { Properties result = new Properties(); Objects.requireNonNull(locale); File i18nFile = getI18nFile(collectDirectory, collectBundleName, locale, false); getLog().debug(String.format("dependencies file: %s", i18nFile)); if (i18nFile.exists()) { List urls = Files.readLines(i18nFile, charset); for (String url : urls) { getLog().debug(String.format("url to test: %s", url)); if (url.startsWith("file:")) { File f = new File(new URL(url).getFile()); if (f.getParentFile().equals(bundleOut.getParentFile()) || f.getName().equals(bundleOut.getName())) // this is the file of this module continue; } try (Reader reader = new InputStreamReader(new URL(url).openStream(), charset)) { result.load(reader); } } } return result; } private void copyI18nBundleToClasses() { if (!copyToClasses) { return; } Set bundleFiles = new LinkedHashSet<>(); for (Locale locale : locales) { try { File bundleSrc = getI18nFile(sourceDirectory, artifactId, locale, false); if (bundleSrc.exists()) { bundleFiles.add(bundleSrc); } } catch (IOException e) { throw new IllegalStateException(e); } } File outputDir = new File(buildOutputDirectory, "i18n"); for (File bundleFile : bundleFiles) { try { PluginHelper.copy(bundleFile, new File(outputDir, bundleFile.getName())); } catch (IOException e) { throw new RuntimeException("Can't copy some files...", e); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy