1 | |
package fr.paris.lutece.plugins.releaser.util.pom; |
2 | |
|
3 | |
import java.io.FileInputStream; |
4 | |
import java.io.FileNotFoundException; |
5 | |
import java.io.FileOutputStream; |
6 | |
import java.io.IOException; |
7 | |
import java.io.InputStream; |
8 | |
import java.io.OutputStream; |
9 | |
import java.util.Iterator; |
10 | |
|
11 | |
import javax.xml.bind.JAXBContext; |
12 | |
import javax.xml.bind.JAXBElement; |
13 | |
import javax.xml.bind.JAXBException; |
14 | |
import javax.xml.bind.Marshaller; |
15 | |
import javax.xml.bind.Unmarshaller; |
16 | |
|
17 | |
import fr.paris.lutece.plugins.releaser.business.Component; |
18 | |
import fr.paris.lutece.plugins.releaser.business.Site; |
19 | |
import fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model; |
20 | |
import fr.paris.lutece.plugins.releaser.business.jaxb.maven.ObjectFactory; |
21 | |
import fr.paris.lutece.plugins.releaser.util.ReleaserUtils; |
22 | |
import fr.paris.lutece.portal.service.util.AppLogService; |
23 | |
|
24 | 0 | public class PomUpdater |
25 | |
{ |
26 | |
|
27 | |
public static String updateSiteBeforeTag( Site site ) throws JAXBException |
28 | |
{ |
29 | |
|
30 | 0 | InputStream inputStream = null; |
31 | 0 | OutputStream outputStream = null; |
32 | 0 | String strSiteLocalPomPath = ReleaserUtils.getLocalSitePomPath( site.getArtifactId( ) ); |
33 | |
|
34 | |
try |
35 | |
{ |
36 | |
|
37 | 0 | inputStream = new FileInputStream( strSiteLocalPomPath ); |
38 | 0 | Model model = unmarshal( Model.class, inputStream ); |
39 | 0 | if(!site.isTheme( )) |
40 | |
{ |
41 | 0 | model.setVersion( site.getNextReleaseVersion( ) ); |
42 | |
} |
43 | |
|
44 | 0 | model.setDescription( site.getTagInformation( ) ); |
45 | |
|
46 | 0 | fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( ); |
47 | |
|
48 | 0 | if ( dependencies != null ) |
49 | |
{ |
50 | 0 | for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) ) |
51 | |
{ |
52 | 0 | for ( Component component : site.getComponents( ) ) |
53 | |
{ |
54 | |
|
55 | 0 | if ( jaxDependency.getArtifactId( ).equals( component.getArtifactId( ) ) ) |
56 | |
{ |
57 | |
|
58 | 0 | jaxDependency.setVersion( component.getTargetVersion( ) ); |
59 | |
|
60 | |
} |
61 | 0 | } |
62 | |
|
63 | 0 | } |
64 | |
} |
65 | |
|
66 | 0 | outputStream = new FileOutputStream( strSiteLocalPomPath ); |
67 | |
|
68 | 0 | save( model, outputStream ); |
69 | |
|
70 | |
} |
71 | 0 | catch( FileNotFoundException e ) |
72 | |
{ |
73 | 0 | AppLogService.error( e ); |
74 | |
} |
75 | |
finally |
76 | |
{ |
77 | 0 | if ( outputStream != null ) |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | outputStream.close( ); |
82 | |
} |
83 | 0 | catch( IOException ex ) |
84 | |
{ |
85 | |
|
86 | 0 | AppLogService.error( ex ); |
87 | 0 | } |
88 | |
} |
89 | |
} |
90 | |
|
91 | 0 | return ""; |
92 | |
} |
93 | |
|
94 | |
public static String updateSiteAfterTag( Site site ) throws JAXBException |
95 | |
{ |
96 | |
|
97 | 0 | InputStream inputStream = null; |
98 | 0 | OutputStream outputStream = null; |
99 | 0 | String strSiteLocalPomPath = ReleaserUtils.getLocalSitePomPath( site.getArtifactId( ) ); |
100 | |
|
101 | |
try |
102 | |
{ |
103 | |
|
104 | 0 | inputStream = new FileInputStream( strSiteLocalPomPath ); |
105 | 0 | Model model = unmarshal( Model.class, inputStream ); |
106 | |
|
107 | 0 | model.setVersion( site.getNextSnapshotVersion( ) ); |
108 | 0 | model.setDescription( ""); |
109 | |
|
110 | 0 | fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( ); |
111 | |
|
112 | 0 | if ( dependencies != null ) |
113 | |
{ |
114 | 0 | for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) ) |
115 | |
{ |
116 | 0 | for ( Component component : site.getComponents( ) ) |
117 | |
{ |
118 | |
|
119 | 0 | if ( jaxDependency.getArtifactId( ).equals( component.getArtifactId( ) ) ) |
120 | |
{ |
121 | |
|
122 | 0 | if(component.isProject( ) && component.isSnapshotVersion( )) |
123 | |
{ |
124 | 0 | jaxDependency.setVersion( component.getNextSnapshotVersion( ) ); |
125 | |
} |
126 | |
|
127 | |
} |
128 | 0 | } |
129 | |
|
130 | 0 | } |
131 | |
} |
132 | |
|
133 | 0 | outputStream = new FileOutputStream( strSiteLocalPomPath ); |
134 | |
|
135 | 0 | save( model, outputStream ); |
136 | |
|
137 | |
} |
138 | 0 | catch( FileNotFoundException e ) |
139 | |
{ |
140 | 0 | AppLogService.error( e ); |
141 | |
} |
142 | |
finally |
143 | |
{ |
144 | 0 | if ( outputStream != null ) |
145 | |
{ |
146 | |
try |
147 | |
{ |
148 | 0 | outputStream.close( ); |
149 | |
} |
150 | 0 | catch( IOException ex ) |
151 | |
{ |
152 | |
|
153 | 0 | AppLogService.error( ex ); |
154 | 0 | } |
155 | |
} |
156 | |
} |
157 | |
|
158 | 0 | return ""; |
159 | |
} |
160 | |
public static void save( Model model, OutputStream outputStream ) throws JAXBException |
161 | |
{ |
162 | 0 | String packageName = model.getClass( ).getPackage( ).getName( ); |
163 | 0 | ObjectFactory factory = new ObjectFactory( ); |
164 | 0 | JAXBElement<Model> element = factory.createProject( model ); |
165 | |
|
166 | 0 | JAXBContext jc = JAXBContext.newInstance( packageName ); |
167 | 0 | Marshaller m = jc.createMarshaller( ); |
168 | 0 | m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); |
169 | 0 | m.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, "http://maven.apache.org/maven-v4_0_0.xsd" ); |
170 | 0 | m.marshal( element, outputStream ); |
171 | 0 | } |
172 | |
|
173 | |
public static <T> T unmarshal( Class<T> docClass, InputStream inputStream ) throws JAXBException |
174 | |
{ |
175 | 1 | String packageName = docClass.getPackage( ).getName( ); |
176 | 1 | JAXBContext jc = JAXBContext.newInstance( packageName ); |
177 | 1 | Unmarshaller u = jc.createUnmarshaller( ); |
178 | 1 | JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputStream ); |
179 | 1 | return doc.getValue( ); |
180 | |
} |
181 | |
|
182 | |
} |