View Javadoc

1   package org.apache.continuum.buildagent.build.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.buildagent.build.execution.AbstractBuildExecutor;
23  import org.apache.continuum.buildagent.build.execution.ContinuumAgentBuildCancelledException;
24  import org.apache.continuum.buildagent.build.execution.ContinuumAgentBuildExecutionResult;
25  import org.apache.continuum.buildagent.build.execution.ContinuumAgentBuildExecutor;
26  import org.apache.continuum.buildagent.build.execution.ContinuumAgentBuildExecutorException;
27  import org.apache.continuum.buildagent.installation.BuildAgentInstallationService;
28  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
29  import org.apache.maven.continuum.model.project.BuildDefinition;
30  import org.apache.maven.continuum.model.project.Project;
31  import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  import java.io.File;
35  import java.util.Enumeration;
36  import java.util.Map;
37  import java.util.Properties;
38  import javax.annotation.Resource;
39  
40  public class MavenOneBuildExecutor
41      extends AbstractBuildExecutor
42      implements ContinuumAgentBuildExecutor
43  {
44      public final static String CONFIGURATION_GOALS = "goals";
45  
46      public final static String ID = ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR;
47  
48      @Resource
49      private BuildAgentMavenOneMetadataHelper buildAgentMavenOneMetadataHelper;
50  
51      public MavenOneBuildExecutor()
52      {
53          super( ID, true );
54      }
55  
56      public ContinuumAgentBuildExecutionResult build( Project project, BuildDefinition buildDefinition, File buildOutput,
57                                                       Map<String, String> environments, String localRepository )
58          throws ContinuumAgentBuildExecutorException, ContinuumAgentBuildCancelledException
59      {
60          String executable = getBuildAgentInstallationService().getExecutorConfigurator(
61              BuildAgentInstallationService.MAVEN1_TYPE ).getExecutable();
62  
63          StringBuffer arguments = new StringBuffer();
64  
65          String buildFile = getBuildFileForProject( buildDefinition );
66  
67          if ( !StringUtils.isEmpty( buildFile ) && !"project.xml".equals( buildFile ) )
68          {
69              arguments.append( "-p " ).append( buildFile ).append( " " );
70          }
71  
72          arguments.append( StringUtils.clean( buildDefinition.getArguments() ) ).append( " " );
73  
74          Properties props = getContinuumSystemProperties( project );
75          for ( Enumeration itr = props.propertyNames(); itr.hasMoreElements(); )
76          {
77              String name = (String) itr.nextElement();
78              String value = props.getProperty( name );
79              arguments.append( "\"-D" ).append( name ).append( "=" ).append( value ).append( "\" " );
80          }
81  
82          if ( StringUtils.isNotEmpty( localRepository ) )
83          {
84              arguments.append( "\"-Dmaven.repo.local=" ).append( StringUtils.clean( localRepository ) ).append( "\" " );
85          }
86  
87          arguments.append( StringUtils.clean( buildDefinition.getGoals() ) );
88  
89          String m1Home = null;
90  
91          if ( environments != null )
92          {
93              m1Home = environments.get( getBuildAgentInstallationService().getEnvVar(
94                  BuildAgentInstallationService.MAVEN1_TYPE ) );
95          }
96  
97          if ( StringUtils.isNotEmpty( m1Home ) )
98          {
99              executable = m1Home + File.separator + "bin" + File.separator + executable;
100             setResolveExecutable( false );
101         }
102 
103         return executeShellCommand( project, executable, arguments.toString(), buildOutput, environments );
104     }
105 
106     public void updateProjectFromWorkingDirectory( File workingDirectory, Project project,
107                                                    BuildDefinition buildDefinition )
108         throws ContinuumAgentBuildExecutorException
109     {
110         File projectXmlFile = null;
111 
112         if ( buildDefinition != null )
113         {
114             String buildFile = StringUtils.clean( buildDefinition.getBuildFile() );
115 
116             if ( !StringUtils.isEmpty( buildFile ) )
117             {
118                 projectXmlFile = new File( workingDirectory, buildFile );
119             }
120         }
121 
122         if ( projectXmlFile == null )
123         {
124             projectXmlFile = new File( workingDirectory, "project.xml" );
125         }
126 
127         if ( !projectXmlFile.exists() )
128         {
129             throw new ContinuumAgentBuildExecutorException( "Could not find Maven project descriptor." );
130         }
131 
132         try
133         {
134             ContinuumProjectBuildingResult result = new ContinuumProjectBuildingResult();
135 
136             buildAgentMavenOneMetadataHelper.mapMetadata( result, projectXmlFile, project );
137 
138             if ( result.hasErrors() )
139             {
140                 throw new ContinuumAgentBuildExecutorException(
141                     "Error while mapping metadata:" + result.getErrorsAsString() );
142             }
143 
144             updateProject( project );
145         }
146         catch ( BuildAgentMavenOneMetadataHelperException e )
147         {
148             throw new ContinuumAgentBuildExecutorException( "Error while mapping metadata.", e );
149         }
150     }
151 }