View Javadoc

1   package org.apache.maven.continuum.execution.maven.m1;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.continuum.model.repository.LocalRepository;
23  import org.apache.maven.continuum.execution.AbstractBuildExecutor;
24  import org.apache.maven.continuum.execution.ContinuumBuildExecutionResult;
25  import org.apache.maven.continuum.execution.ContinuumBuildExecutor;
26  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
27  import org.apache.maven.continuum.execution.ContinuumBuildExecutorException;
28  import org.apache.maven.continuum.installation.InstallationService;
29  import org.apache.maven.continuum.model.project.BuildDefinition;
30  import org.apache.maven.continuum.model.project.Project;
31  import org.apache.maven.continuum.model.scm.ScmResult;
32  import org.apache.maven.continuum.model.system.Installation;
33  import org.apache.maven.continuum.model.system.Profile;
34  import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  import java.io.File;
38  import java.util.Collections;
39  import java.util.Enumeration;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  /**
46   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
47   * @version $Id: MavenOneBuildExecutor.java 1372260 2012-08-13 04:29:09Z brett $
48   */
49  public class MavenOneBuildExecutor
50      extends AbstractBuildExecutor
51      implements ContinuumBuildExecutor
52  {
53      public final static String CONFIGURATION_GOALS = "goals";
54  
55      public final static String ID = ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR;
56  
57      /**
58       * @plexus.requirement
59       */
60      private MavenOneMetadataHelper metadataHelper;
61  
62      // ----------------------------------------------------------------------
63      //
64      // ----------------------------------------------------------------------
65  
66      public MavenOneBuildExecutor()
67      {
68          super( ID, true );
69      }
70  
71      public MavenOneMetadataHelper getMetadataHelper()
72      {
73          return metadataHelper;
74      }
75  
76      public void setMetadataHelper( MavenOneMetadataHelper metadataHelper )
77      {
78          this.metadataHelper = metadataHelper;
79      }
80  
81      // ----------------------------------------------------------------------
82      // Builder Implementation
83      // ----------------------------------------------------------------------
84  
85      public ContinuumBuildExecutionResult build( Project project, BuildDefinition buildDefinition, File buildOutput,
86                                                  List<Project> projectsWithCommonScmRoot, String projectScmRootUrl )
87          throws ContinuumBuildExecutorException
88      {
89          String executable = getInstallationService().getExecutorConfigurator(
90              InstallationService.MAVEN1_TYPE ).getExecutable();
91  
92          StringBuffer arguments = new StringBuffer();
93  
94          String buildFile = getBuildFileForProject( project, buildDefinition );
95  
96          if ( !StringUtils.isEmpty( buildFile ) && !"project.xml".equals( buildFile ) )
97          {
98              arguments.append( "-p " ).append( buildFile ).append( " " );
99          }
100 
101         arguments.append( StringUtils.clean( buildDefinition.getArguments() ) ).append( " " );
102 
103         Properties props = getContinuumSystemProperties( project );
104         for ( Enumeration itr = props.propertyNames(); itr.hasMoreElements(); )
105         {
106             String name = (String) itr.nextElement();
107             String value = props.getProperty( name );
108             arguments.append( "\"-D" ).append( name ).append( "=" ).append( value ).append( "\" " );
109         }
110 
111         // append -Dmaven.repo.local if project group has a local repository
112         LocalRepository repository = project.getProjectGroup().getLocalRepository();
113         if ( repository != null )
114         {
115             arguments.append( "\"-Dmaven.repo.local=" ).append( StringUtils.clean( repository.getLocation() ) ).append(
116                 "\" " );
117         }
118 
119         arguments.append( StringUtils.clean( buildDefinition.getGoals() ) );
120 
121         Map<String, String> environments = getEnvironments( buildDefinition );
122         String m1Home = environments.get( getInstallationService().getEnvVar( InstallationService.MAVEN1_TYPE ) );
123         if ( StringUtils.isNotEmpty( m1Home ) )
124         {
125             executable = m1Home + File.separator + "bin" + File.separator + executable;
126             setResolveExecutable( false );
127         }
128 
129         return executeShellCommand( project, executable, arguments.toString(), buildOutput, environments, null, null );
130     }
131 
132     protected Map<String, String> getEnvironments( BuildDefinition buildDefinition )
133     {
134         Profile profile = buildDefinition.getProfile();
135         if ( profile == null )
136         {
137             return Collections.EMPTY_MAP;
138         }
139         Map<String, String> envVars = new HashMap<String, String>();
140         String javaHome = getJavaHomeValue( buildDefinition );
141         if ( !StringUtils.isEmpty( javaHome ) )
142         {
143             envVars.put( getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), javaHome );
144         }
145         Installation builder = profile.getBuilder();
146         if ( builder != null )
147         {
148             envVars.put( getInstallationService().getEnvVar( InstallationService.MAVEN1_TYPE ), builder.getVarValue() );
149         }
150         envVars.putAll( getEnvironmentVariables( buildDefinition ) );
151         return envVars;
152 
153     }
154 
155     public void updateProjectFromCheckOut( File workingDirectory, Project project, BuildDefinition buildDefinition,
156                                            ScmResult scmResult )
157         throws ContinuumBuildExecutorException
158     {
159         File projectXmlFile = null;
160 
161         if ( buildDefinition != null )
162         {
163             String buildFile = StringUtils.clean( buildDefinition.getBuildFile() );
164 
165             if ( !StringUtils.isEmpty( buildFile ) )
166             {
167                 projectXmlFile = new File( workingDirectory, buildFile );
168             }
169         }
170 
171         if ( projectXmlFile == null )
172         {
173             projectXmlFile = new File( workingDirectory, "project.xml" );
174         }
175 
176         if ( !projectXmlFile.exists() )
177         {
178             throw new ContinuumBuildExecutorException( "Could not find Maven project descriptor." );
179         }
180 
181         try
182         {
183             boolean update = isDescriptionUpdated( buildDefinition, scmResult, project );
184             metadataHelper.mapMetadata( new ContinuumProjectBuildingResult(), projectXmlFile, project, update );
185         }
186         catch ( MavenOneMetadataHelperException e )
187         {
188             throw new ContinuumBuildExecutorException( "Error while mapping metadata.", e );
189         }
190     }
191 }