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

org.nuiton.i18n.plugin.bundle.CollectI18nArtifactsMojo Maven / Gradle / Ivy

/*
 * *##% 
 * I18n :: Maven Plugin
 * Copyright (C) 2007 - 2010 CodeLutin
 *
 * 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
 * .
 * ##%*
 */
package org.nuiton.i18n.plugin.bundle;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.shared.dependency.tree.DependencyNode;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
import org.nuiton.i18n.bundle.I18nBundleEntry;
import org.nuiton.plugin.DependencyUtil;
import org.nuiton.plugin.PluginHelper;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * Detects any i18n artifacts and store the result as a file.
 * 

* Created: 24 déc. 2009 * * @author tchemit * @version $Revision: 1711 $ *

* Mise a jour: $Date: 2010-03-09 19:18:28 +0100 (mar., 09 mars 2010) $ * par : $Author: tchemit $ * @goal collect-i18n-artifacts * @phase generate-resources * @requiresProject true * @requiresDependencyResolution runtime * @since 1.0.2 */ public class CollectI18nArtifactsMojo extends AbstractI18nBundleMojo { /** * Local Repository. * * @parameter expression="${localRepository}" * @required * @readonly * @since 1.0.2 */ protected ArtifactRepository localRepository; /** * Dependency tree builder component. * * @component * @since 1.0.2 */ protected DependencyTreeBuilder dependencyTreeBuilder; /** * Artifact Factory component. * * @component * @since 1.0.2 */ protected ArtifactFactory factory; /** * Artifact metadata source component. * * @component * @since 1.0.2 */ protected ArtifactMetadataSource artifactMetadataSource; /** * Artifact collector component. * * @component * @since 1.0.2 */ protected ArtifactCollector collector; I18nArtifact[] i18nArtifacts; @Override public void init() throws Exception { super.init(); PluginHelper.createDirectoryIfNecessary(collectOutputDir); } @Override protected void doAction() throws Exception { // detects the i18n artifacts (only once since it cost some times)... i18nArtifacts = detectI18nArtifacts(); if (i18nArtifacts.length == 0) { getLog().warn("no i18n artifact detected."); return; } for (Locale locale : locales) { if (!silent) { getLog().info("generate collected i18n artifacts for locale " + locale); } URL[] urls = getCollectI18nResources(locale); if (urls.length == 0) { getLog().warn("no i18n bundles for locale " + locale); return; } File bundleOut = getCollectOutputFile(locale, true); storeCollectI18nResources(bundleOut, urls); getLog().info("collected " + urls.length + " i18n artifacts for locale " + locale + " stored in " + bundleOut); } } @Override protected URL[] getCollectI18nResources(Locale locale) throws IOException, DependencyTreeBuilderException { // la locale par defaut est la première Locale defaultLocale = locales[0]; List urls = new ArrayList(); for (I18nArtifact artifact : i18nArtifacts) { I18nBundleEntry[] bundleEntries = artifact.getBundleEntries(locale, defaultLocale); for (I18nBundleEntry bundleEntry : bundleEntries) { URL path = bundleEntry.getPath(); urls.add(path); if (verbose) { getLog().info("add " + path); } } } return urls.toArray(new URL[urls.size()]); } protected void storeCollectI18nResources(File bundleOut, URL[] urls) throws IOException { StringBuilder buffer = new StringBuilder(); for (URL path : urls) { buffer.append(path).append("\n"); if (verbose) { getLog().info("add " + path); } } writeFile(bundleOut, buffer.toString(), encoding); } /** * Detecte les {@link I18nArtifact} et les retourne dans l'ordre de * chargement dans le système i18n, i.e l'ordre des dependances entre * artifacts. * * @return les artifacts i18nables triés par leur ordre de chargement dans * le système i18n. * @throws IOException while detecting bundles from * artifacts * @throws DependencyTreeBuilderException if any error while building the * depencendy tree */ protected I18nArtifact[] detectI18nArtifacts() throws IOException, DependencyTreeBuilderException { Map dico = new HashMap(); I18nArtifact i18nArtifact; for (Object o : project.getArtifacts()) { i18nArtifact = new I18nArtifact((Artifact) o); if (i18nArtifact.detectBundles()) { if (!silent) { getLog().info("detected i18n artifact " + i18nArtifact); } dico.put(i18nArtifact.getArtifact(), i18nArtifact); } else { if (getLog().isDebugEnabled()) { getLog().debug("reject artifact " + i18nArtifact); } } } ArtifactFilter artifactFilter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME); DependencyNode rootNode = dependencyTreeBuilder.buildDependencyTree( project, localRepository, factory, artifactMetadataSource, artifactFilter, collector); List artifacts = new ArrayList(dico.keySet()); DependencyUtil.sortArtifacts(rootNode, artifacts, getLog().isDebugEnabled()); // l'artifact du projet est traite en dernier car s'il possède des // bundles alors ils doivent etre charge en dernier Artifact projectArtifact = project.getArtifact(); i18nArtifact = new I18nArtifact(projectArtifact, src.getParentFile()); if (i18nArtifact.detectBundles()) { if (!silent) { getLog().info("detected i18n artifact " + i18nArtifact); } artifacts.add(i18nArtifact.getArtifact()); dico.put(i18nArtifact.getArtifact(), i18nArtifact); } else { if (getLog().isDebugEnabled()) { getLog().debug("reject artifact " + i18nArtifact); } } I18nArtifact[] result = new I18nArtifact[artifacts.size()]; int i = 0; for (Artifact artifact : artifacts) { result[i++] = dico.get(artifact); } return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy