r634 - in trunk: . src/main/java/org/nuiton/helper/plugin src/main/resources src/main/resources/org src/main/resources/org/nuiton src/main/resources/org/nuiton/helper src/main/resources/org/nuiton/helper/plugin
Author: tchemit Date: 2009-11-24 15:47:57 +0100 (Tue, 24 Nov 2009) New Revision: 634 Added: trunk/src/main/java/org/nuiton/helper/plugin/GenerateSiteIndexPlugin.java trunk/src/main/resources/org/ trunk/src/main/resources/org/nuiton/ trunk/src/main/resources/org/nuiton/helper/ trunk/src/main/resources/org/nuiton/helper/plugin/ trunk/src/main/resources/org/nuiton/helper/plugin/site-index.vm Modified: trunk/pom.xml Log: Evolution #128: Ajout d'un goal pour g?\195?\169n?\195?\169rer une page index.html dans les site respectant la symetricLayout Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2009-11-12 19:10:51 UTC (rev 633) +++ trunk/pom.xml 2009-11-24 14:47:57 UTC (rev 634) @@ -210,6 +210,11 @@ <version>1.2.14</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-velocity</artifactId> + <version>1.1.7</version> + </dependency> </dependencies> Added: trunk/src/main/java/org/nuiton/helper/plugin/GenerateSiteIndexPlugin.java =================================================================== --- trunk/src/main/java/org/nuiton/helper/plugin/GenerateSiteIndexPlugin.java (rev 0) +++ trunk/src/main/java/org/nuiton/helper/plugin/GenerateSiteIndexPlugin.java 2009-11-24 14:47:57 UTC (rev 634) @@ -0,0 +1,220 @@ +package org.nuiton.helper.plugin; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; +import org.apache.maven.settings.Settings; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.context.Context; +import org.apache.velocity.exception.ResourceNotFoundException; +import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.velocity.VelocityComponent; +import org.nuiton.plugin.AbstractPlugin; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; + +/** + * Generate a simple index.html page for the site using the symetricLayout. + * + * @author chemit + * @goal generate-site-index + * @phase pre-site + * @requiresProject true + * @since 1.1.1 + */ +public class GenerateSiteIndexPlugin extends AbstractPlugin { + + /** + * Directory where the template file will be generated. + * + * @parameter expression="${helper.templateOutputDirectory}" default-value="${project.reporting.outputDirectory}" + * @required + * @since 1.1.1 + */ + protected File templateOutputDirectory; + /** + * Directory that contains the template. + * <p> + * <b>Note:</b> This directory must be a subdirectory of + * <code>/src/main/resources/ or current project base directory</code>. + * </p> + * + * @parameter expression="${helper.templateDirectory}" default-value="org/nuiton/helper/plugin" + * @required + * @since 1.1.1 + */ + protected String templateDirectory; + /** + * Velocity template name to use. + * + * @parameter expression="${helper.template}" default-value="site-index.vm" + * @required + * @since 1.1.1 + */ + protected String template; + /** + * The template encoding. + * + * @parameter expression="${helper.templateEncoding}" default-value="${project.reporting.outputEncoding}" + * @since 1.1.1 + */ + protected String templateEncoding; + /** + * The name of the artifact to be used in the announcement. + * + * @parameter expression="${redmine.title}" default-value="${project.name}" + * @required + * @since 1.1.1 + */ + protected String title; + /** + * A comma separated list of locales supported by Maven. The first valid token will be the default Locale + * for this instance of the Java Virtual Machine. + * + * @parameter expression="${locales}" + * @required + */ + protected String locales; + /** + * Dependance du projet. + * + * @parameter default-value="${project}" + * @required + * @readonly + * @since 1.1.1 + */ + protected MavenProject project; + /** + * Dependance du settings. + * + * @parameter default-value="${settings}" + * @required + * @readonly + * @since 1.1.1 + */ + protected Settings settings; + + /** + * Un flag pour activer le mode verbeux. + * + * @parameter expression="${helper.verbose}" default-value="${maven.verbose}" + * @since 1.1.1 + */ + protected boolean verbose; +// /** +// * Un flag pour forcer la génération de la page html. +// * +// * @parameter expression="${helper.force}" default-value="false" +// * @since 1.1.1 +// */ +// protected boolean force; + + /** + * The first locale of the given locales. + */ + protected String defaultLocale; + + /** + * Velocity Component. + * + * @component roleHint="maven-helper-plugin" + */ + protected VelocityComponent velocity; + + @Override + protected boolean init() throws Exception { + if (StringUtils.isEmpty(templateEncoding)) { + templateEncoding = ReaderFactory.FILE_ENCODING; + getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding + ", i.e. build is platform dependent!"); + } + int firstComma = locales.indexOf(","); + if (firstComma == -1) { + defaultLocale = locales; + } else { + defaultLocale = locales.substring(0, firstComma); + } + + return true; + } + + @Override + protected void doAction() throws Exception { + + + File out = new File(templateOutputDirectory, "index.html"); + if (verbose) { + getLog().info("destination file " + out); + } + + VelocityEngine engine = velocity.getEngine(); + + engine.setApplicationAttribute("baseDirectory", project.getBasedir()); + + // prepare velocity context + + Context context = new VelocityContext(); + + context.put("project", project); + context.put("title", title); + context.put("defaultLocale", defaultLocale); + + String templatePath = templateDirectory + "/" + template; + + // generate index.html + + try { + + if (!out.getParentFile().exists()) { + out.getParentFile().mkdirs(); + } + + Writer writer = new OutputStreamWriter(new FileOutputStream(out), templateEncoding); + + Template velocityTemplate = engine.getTemplate(templatePath, templateEncoding); + + velocityTemplate.merge(context, writer); + + writer.flush(); + + writer.close(); + + getLog().info("Created index.html from [" + template + "] in " + out); + + } catch (ResourceNotFoundException rnfe) { + throw new MojoExecutionException("Resource not found.", rnfe); + } catch (Exception e) { + throw new MojoExecutionException(e.toString(), e); + } + + } + + + public MavenProject getProject() { + return project; + } + + public void setProject(MavenProject project) { + this.project = project; + } + + public Settings getSettings() { + return settings; + } + + public void setSettings(Settings settings) { + this.settings = settings; + } + + public boolean isVerbose() { + return verbose; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } +} Property changes on: trunk/src/main/java/org/nuiton/helper/plugin/GenerateSiteIndexPlugin.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Added: trunk/src/main/resources/org/nuiton/helper/plugin/site-index.vm =================================================================== --- trunk/src/main/resources/org/nuiton/helper/plugin/site-index.vm (rev 0) +++ trunk/src/main/resources/org/nuiton/helper/plugin/site-index.vm 2009-11-24 14:47:57 UTC (rev 634) @@ -0,0 +1,9 @@ + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> + <head> + <title>${title}</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> + <meta http-equiv="Refresh" content="0; url=${defaultLocale}/index.html"> + </head> + <body /> +</html> \ No newline at end of file
participants (1)
-
tchemit@users.nuiton.org