View Javadoc

1   /*
2    *  jDTAUS Core Container Mojo
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.mojo;
22  
23  import java.io.File;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.jdtaus.core.container.ContainerError;
27  import org.jdtaus.core.container.ContextError;
28  import org.jdtaus.core.container.Implementation;
29  import org.jdtaus.core.container.ModelError;
30  import org.jdtaus.core.container.ModelFactory;
31  import org.jdtaus.core.container.Module;
32  
33  /**
34   * Mojo to commit container meta-data to compiled java test classes.
35   *
36   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
37   * @version $JDTAUS: JavaCommitTestsMojo.java 8743 2012-10-07 03:06:20Z schulte $
38   * @goal java-commit-tests
39   * @phase process-test-classes
40   * @requiresDependencyResolution test
41   */
42  public class JavaCommitTestsMojo extends JavaCommitMojo
43  {
44  
45      /** Creates a new {@code JavaCommitTestsMojo} instance. */
46      public JavaCommitTestsMojo()
47      {
48          super();
49      }
50  
51      public void execute() throws MojoExecutionException, MojoFailureException
52      {
53          final ClassLoader mavenLoader = Thread.currentThread().
54              getContextClassLoader();
55  
56          try
57          {
58              Thread.currentThread().setContextClassLoader(
59                  this.getTestClassLoader( mavenLoader ) );
60  
61              enableThreadContextClassLoader();
62  
63              final Module module = this.getTestModule();
64              if ( module != null )
65              {
66                  for ( int i = module.getImplementations().size() - 1; i >= 0;
67                        i-- )
68                  {
69                      final Implementation impl = module.getImplementations().
70                          getImplementation( i );
71  
72                      this.commitImplementation(
73                          ModelFactory.newModel(), impl,
74                          new File( this.getMavenProject().getBuild().
75                          getTestOutputDirectory() ) );
76  
77                  }
78              }
79          }
80          catch ( final ContextError e )
81          {
82              throw new MojoExecutionException( e.getMessage(), e );
83          }
84          catch ( final ContainerError e )
85          {
86              throw new MojoExecutionException( e.getMessage(), e );
87          }
88          catch ( final ModelError e )
89          {
90              throw new MojoExecutionException( e.getMessage(), e );
91          }
92          catch ( final Exception e )
93          {
94              throw new MojoExecutionException( e.getMessage(), e );
95          }
96          finally
97          {
98              disableThreadContextClassLoader();
99              Thread.currentThread().setContextClassLoader( mavenLoader );
100         }
101     }
102 
103 }