You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by jasnook <ja...@ntlworld.com> on 2010/08/25 12:54:35 UTC

Using a Veloctity template in a custom Maven report

Hi,

I am trying to write a report plugin for Maven which will run during
the 'site' stage in the lifecycle.  The plugin needs to be able to use
a Velocity template to create a HTML file.

I have created a Mojo which extends AbstractMavenReport and within
that I have

/**
     * @component
     */
    private VelocityComponent velocityComponent;

so that I can get hold of the Plexus velocity component.  I am
currently passing this into a method and using this as:

public void createReport(VelocityComponent velocityComponent) throws
Exception {

        VelocityContext context = new VelocityContext();
        // put stuff into the context.

        Template template = null;
        try {
            Properties props = new Properties();
            props.setProperty("resource.loader",
"classpath");                       //makes no difference if set or
not

props.setProperty("classpath.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //
makes no difference if set or not
            velocityComponent.getEngine().init(props);
            template =
velocityComponent.getEngine().getTemplate("report.vm");
        }
        catch (ResourceNotFoundException rnfe) {
            System.out.println("Example : error : cannot find template
report.vm");
        }
        catch (ParseErrorException pee) {
            System.out.println("Example : Syntax error in template
report.vm: " + pee);
        }
        BufferedWriter writer = writer = new BufferedWriter(
                new OutputStreamWriter(System.out));  //testing by
writing to the screen
        if (template != null) {
            template.merge(context, writer);
        }
        writer.flush();
        writer.close();
    }

I am getting a ResourceNotFoundException when running this.  Where do
I need to put the vm file so that it is picked up or how should I be
using the velocitycomponent?

btw.  I am using Maven 2.2.1.

Thanks,
Jacqui 
-- 
View this message in context: http://maven.40175.n5.nabble.com/Using-a-Veloctity-template-in-a-custom-Maven-report-tp2652484p2652484.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Using a Veloctity template in a custom Maven report

Posted by jasnook <ja...@ntlworld.com>.
Thanks for the very fast reply.  However, I was thinking more along the lines
of having a self contained plugin (as described here -
http://docs.codehaus.org/display/MAVENUSER/Write+your+own+report+plugin)
which contains the velocity files.

Then I could call it from my code as:

<reporting>
  <plugins>
    <plugin>
      <groupId>custom-groupid</groupId>
      <artifactId>custom-artifactid</artifactId>
    </plugin>
  </plugins>
</reporting>

Cheers,
Jacqui
-- 
View this message in context: http://maven.40175.n5.nabble.com/Using-a-Veloctity-template-in-a-custom-Maven-report-tp2652484p2652550.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Using a Veloctity template in a custom Maven report

Posted by Gajo Csaba <cs...@cosylab.com>.
To build a website with "mvn site", you need to put your custom .vm file 
somewhere in your project directory, and then in your pom.xml configure 
the maven-site-plugin to use that .vm file.

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<templateFile>${basedir}/src/my-custom-template.vm</templateFile>
</configuration>
</plugin>

Try this and maybe the velocity component will "just work".

Cheers, Csaba


On 08/25/2010 12:54 PM, jasnook wrote:
> Hi,
>
> I am trying to write a report plugin for Maven which will run during
> the 'site' stage in the lifecycle.  The plugin needs to be able to use
> a Velocity template to create a HTML file.
>
> I have created a Mojo which extends AbstractMavenReport and within
> that I have
>
> /**
>       * @component
>       */
>      private VelocityComponent velocityComponent;
>
> so that I can get hold of the Plexus velocity component.  I am
> currently passing this into a method and using this as:
>
> public void createReport(VelocityComponent velocityComponent) throws
> Exception {
>
>          VelocityContext context = new VelocityContext();
>          // put stuff into the context.
>
>          Template template = null;
>          try {
>              Properties props = new Properties();
>              props.setProperty("resource.loader",
> "classpath");                       //makes no difference if set or
> not
>
> props.setProperty("classpath.resource.loader.class",
> "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //
> makes no difference if set or not
>              velocityComponent.getEngine().init(props);
>              template =
> velocityComponent.getEngine().getTemplate("report.vm");
>          }
>          catch (ResourceNotFoundException rnfe) {
>              System.out.println("Example : error : cannot find template
> report.vm");
>          }
>          catch (ParseErrorException pee) {
>              System.out.println("Example : Syntax error in template
> report.vm: " + pee);
>          }
>          BufferedWriter writer = writer = new BufferedWriter(
>                  new OutputStreamWriter(System.out));  //testing by
> writing to the screen
>          if (template != null) {
>              template.merge(context, writer);
>          }
>          writer.flush();
>          writer.close();
>      }
>
> I am getting a ResourceNotFoundException when running this.  Where do
> I need to put the vm file so that it is picked up or how should I be
> using the velocitycomponent?
>
> btw.  I am using Maven 2.2.1.
>
> Thanks,
> Jacqui
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org