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.deployment.service;
35
36 import fr.paris.lutece.plugins.deployment.business.CommandResult;
37 import fr.paris.lutece.plugins.deployment.business.MavenGoals;
38 import fr.paris.lutece.plugins.deployment.business.vcs.AbstractVCSUser;
39 import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
40 import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
41 import fr.paris.lutece.plugins.deployment.util.ReleaseUtils;
42 import fr.paris.lutece.portal.service.datastore.DatastoreService;
43 import fr.paris.lutece.portal.service.spring.SpringContextService;
44 import fr.paris.lutece.portal.service.util.AppLogService;
45 import fr.paris.lutece.portal.service.util.AppPropertiesService;
46
47 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
48 import org.apache.maven.shared.invoker.DefaultInvoker;
49 import org.apache.maven.shared.invoker.InvocationOutputHandler;
50 import org.apache.maven.shared.invoker.InvocationRequest;
51 import org.apache.maven.shared.invoker.InvocationResult;
52 import org.apache.maven.shared.invoker.Invoker;
53
54 import java.io.File;
55 import java.io.FileNotFoundException;
56 import java.io.IOException;
57 import java.io.PrintWriter;
58 import java.io.StringWriter;
59
60 import java.util.ArrayList;
61 import java.util.List;
62
63 import javax.xml.bind.JAXBException;
64
65
66
67
68
69
70 public class MavenService implements IMavenService
71 {
72
73 private Invoker _invoker;
74 private static IMavenService _singleton;
75
76 private MavenService( )
77 {
78
79 }
80
81 public static IMavenService getService( )
82 {
83
84 if ( _singleton == null )
85 {
86
87 _singleton = SpringContextService.getBean( "deployment.MavenService" );
88
89 _singleton.init( );
90 }
91 return _singleton;
92 }
93
94 public void init( )
95 {
96 _invoker = new DefaultInvoker( );
97 _invoker.setMavenHome( new File( AppPropertiesService.getProperty( ConstanteUtils.CONSTANTE_MAVEN_HOME_PATH ) ) );
98 _invoker.setLocalRepositoryDirectory( new File( AppPropertiesService.getProperty( ConstanteUtils.CONSTANTE_MAVEN_LOCAL_REPOSITORY ) ) );
99 }
100
101
102
103
104
105
106
107 public void mvnSiteAssembly( String strSiteName, String strTagName, String strMavenProfile, AbstractVCSUser user, CommandResult commandResult )
108 {
109 String strSiteLocalBasePath = DeploymentUtils.getPathCheckoutSite( strSiteName );
110
111 List<String> listGoals = MavenGoals.LUTECE_SITE_ASSEMBLY.asList( );
112 List<String> listGoalsProfile = new ArrayList<String>( );
113 listGoalsProfile.addAll( listGoals );
114 listGoalsProfile.add( "-P " + strMavenProfile );
115 listGoalsProfile.add( "-U" );
116 mvn( strTagName, strSiteLocalBasePath, listGoalsProfile, commandResult );
117 }
118
119 public void saveMvnProfilName( String strProfilValue, String strIdApplication, String strCodeEnvironment, String strCodeServerApplicationInstance )
120 {
121
122 DatastoreService.setDataValue( strIdApplication + "_" + strCodeEnvironment + "_" + strCodeServerApplicationInstance, strProfilValue );
123 }
124
125 public String getMvnProfilSaved( String strIdApplication, String strCodeEnvironment, String strCodeServerApplicationInstance )
126 {
127
128 return DatastoreService.getDataValue( strIdApplication + "_" + strCodeEnvironment + "_" + strCodeServerApplicationInstance, null );
129 }
130
131 public String getSiteWarName( String strSiteName )
132 {
133 String strWarGeneratedName = null;
134 try
135 {
136 String strSiteArtifactId = ReleaseUtils.getSiteArtifactId( DeploymentUtils.getPathCheckoutSite( strSiteName ) );
137 String strSiteVersion = ReleaseUtils.getSiteVersion( DeploymentUtils.getPathCheckoutSite( strSiteName ) );
138 strWarGeneratedName = strSiteArtifactId + "-" + strSiteVersion;
139 }
140 catch( FileNotFoundException e )
141 {
142 AppLogService.error( e );
143 }
144 catch( JAXBException e )
145 {
146
147 AppLogService.error( e );
148 }
149 return strWarGeneratedName;
150 }
151
152
153
154
155
156
157
158 private String getGoalToString( List<String> goals )
159 {
160 StringBuilder sbGoal = new StringBuilder( );
161
162 for ( String strGoal : goals )
163 {
164 sbGoal.append( strGoal ).append( ConstanteUtils.CONSTANTE_SPACE );
165 }
166
167 return sbGoal.toString( );
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 @SuppressWarnings( "unchecked" )
194 private String mvn( String strTagName, String strSitePath, List<String> goals, CommandResult commandResult )
195 {
196 InvocationRequest request = new DefaultInvocationRequest( );
197 request.setPomFile( new File( DeploymentUtils.getPathPomFile( strSitePath ) ) );
198 request.setGoals( goals );
199
200 try
201 {
202 final StringBuffer sbLog = commandResult.getLog( );
203
204
205 _invoker.setOutputHandler( new InvocationOutputHandler( )
206 {
207 public void consumeLine( String strLine )
208 {
209 sbLog.append( strLine + "\n" );
210 }
211 } );
212
213 InvocationResult invocationResult = _invoker.execute( request );
214
215 int nStatus = invocationResult.getExitCode( );
216
217 if ( nStatus != 0 )
218 {
219
220 DeploymentUtils.addTechnicalError( commandResult, commandResult.getLog( ).toString( ) );
221
222 }
223 else
224 {
225 commandResult.setStatus( CommandResult.STATUS_OK );
226 }
227
228 }
229 catch( Exception e )
230 {
231 StringWriter sw = new StringWriter( );
232 PrintWriter pw = new PrintWriter( sw );
233 e.printStackTrace( pw );
234
235 String errorLog = sw.toString( );
236 pw.flush( );
237 pw.close( );
238
239 try
240 {
241 sw.flush( );
242 sw.close( );
243 }
244 catch( IOException e1 )
245 {
246
247 AppLogService.error( e1 );
248 }
249
250 commandResult.getLog( ).append( errorLog );
251
252 DeploymentUtils.addTechnicalError( commandResult, errorLog );
253
254 }
255
256
257 return null;
258 }
259
260 }