Coverage Report - fr.paris.lutece.plugins.releaser.service.MavenService
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenService
4 %
2/43
0 %
0/10
1,875
MavenService$1
0 %
0/3
N/A
1,875
 
 1  
 /*
 2  
  * Copyright (c) 2002-2013, Mairie de Paris
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  *
 9  
  *  1. Redistributions of source code must retain the above copyright notice
 10  
  *     and the following disclaimer.
 11  
  *
 12  
  *  2. Redistributions in binary form must reproduce the above copyright notice
 13  
  *     and the following disclaimer in the documentation and/or other materials
 14  
  *     provided with the distribution.
 15  
  *
 16  
  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 17  
  *     contributors may be used to endorse or promote products derived from
 18  
  *     this software without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  *
 32  
  * License 1.0
 33  
  */
 34  
 package fr.paris.lutece.plugins.releaser.service;
 35  
 
 36  
 import java.io.File;
 37  
 import java.util.ArrayList;
 38  
 import java.util.List;
 39  
 
 40  
 import org.apache.commons.lang.StringUtils;
 41  
 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
 42  
 import org.apache.maven.shared.invoker.DefaultInvoker;
 43  
 import org.apache.maven.shared.invoker.InvocationOutputHandler;
 44  
 import org.apache.maven.shared.invoker.InvocationRequest;
 45  
 import org.apache.maven.shared.invoker.InvocationResult;
 46  
 import org.apache.maven.shared.invoker.Invoker;
 47  
 
 48  
 import fr.paris.lutece.plugins.releaser.util.CommandResult;
 49  
 import fr.paris.lutece.plugins.releaser.util.ConstanteUtils;
 50  
 import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
 51  
 import fr.paris.lutece.plugins.releaser.util.maven.MavenGoals;
 52  
 import fr.paris.lutece.plugins.releaser.util.maven.MavenUtils;
 53  
 import fr.paris.lutece.plugins.releaser.util.svn.SvnUser;
 54  
 import fr.paris.lutece.portal.service.spring.SpringContextService;
 55  
 import fr.paris.lutece.portal.service.util.AppPropertiesService;
 56  
 
 57  
 /**
 58  
  * 
 59  
  * MavenService : provides maven command launcher
 60  
  * 
 61  
  */
 62  
 public class MavenService implements IMavenService
 63  
 {
 64  
     // private static IMavenService _singleton;
 65  
     private Invoker _invoker;
 66  
     private static IMavenService _instance;
 67  
     
 68  
 
 69  
     private MavenService( )
 70  1
     {
 71  
        
 72  1
     }
 73  
     
 74  
     public static IMavenService getService()
 75  
     {
 76  0
         if(_instance==null)
 77  
         {
 78  0
             _instance=SpringContextService.getBean(ConstanteUtils.BEAN_MAVEN_SERVICE );
 79  0
             _instance.init( );
 80  
         }
 81  
             
 82  0
       return _instance;
 83  
         
 84  
     }
 85  
 
 86  
     public void init( )
 87  
     {
 88  0
         _invoker = new DefaultInvoker( );
 89  0
         _invoker.setMavenHome( new File( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_MAVEN_HOME_PATH ) ) );
 90  0
         _invoker.setLocalRepositoryDirectory( new File( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_MAVEN_LOCAL_REPOSITORY ) ) );
 91  
         
 92  0
     }
 93  
 
 94  
     /*
 95  
      * (non-Javadoc)
 96  
      * 
 97  
      * @see fr.paris.lutece.plugins.deployment.service.IMavenService#mvnSiteAssembly(java.lang.String, fr.paris.lutece.plugins.deployment.business.Environment,
 98  
      * fr.paris.lutece.plugins.deployment.business.MavenUser)
 99  
      */
 100  
     public void mvnSiteAssembly( String strSiteName, String strTagName, String strMavenProfile, SvnUser user, CommandResult commandResult )
 101  
     {
 102  0
         String strSiteLocalBasePath = ReleaserUtils.getLocalSitePath( strSiteName );
 103  
 
 104  0
         List<String> listGoals = MavenGoals.LUTECE_SITE_ASSEMBLY.asList( );
 105  0
         List<String> listGoalsProfile = new ArrayList<String>( );
 106  0
         listGoalsProfile.addAll( listGoals );
 107  0
         listGoalsProfile.add( "-P " + strMavenProfile );
 108  0
         listGoalsProfile.add( "-U" );
 109  
         // mvnExecute( strTagName, strSiteLocalBasePath, listGoalsProfile, commandResult );
 110  0
     }
 111  
 
 112  
     /**
 113  
      * Transforme la liste en chaine, pour passer l'argument � la ligne de commande
 114  
      * 
 115  
      * @param goals
 116  
      * @return
 117  
      */
 118  
 
 119  
 
 120  
     // public static IMavenService getInstance()
 121  
     // {
 122  
     //
 123  
     // if(_singleton ==null)
 124  
     // {
 125  
     // _singleton=new MavenService();
 126  
     // }
 127  
     //
 128  
     // return _singleton;
 129  
     //
 130  
     // }
 131  
     //
 132  
 
 133  
     /**
 134  
      * Launches mvn cmd
 135  
      * 
 136  
      * @param strPluginName
 137  
      *            plugin name (ex: plugin-ods)
 138  
      * @param goals
 139  
      *            maven goals
 140  
      * @param strSVNBinPath
 141  
      *            svn bin path (ex: /home/svn/apps/subversion/bin)
 142  
      */
 143  
    
 144  
     private InvocationResult mvnExecute( String strPathPom, List<String> goals, CommandResult commandResult )
 145  
     {
 146  0
         InvocationRequest request = new DefaultInvocationRequest( );
 147  0
         request.setPomFile( new File( strPathPom ) );
 148  0
         request.setGoals( goals );
 149  0
         String strProxyHost=AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_PROXY_HOST );
 150  0
         String strProxyPort=AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_PROXY_PORT );
 151  
          
 152  0
         if(!StringUtils.isEmpty( strProxyHost ) && !StringUtils.isEmpty( strProxyPort ))
 153  
         {
 154  0
             request.setMavenOpts( "-Dhttps.proxyHost="+strProxyHost+"  -Dhttps.proxyPort="+strProxyPort+" -Dhttp.proxyHost="+strProxyHost+"  -Dhttp.proxyPort="+strProxyPort);
 155  
         }
 156  0
         InvocationResult invocationResult = null;
 157  
         try
 158  
         {
 159  0
             final StringBuffer sbLog = commandResult.getLog( );
 160  
 
 161  
             // logger
 162  0
             _invoker.setOutputHandler( new InvocationOutputHandler( )
 163  0
             {
 164  
                 public void consumeLine( String strLine )
 165  
                 {
 166  0
                     sbLog.append( strLine + "\n" );
 167  0
                 }
 168  
             } );
 169  
 
 170  0
             invocationResult = _invoker.execute( request );
 171  
 
 172  0
             return invocationResult;
 173  
 
 174  
         }
 175  0
         catch( Exception e )
 176  
         {
 177  
 
 178  0
             ReleaserUtils.addTechnicalError( commandResult, commandResult.getLog( ).toString( ), e );
 179  
         }
 180  
 
 181  0
         return invocationResult;
 182  
     }
 183  
 
 184  
     public String mvnReleasePerform( String strPathPom,String strUsername, String strPassword, CommandResult commandResult )
 185  
     {
 186  0
         InvocationResult invocationResult = mvnExecute( strPathPom, MavenGoals.RELEASE_PERFORM.asList( ), commandResult );
 187  0
         int nStatus = invocationResult.getExitCode( );
 188  
 
 189  0
         if ( nStatus != 0 )
 190  
         {
 191  0
             ReleaserUtils.addTechnicalError( commandResult, "Error during Release Perform exit code is: " + nStatus );
 192  
         }
 193  
         
 194  0
         return "";
 195  
     }
 196  
 
 197  
     /**
 198  
      * 
 199  
      * mvnReleasePrepare
 200  
      * 
 201  
      * @param strBasePath
 202  
      *            chemin sur le disque pour l'acces au composant
 203  
      * @param strPluginName
 204  
      *            le nom du composant
 205  
      * @param strReleaseVersion
 206  
      *            la version a release
 207  
      * @param strTag
 208  
      *            le nom du tag
 209  
      * @param strDevelopmentVersion
 210  
      *            la prochaine version de developpement (avec -SNAPSHOT)
 211  
      * @return le thread
 212  
      */
 213  
     public String mvnReleasePrepare( String strPathPom, String strReleaseVersion, String strTag, String strDevelopmentVersion, String strUsername,
 214  
             String strPassword, CommandResult commandResult )
 215  
     {
 216  0
         List<String> listGoals = MavenUtils.createReleasePrepare( strReleaseVersion, strTag, strDevelopmentVersion, strUsername, strPassword );
 217  
 
 218  0
         InvocationResult invocationResult = mvnExecute( strPathPom, listGoals, commandResult );
 219  
 
 220  0
         int nStatus = invocationResult.getExitCode( );
 221  
 
 222  0
         if ( nStatus != 0 )
 223  
         {
 224  0
             ReleaserUtils.addTechnicalError( commandResult, "Error during Release Prepare exit code is: " + nStatus );
 225  
         }
 226  
         
 227  0
         return "";
 228  
     }
 229  
 
 230  
 }