1   /*
2    * ========================================================================
3    *
4    * Codehaus CARGO, copyright 2004-2010 Vincent Massol.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   * ========================================================================
19   */
20  package com.atlassian.maven.plugins.amps.util.ant;
21  
22  import org.apache.maven.plugin.logging.Log;
23  import org.apache.tools.ant.BuildEvent;
24  import org.apache.tools.ant.BuildListener;
25  import org.apache.tools.ant.Project;
26  import org.codehaus.plexus.logging.Logger;
27  
28  /**
29   * Ant build listener used to collect logs from Ant tasks and to redirect them to a
30   * {@link Logger}.
31   *  
32   * @version $Id$
33   */
34  public class AntBuildListener implements BuildListener
35  {
36      /**
37       * Logger to where to redirect Ant logs.
38       */
39      private Log logger;
40  
41      /**
42       * @param logger the logger to which to log the Ant messages received
43       */
44      public AntBuildListener(Log logger)
45      {
46          this.logger = logger;
47      }
48  
49      /**
50       * {@inheritDoc}
51       * @see BuildListener#buildStarted(org.apache.tools.ant.BuildEvent)
52       */
53      public void buildStarted(BuildEvent event)
54      {
55          // Voluntarily do nothing
56      }
57  
58      /**
59       * {@inheritDoc}
60       * @see BuildListener#buildFinished(org.apache.tools.ant.BuildEvent)
61       */
62      public void buildFinished(BuildEvent event)
63      {
64          // Voluntarily do nothing
65      }
66  
67      /**
68       * {@inheritDoc}
69       * @see BuildListener#targetStarted(org.apache.tools.ant.BuildEvent)
70       */
71      public void targetStarted(BuildEvent event)
72      {
73          // Voluntarily do nothing
74      }
75  
76      /**
77       * {@inheritDoc}
78       * @see BuildListener#targetFinished(org.apache.tools.ant.BuildEvent)
79       */
80      public void targetFinished(BuildEvent event)
81      {
82          // Voluntarily do nothing
83      }
84  
85      /**
86       * {@inheritDoc}
87       * @see BuildListener#taskStarted(org.apache.tools.ant.BuildEvent)
88       */
89      public void taskStarted(BuildEvent event)
90      {
91          // Voluntarily do nothing
92      }
93  
94      /**
95       * {@inheritDoc}
96       * @see BuildListener#taskFinished(org.apache.tools.ant.BuildEvent)
97       */
98      public void taskFinished(BuildEvent event)
99      {
100         // Voluntarily do nothing
101     }
102 
103     /**
104      * {@inheritDoc}
105      * @see BuildListener#messageLogged(org.apache.tools.ant.BuildEvent)
106      */
107     public void messageLogged(BuildEvent event)
108     {
109         if ((event.getPriority() == Project.MSG_DEBUG)
110             || (event.getPriority() == Project.MSG_VERBOSE))
111         {
112             this.logger.debug(event.getMessage());
113         }
114         else if (event.getPriority() == Project.MSG_INFO)
115         {
116             this.logger.info(event.getMessage());
117         }
118         else if (event.getPriority() == Project.MSG_WARN)
119         {
120             this.logger.warn(event.getMessage());
121         }
122         else if (event.getPriority() == Project.MSG_ERR)
123         {
124             this.logger.error(event.getMessage());
125         }
126     }
127 }