1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.releaser.util.pom;
35
36 import java.io.FileInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.FileOutputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.OutputStream;
42 import java.util.Iterator;
43
44 import javax.xml.bind.JAXBContext;
45 import javax.xml.bind.JAXBElement;
46 import javax.xml.bind.JAXBException;
47 import javax.xml.bind.Marshaller;
48 import javax.xml.bind.Unmarshaller;
49
50 import fr.paris.lutece.plugins.releaser.business.Component;
51 import fr.paris.lutece.plugins.releaser.business.Site;
52 import fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model;
53 import fr.paris.lutece.plugins.releaser.business.jaxb.maven.ObjectFactory;
54 import fr.paris.lutece.plugins.releaser.util.ConstanteUtils;
55 import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
56 import fr.paris.lutece.portal.service.i18n.I18nService;
57 import fr.paris.lutece.portal.service.util.AppLogService;
58 import fr.paris.lutece.portal.service.util.AppPropertiesService;
59
60
61
62
63
64 public class PomUpdater
65 {
66
67
68
69
70
71
72
73
74
75
76
77
78 public static String updateSiteBeforeTag( Site site, String strSiteLocalPomPath ) throws JAXBException
79 {
80
81 InputStream inputStream = null;
82 OutputStream outputStream = null;
83
84 try
85 {
86
87 inputStream = new FileInputStream( strSiteLocalPomPath );
88 Model model = unmarshal( Model.class, inputStream );
89
90 model.setDescription( site.getTagInformation( ) );
91
92 fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( );
93
94 if ( dependencies != null )
95 {
96 for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) )
97 {
98 for ( Component component : site.getComponents( ) )
99 {
100
101 if ( jaxDependency.getArtifactId( ).equals( component.getArtifactId( ) ) )
102 {
103
104 jaxDependency.setVersion( "[" + component.getTargetVersion( ) + "]" );
105 }
106 }
107
108 }
109 }
110
111 String strParentSiteVersion = model.getParent( ).getVersion( );
112 String strPomParentReferenceVersion = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_POM_PARENT_SITE_VERSION );
113
114 if ( ReleaserUtils.compareVersion( strParentSiteVersion, strPomParentReferenceVersion ) < 0 )
115 {
116
117 String strPomParentArtifactId = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_POM_PARENT_ARTIFCAT_ID );
118 String strPomParentGroupId = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_POM_PARENT_GROUP_ID );
119 model.getParent( ).setArtifactId( strPomParentArtifactId );
120 model.getParent( ).setGroupId( strPomParentGroupId );
121 model.getParent( ).setVersion( strPomParentReferenceVersion );
122 }
123
124 outputStream = new FileOutputStream( strSiteLocalPomPath );
125
126 save( model, outputStream );
127
128 }
129 catch( FileNotFoundException e )
130 {
131 AppLogService.error( e );
132 }
133 finally
134 {
135 if ( outputStream != null )
136 {
137 try
138 {
139 outputStream.close( );
140 }
141 catch( IOException ex )
142 {
143
144 AppLogService.error( ex );
145 }
146 }
147 }
148
149 return "";
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163 public static String updateSiteAfterTag( Site site, String strSiteLocalPomPath ) throws JAXBException
164 {
165
166 InputStream inputStream = null;
167 OutputStream outputStream = null;
168
169 try
170 {
171
172 inputStream = new FileInputStream( strSiteLocalPomPath );
173 Model model = unmarshal( Model.class, inputStream );
174
175 model.setVersion( site.getNextSnapshotVersion( ) );
176 model.setDescription( "" );
177
178 fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( );
179
180 if ( dependencies != null )
181 {
182 for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) )
183 {
184 for ( Component component : site.getComponents( ) )
185 {
186
187 if ( jaxDependency.getArtifactId( ).equals( component.getArtifactId( ) ) )
188 {
189
190 if ( component.isProject( ) && component.isSnapshotVersion( ) )
191 {
192 jaxDependency.setVersion( component.getNextSnapshotVersion( ) );
193 }
194
195 }
196 }
197
198 }
199 }
200
201 outputStream = new FileOutputStream( strSiteLocalPomPath );
202
203 save( model, outputStream );
204
205 }
206 catch( FileNotFoundException e )
207 {
208 AppLogService.error( e );
209 }
210 finally
211 {
212 if ( outputStream != null )
213 {
214 try
215 {
216 outputStream.close( );
217 }
218 catch( IOException ex )
219 {
220
221 AppLogService.error( ex );
222 }
223 }
224 }
225
226 return "";
227 }
228
229
230
231
232
233
234
235
236
237
238
239 public static void save( Model model, OutputStream outputStream ) throws JAXBException
240 {
241 String packageName = model.getClass( ).getPackage( ).getName( );
242 ObjectFactory factory = new ObjectFactory( );
243 JAXBElement<Model> element = factory.createProject( model );
244
245 JAXBContext jc = JAXBContext.newInstance( packageName );
246 Marshaller m = jc.createMarshaller( );
247 m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
248 m.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, "http://maven.apache.org/maven-v4_0_0.xsd" );
249 m.marshal( element, outputStream );
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 public static <T> T unmarshal( Class<T> docClass, InputStream inputStream ) throws JAXBException
266 {
267 String packageName = docClass.getPackage( ).getName( );
268 JAXBContext jc = JAXBContext.newInstance( packageName );
269 Unmarshaller u = jc.createUnmarshaller( );
270 JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputStream );
271 return doc.getValue( );
272 }
273
274 }