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