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.Iterator;
35 import java.util.List;
36 import java.util.Properties;
37
38
39
40
41
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
81
82
83
84
85
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
96
97
98
99
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 }