View Javadoc

1   package org.apache.maven.model.converter.plugins;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Build;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.model.Plugin;
25  import org.apache.maven.model.ReportPlugin;
26  import org.apache.maven.model.Reporting;
27  import org.apache.maven.model.converter.ConverterListener;
28  import org.apache.maven.model.converter.ModelUtils;
29  import org.apache.maven.model.converter.ProjectConverterException;
30  import org.codehaus.plexus.logging.AbstractLogEnabled;
31  import org.codehaus.plexus.util.xml.Xpp3Dom;
32  
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.Iterator;
36  import java.util.List;
37  import java.util.Properties;
38  
39  /**
40   * @author Fabrizio Giustina
41   * @author Dennis Lundberg
42   * @version $Id: AbstractPluginConfigurationConverter.java 695609 2008-09-15 20:29:30Z dennisl $
43   */
44  public abstract class AbstractPluginConfigurationConverter
45      extends AbstractLogEnabled
46      implements PluginConfigurationConverter
47  {
48      public static final String TYPE_BUILD_PLUGIN = "build plugin";
49  
50      public static final String TYPE_REPORT_PLUGIN = "report plugin";
51  
52      private List listeners = new ArrayList();
53  
54      public abstract String getArtifactId();
55  
56      public String getGroupId()
57      {
58          return "org.apache.maven.plugins";
59      }
60  
61      public abstract String getType();
62  
63      public void addListeners( List listeners )
64      {
65          for ( Iterator i = listeners.iterator(); i.hasNext(); )
66          {
67              ConverterListener listener = (ConverterListener) i.next();
68              addListener( listener );
69          }
70      }
71  
72      public void addListener( ConverterListener listener )
73      {
74          if ( !listeners.contains( listener ) )
75          {
76              listeners.add( listener );
77          }
78      }
79  
80      /**
81       * Add a child element to the configuration.
82       *
83       * @param configuration     The configuration to add the element to
84       * @param projectProperties The M1 properties
85       * @param mavenOneProperty  The name of the Maven 1 property to convert
86       * @param mavenTwoElement   The name of the Maven 2 configuration element
87       */
88      protected void addConfigurationChild( Xpp3Dom configuration, Properties projectProperties, String mavenOneProperty,
89                                            String mavenTwoElement )
90      {
91          String value = projectProperties.getProperty( mavenOneProperty );
92          addConfigurationChild( configuration, mavenTwoElement, value );
93      }
94  
95      /**
96       * Add a child element to the configuration.
97       *
98       * @param configuration   The configuration to add the element to
99       * @param mavenTwoElement The name of the Maven 2 configuration element
100      * @param value           Set the value of the element to this
101      */
102     protected void addConfigurationChild( Xpp3Dom configuration, String mavenTwoElement, String value )
103     {
104         if ( value != null )
105         {
106             Xpp3Dom child = new Xpp3Dom( mavenTwoElement );
107             child.setValue( value );
108             configuration.addChild( child );
109         }
110     }
111 
112     public void convertConfiguration( Model v4Model, org.apache.maven.model.v3_0_0.Model v3Model,
113                                       Properties projectProperties )
114         throws ProjectConverterException
115     {
116         boolean addPlugin = false;
117 
118         Xpp3Dom configuration = new Xpp3Dom( "configuration" );
119 
120         buildConfiguration( configuration, v3Model, projectProperties );
121 
122         if ( configuration.getChildCount() > 0 )
123         {
124             if ( TYPE_BUILD_PLUGIN.equals( getType() ) )
125             {
126                 Plugin plugin = ModelUtils.findBuildPlugin( v4Model, getGroupId(), getArtifactId() );
127                 if ( plugin == null )
128                 {
129                     addPlugin = true;
130                     plugin = new Plugin();
131                     plugin.setGroupId( getGroupId() );
132                     plugin.setArtifactId( getArtifactId() );
133                 }
134 
135                 plugin.setConfiguration( configuration );
136 
137                 if ( addPlugin )
138                 {
139                     if ( v4Model.getBuild() == null )
140                     {
141                         v4Model.setBuild( new Build() );
142                     }
143                     v4Model.getBuild().addPlugin( plugin );
144                     sendInfoMessage( "Adding plugin " + plugin.getGroupId() + ":" + plugin.getArtifactId() );
145                     fireAddPluginEvent( plugin );
146                 }
147             }
148             else if ( TYPE_REPORT_PLUGIN.equals( getType() ) )
149             {
150                 ReportPlugin plugin = ModelUtils.findReportPlugin( v4Model, getGroupId(), getArtifactId() );
151                 if ( plugin == null )
152                 {
153                     addPlugin = true;
154                     plugin = new ReportPlugin();
155                     plugin.setGroupId( getGroupId() );
156                     plugin.setArtifactId( getArtifactId() );
157                 }
158 
159                 plugin.setConfiguration( configuration );
160 
161                 if ( getReportSets() != null && !getReportSets().isEmpty() )
162                 {
163                     plugin.setReportSets( getReportSets() );
164                 }
165 
166                 if ( addPlugin )
167                 {
168                     if ( v4Model.getReporting() == null )
169                     {
170                         v4Model.setReporting( new Reporting() );
171                     }
172                     v4Model.getReporting().addPlugin( plugin );
173                     sendInfoMessage( "Adding report " + plugin.getGroupId() + ":" + plugin.getArtifactId() );
174                     fireAddReportEvent( plugin );
175                 }
176             }
177         }
178     }
179 
180     protected abstract void buildConfiguration( Xpp3Dom configuration, org.apache.maven.model.v3_0_0.Model v3Model,
181                                                 Properties projectProperties )
182         throws ProjectConverterException;
183 
184 
185     /**
186      * Get the report sets for this plugin. Override this method if the
187      * plugin you are converting needs report sets.
188      *
189      * @return This default implementation simply returns an empty list
190      */
191     protected List getReportSets()
192     {
193         return Collections.EMPTY_LIST;
194     }
195 
196     private void sendInfoMessage( String message )
197     {
198         getLogger().info( message );
199 
200         for ( Iterator i = listeners.iterator(); i.hasNext(); )
201         {
202             ConverterListener listener = (ConverterListener) i.next();
203             listener.info( message );
204         }
205     }
206 
207     private void fireAddPluginEvent( Plugin plugin )
208     {
209         for ( Iterator i = listeners.iterator(); i.hasNext(); )
210         {
211             ConverterListener listener = (ConverterListener) i.next();
212             listener.addPluginEvent( plugin.getGroupId(), plugin.getArtifactId() );
213         }
214     }
215 
216     private void fireAddReportEvent( ReportPlugin plugin )
217     {
218         for ( Iterator i = listeners.iterator(); i.hasNext(); )
219         {
220             ConverterListener listener = (ConverterListener) i.next();
221             listener.addReportEvent( plugin.getGroupId(), plugin.getArtifactId() );
222         }
223     }
224 }