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 | 26 | 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 | 26 | private List listeners = new ArrayList(); |
52 | |
|
53 | |
public abstract String getArtifactId(); |
54 | |
|
55 | |
public String getGroupId() |
56 | |
{ |
57 | 0 | return "org.apache.maven.plugins"; |
58 | |
} |
59 | |
|
60 | |
public abstract String getType(); |
61 | |
|
62 | |
public void addListeners( List listeners ) |
63 | |
{ |
64 | 0 | for ( Iterator i = listeners.iterator(); i.hasNext(); ) |
65 | |
{ |
66 | 0 | ConverterListener listener = (ConverterListener) i.next(); |
67 | 0 | addListener( listener ); |
68 | |
} |
69 | 0 | } |
70 | |
|
71 | |
public void addListener( ConverterListener listener ) |
72 | |
{ |
73 | 0 | if ( !listeners.contains( listener ) ) |
74 | |
{ |
75 | 0 | listeners.add( listener ); |
76 | |
} |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
protected void addConfigurationChild( Xpp3Dom configuration, Properties projectProperties, String mavenOneProperty, |
88 | |
String mavenTwoElement ) |
89 | |
{ |
90 | 206 | String value = projectProperties.getProperty( mavenOneProperty ); |
91 | 206 | addConfigurationChild( configuration, mavenTwoElement, value ); |
92 | 206 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
protected void addConfigurationChild( Xpp3Dom configuration, String mavenTwoElement, String value ) |
102 | |
{ |
103 | 248 | if ( value != null ) |
104 | |
{ |
105 | 163 | Xpp3Dom child = new Xpp3Dom( mavenTwoElement ); |
106 | 163 | child.setValue( value ); |
107 | 163 | configuration.addChild( child ); |
108 | |
} |
109 | 248 | } |
110 | |
|
111 | |
public void convertConfiguration( Model v4Model, org.apache.maven.model.v3_0_0.Model v3Model, |
112 | |
Properties projectProperties ) |
113 | |
throws ProjectConverterException |
114 | |
{ |
115 | 0 | boolean addPlugin = false; |
116 | |
|
117 | 0 | Xpp3Dom configuration = new Xpp3Dom( "configuration" ); |
118 | |
|
119 | 0 | buildConfiguration( configuration, v3Model, projectProperties ); |
120 | |
|
121 | 0 | if ( configuration.getChildCount() > 0 ) |
122 | |
{ |
123 | 0 | if ( TYPE_BUILD_PLUGIN.equals( getType() ) ) |
124 | |
{ |
125 | 0 | Plugin plugin = ModelUtils.findBuildPlugin( v4Model, getGroupId(), getArtifactId() ); |
126 | 0 | if ( plugin == null ) |
127 | |
{ |
128 | 0 | addPlugin = true; |
129 | 0 | plugin = new Plugin(); |
130 | 0 | plugin.setGroupId( getGroupId() ); |
131 | 0 | plugin.setArtifactId( getArtifactId() ); |
132 | |
} |
133 | |
|
134 | 0 | plugin.setConfiguration( configuration ); |
135 | |
|
136 | 0 | if ( addPlugin ) |
137 | |
{ |
138 | 0 | if ( v4Model.getBuild() == null ) |
139 | |
{ |
140 | 0 | v4Model.setBuild( new Build() ); |
141 | |
} |
142 | 0 | v4Model.getBuild().addPlugin( plugin ); |
143 | 0 | sendInfoMessage( "Adding plugin " + plugin.getGroupId() + ":" + plugin.getArtifactId() ); |
144 | 0 | fireAddPluginEvent( plugin ); |
145 | |
} |
146 | |
} |
147 | 0 | else if ( TYPE_REPORT_PLUGIN.equals( getType() ) ) |
148 | |
{ |
149 | 0 | ReportPlugin plugin = ModelUtils.findReportPlugin( v4Model, getGroupId(), getArtifactId() ); |
150 | 0 | if ( plugin == null ) |
151 | |
{ |
152 | 0 | addPlugin = true; |
153 | 0 | plugin = new ReportPlugin(); |
154 | 0 | plugin.setGroupId( getGroupId() ); |
155 | 0 | plugin.setArtifactId( getArtifactId() ); |
156 | |
} |
157 | |
|
158 | 0 | plugin.setConfiguration( configuration ); |
159 | |
|
160 | 0 | if ( addPlugin ) |
161 | |
{ |
162 | 0 | if ( v4Model.getReporting() == null ) |
163 | |
{ |
164 | 0 | v4Model.setReporting( new Reporting() ); |
165 | |
} |
166 | 0 | v4Model.getReporting().addPlugin( plugin ); |
167 | 0 | sendInfoMessage( "Adding report " + plugin.getGroupId() + ":" + plugin.getArtifactId() ); |
168 | 0 | fireAddReportEvent( plugin ); |
169 | |
} |
170 | |
} |
171 | |
} |
172 | 0 | } |
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 | 0 | getLogger().info( message ); |
181 | |
|
182 | 0 | for ( Iterator i = listeners.iterator(); i.hasNext(); ) |
183 | |
{ |
184 | 0 | ConverterListener listener = (ConverterListener) i.next(); |
185 | 0 | listener.info( message ); |
186 | |
} |
187 | 0 | } |
188 | |
|
189 | |
private void fireAddPluginEvent( Plugin plugin ) |
190 | |
{ |
191 | 0 | for ( Iterator i = listeners.iterator(); i.hasNext(); ) |
192 | |
{ |
193 | 0 | ConverterListener listener = (ConverterListener) i.next(); |
194 | 0 | listener.addPluginEvent( plugin.getGroupId(), plugin.getArtifactId() ); |
195 | |
} |
196 | 0 | } |
197 | |
|
198 | |
private void fireAddReportEvent( ReportPlugin plugin ) |
199 | |
{ |
200 | 0 | for ( Iterator i = listeners.iterator(); i.hasNext(); ) |
201 | |
{ |
202 | 0 | ConverterListener listener = (ConverterListener) i.next(); |
203 | 0 | listener.addReportEvent( plugin.getGroupId(), plugin.getArtifactId() ); |
204 | |
} |
205 | 0 | } |
206 | |
} |