View Javadoc

1   package org.apache.maven.model.converter.relocators;
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.Model;
23  import org.apache.maven.model.Plugin;
24  import org.apache.maven.model.ReportPlugin;
25  import org.apache.maven.model.converter.ConverterListener;
26  import org.apache.maven.model.converter.ModelUtils;
27  import org.codehaus.plexus.logging.AbstractLogEnabled;
28  
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * A general implementation of the <code>PluginRelocator</code> interface.
35   *
36   * @author Dennis Lundberg
37   * @version $Id: AbstractPluginRelocator.java 661727 2008-05-30 14:21:49Z bentmann $
38   */
39  public abstract class AbstractPluginRelocator
40      extends AbstractLogEnabled
41      implements PluginRelocator
42  {
43      private List listeners = new ArrayList();
44  
45      /**
46       * If there is no replacement for this plugin, you can have the plugin
47       * removed from the v4 pom by returning <code>null</code> from this method
48       * and from getNewGroupId().
49       *
50       * @return The artifactId of the new Maven 2 plugin
51       */
52      public abstract String getNewArtifactId();
53  
54      /**
55       * If there is no replacement for this plugin, you can have the plugin
56       * removed from the v4 pom by returning <code>null</code> from this method
57       * and from getNewArtifactId().
58       *
59       * @return The groupId of the new Maven 2 plugin
60       */
61      public abstract String getNewGroupId();
62  
63      /**
64       * <strong>Note:</strong> Because we are working on the recently converted
65       * Maven 2 model, this method must return the artifactId that is in the
66       * model, after the model has been converted.
67       *
68       * @return The artifactId of the Maven 1 plugin.
69       * @see org.apache.maven.model.converter.PomV3ToV4Translator#translateDependencies(java.util.List)
70       */
71      public abstract String getOldArtifactId();
72  
73      /**
74       * <strong>Note:</strong> Because we are working on the recently converted
75       * Maven 2 model, this method must return the groupId that is in the model,
76       * after the model has been converted.
77       * <p/>
78       * Feel free to overload this method if your plugin has a different groupId.
79       * </p>
80       *
81       * @return The groupId of the Maven 1 plugin.
82       * @see org.apache.maven.model.converter.PomV3ToV4Translator#translateDependencies(java.util.List)
83       */
84      public String getOldGroupId()
85      {
86          return "org.apache.maven.plugins";
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public void relocate( Model v4Model )
93      {
94          // Relocate build plugins
95          Plugin oldBuildPlugin = ModelUtils.findBuildPlugin( v4Model, getOldGroupId(), getOldArtifactId() );
96          if ( oldBuildPlugin != null )
97          {
98              if ( getNewArtifactId() == null && getNewGroupId() == null )
99              {
100                 // Remove the old plugin
101                 v4Model.getBuild().getPlugins().remove( oldBuildPlugin );
102                 sendInfoMessage( "Removing build plugin " + getOldGroupId() + ":" + getOldArtifactId() );
103                 fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
104             }
105             else
106             {
107                 Plugin newPlugin = ModelUtils.findBuildPlugin( v4Model, getNewGroupId(), getNewArtifactId() );
108                 if ( newPlugin == null )
109                 {
110                     // The new plugin does not exist, relocate the old one
111                     oldBuildPlugin.setArtifactId( getNewArtifactId() );
112                     oldBuildPlugin.setGroupId( getNewGroupId() );
113                     sendInfoMessage( "Relocating build plugin " + getOldGroupId() + ":" + getOldArtifactId() );
114                     fireRelocatePluginEvent( getOldGroupId(), getOldArtifactId(), getNewGroupId(), getNewArtifactId() );
115                 }
116                 else
117                 {
118                     // The new plugin already exist, remove the old one
119                     v4Model.getBuild().getPlugins().remove( oldBuildPlugin );
120                     sendInfoMessage( "Removing old build plugin " + getOldGroupId() + ":" + getOldArtifactId()
121                         + " because the new one already exist" );
122                     fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
123                 }
124             }
125         }
126 
127         // Relocate report plugins
128         ReportPlugin oldReportPlugin = ModelUtils.findReportPlugin( v4Model, getOldGroupId(), getOldArtifactId() );
129         if ( oldReportPlugin != null )
130         {
131             if ( getNewArtifactId() == null && getNewGroupId() == null )
132             {
133                 // Remove the old plugin
134                 v4Model.getReporting().getPlugins().remove( oldReportPlugin );
135                 sendInfoMessage( "Removing report plugin " + getOldGroupId() + ":" + getOldArtifactId() );
136                 fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
137             }
138             else
139             {
140                 ReportPlugin newPlugin = ModelUtils.findReportPlugin( v4Model, getNewGroupId(), getNewArtifactId() );
141                 if ( newPlugin == null )
142                 {
143                     // The new plugin does not exist, relocate the old one
144                     oldReportPlugin.setArtifactId( getNewArtifactId() );
145                     oldReportPlugin.setGroupId( getNewGroupId() );
146                     sendInfoMessage( "Relocating report plugin " + getOldGroupId() + ":" + getOldArtifactId() );
147                     fireRelocateReportEvent( getOldGroupId(), getOldArtifactId(), getNewGroupId(), getNewArtifactId() );
148                 }
149                 else
150                 {
151                     // The new plugin already exist, remove the old one
152                     v4Model.getReporting().getPlugins().remove( oldReportPlugin );
153                     sendInfoMessage( "Removing old report plugin " + getOldGroupId() + ":" + getOldArtifactId()
154                         + " because the new one already exist" );
155                     fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
156                 }
157             }
158         }
159     }
160 
161     public void addListener( ConverterListener listener )
162     {
163         if ( !listeners.contains( listener ) )
164         {
165             listeners.add( listener );
166         }
167     }
168 
169     public void addListeners( List listeners )
170     {
171         for ( Iterator i = listeners.iterator(); i.hasNext(); )
172         {
173             ConverterListener listener = (ConverterListener) i.next();
174             addListener( listener );
175         }
176     }
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 fireRelocatePluginEvent( String oldGroupId, String oldArtifactId, String newGroupId,
190                                           String newArtifactId )
191     {
192         for ( Iterator i = listeners.iterator(); i.hasNext(); )
193         {
194             ConverterListener listener = (ConverterListener) i.next();
195             listener.relocatePluginEvent( oldGroupId, oldArtifactId, newGroupId, newArtifactId );
196         }
197     }
198 
199     private void fireRelocateReportEvent( String oldGroupId, String oldArtifactId, String newGroupId,
200                                           String newArtifactId )
201     {
202         for ( Iterator i = listeners.iterator(); i.hasNext(); )
203         {
204             ConverterListener listener = (ConverterListener) i.next();
205             listener.relocateReportEvent( oldGroupId, oldArtifactId, newGroupId, newArtifactId );
206         }
207     }
208 
209     private void fireRemovePluginEvent( String groupId, String artifactId )
210     {
211         for ( Iterator i = listeners.iterator(); i.hasNext(); )
212         {
213             ConverterListener listener = (ConverterListener) i.next();
214             listener.removePluginEvent( groupId, artifactId );
215         }
216     }
217 
218     private void fireRemoveReportEvent( String groupId, String artifactId )
219     {
220         for ( Iterator i = listeners.iterator(); i.hasNext(); )
221         {
222             ConverterListener listener = (ConverterListener) i.next();
223             listener.removeReportEvent( groupId, artifactId );
224         }
225     }
226 }