1 package org.apache.maven.model.converter.relocators;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36
37
38
39 public abstract class AbstractPluginRelocator
40 extends AbstractLogEnabled
41 implements PluginRelocator
42 {
43 private List listeners = new ArrayList();
44
45
46
47
48
49
50
51
52 public abstract String getNewArtifactId();
53
54
55
56
57
58
59
60
61 public abstract String getNewGroupId();
62
63
64
65
66
67
68
69
70
71 public abstract String getOldArtifactId();
72
73
74
75
76
77
78
79
80
81
82
83
84 public String getOldGroupId()
85 {
86 return "org.apache.maven.plugins";
87 }
88
89
90
91
92 public void relocate( Model v4Model )
93 {
94
95 Plugin oldBuildPlugin = ModelUtils.findBuildPlugin( v4Model, getOldGroupId(), getOldArtifactId() );
96 if ( oldBuildPlugin != null )
97 {
98 if ( getNewArtifactId() == null && getNewGroupId() == null )
99 {
100
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
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
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
128 ReportPlugin oldReportPlugin = ModelUtils.findReportPlugin( v4Model, getOldGroupId(), getOldArtifactId() );
129 if ( oldReportPlugin != null )
130 {
131 if ( getNewArtifactId() == null && getNewGroupId() == null )
132 {
133
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
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
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 }