1 package org.apache.maven.model.converter.plugins;
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.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
41
42
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
82
83
84
85
86
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
97
98
99
100
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
187
188
189
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 }