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.Iterator;
35  import java.util.List;
36  import java.util.Properties;
37  
38  /**
39   * @author Fabrizio Giustina
40   * @author Dennis Lundberg
41   * @version $Id: AbstractPluginConfigurationConverter.java 661727 2008-05-30 14:21:49Z bentmann $
42   */
43  public abstract class AbstractPluginConfigurationConverter
44      extends AbstractLogEnabled
45      implements PluginConfigurationConverter
46  {
47      public static final String TYPE_BUILD_PLUGIN = "build plugin";
48  
49      public static final String TYPE_REPORT_PLUGIN = "report plugin";
50  
51      private List listeners = new ArrayList();
52  
53      public abstract String getArtifactId();
54  
55      public String getGroupId()
56      {
57          return "org.apache.maven.plugins";
58      }
59  
60      public abstract String getType();
61  
62      public void addListeners( List listeners )
63      {
64          for ( Iterator i = listeners.iterator(); i.hasNext(); )
65          {
66              ConverterListener listener = (ConverterListener) i.next();
67              addListener( listener );
68          }
69      }
70  
71      public void addListener( ConverterListener listener )
72      {
73          if ( !listeners.contains( listener ) )
74          {
75              listeners.add( listener );
76          }
77      }
78  
79      /**
80       * Add a child element to the configuration.
81       *
82       * @param configuration     The configuration to add the element to
83       * @param projectProperties The M1 properties
84       * @param mavenOneProperty  The name of the Maven 1 property to convert
85       * @param mavenTwoElement   The name of the Maven 2 configuration element
86       */
87      protected void addConfigurationChild( Xpp3Dom configuration, Properties projectProperties, String mavenOneProperty,
88                                            String mavenTwoElement )
89      {
90          String value = projectProperties.getProperty( mavenOneProperty );
91          addConfigurationChild( configuration, mavenTwoElement, value );
92      }
93  
94      /**
95       * Add a child element to the configuration.
96       *
97       * @param configuration   The configuration to add the element to
98       * @param mavenTwoElement The name of the Maven 2 configuration element
99       * @param value           Set the value of the element to this
100      */
101     protected void addConfigurationChild( Xpp3Dom configuration, String mavenTwoElement, String value )
102     {
103         if ( value != null )
104         {
105             Xpp3Dom child = new Xpp3Dom( mavenTwoElement );
106             child.setValue( value );
107             configuration.addChild( child );
108         }
109     }
110 
111     public void convertConfiguration( Model v4Model, org.apache.maven.model.v3_0_0.Model v3Model,
112                                       Properties projectProperties )
113         throws ProjectConverterException
114     {
115         boolean addPlugin = false;
116 
117         Xpp3Dom configuration = new Xpp3Dom( "configuration" );
118 
119         buildConfiguration( configuration, v3Model, projectProperties );
120 
121         if ( configuration.getChildCount() > 0 )
122         {
123             if ( TYPE_BUILD_PLUGIN.equals( getType() ) )
124             {
125                 Plugin plugin = ModelUtils.findBuildPlugin( v4Model, getGroupId(), getArtifactId() );
126                 if ( plugin == null )
127                 {
128                     addPlugin = true;
129                     plugin = new Plugin();
130                     plugin.setGroupId( getGroupId() );
131                     plugin.setArtifactId( getArtifactId() );
132                 }
133 
134                 plugin.setConfiguration( configuration );
135 
136                 if ( addPlugin )
137                 {
138                     if ( v4Model.getBuild() == null )
139                     {
140                         v4Model.setBuild( new Build() );
141                     }
142                     v4Model.getBuild().addPlugin( plugin );
143                     sendInfoMessage( "Adding plugin " + plugin.getGroupId() + ":" + plugin.getArtifactId() );
144                     fireAddPluginEvent( plugin );
145                 }
146             }
147             else if ( TYPE_REPORT_PLUGIN.equals( getType() ) )
148             {
149                 ReportPlugin plugin = ModelUtils.findReportPlugin( v4Model, getGroupId(), getArtifactId() );
150                 if ( plugin == null )
151                 {
152                     addPlugin = true;
153                     plugin = new ReportPlugin();
154                     plugin.setGroupId( getGroupId() );
155                     plugin.setArtifactId( getArtifactId() );
156                 }
157 
158                 plugin.setConfiguration( configuration );
159 
160                 if ( addPlugin )
161                 {
162                     if ( v4Model.getReporting() == null )
163                     {
164                         v4Model.setReporting( new Reporting() );
165                     }
166                     v4Model.getReporting().addPlugin( plugin );
167                     sendInfoMessage( "Adding report " + plugin.getGroupId() + ":" + plugin.getArtifactId() );
168                     fireAddReportEvent( plugin );
169                 }
170             }
171         }
172     }
173 
174     protected abstract void buildConfiguration( Xpp3Dom configuration, org.apache.maven.model.v3_0_0.Model v3Model,
175                                                 Properties projectProperties )
176         throws ProjectConverterException;
177 
178     private void sendInfoMessage( String message )
179     {
180         getLogger().info( message );
181 
182         for ( Iterator i = listeners.iterator(); i.hasNext(); )
183         {
184             ConverterListener listener = (ConverterListener) i.next();
185             listener.info( message );
186         }
187     }
188 
189     private void fireAddPluginEvent( Plugin plugin )
190     {
191         for ( Iterator i = listeners.iterator(); i.hasNext(); )
192         {
193             ConverterListener listener = (ConverterListener) i.next();
194             listener.addPluginEvent( plugin.getGroupId(), plugin.getArtifactId() );
195         }
196     }
197 
198     private void fireAddReportEvent( ReportPlugin plugin )
199     {
200         for ( Iterator i = listeners.iterator(); i.hasNext(); )
201         {
202             ConverterListener listener = (ConverterListener) i.next();
203             listener.addReportEvent( plugin.getGroupId(), plugin.getArtifactId() );
204         }
205     }
206 }