View Javadoc

1   package org.apache.maven.continuum.execution.ant;
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.maven.continuum.execution.AbstractBuildExecutor;
23  import org.apache.maven.continuum.execution.ContinuumBuildExecutionResult;
24  import org.apache.maven.continuum.execution.ContinuumBuildExecutor;
25  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
26  import org.apache.maven.continuum.execution.ContinuumBuildExecutorException;
27  import org.apache.maven.continuum.installation.InstallationService;
28  import org.apache.maven.continuum.model.project.BuildDefinition;
29  import org.apache.maven.continuum.model.project.Project;
30  import org.apache.maven.continuum.model.scm.ScmResult;
31  import org.apache.maven.continuum.model.system.Installation;
32  import org.apache.maven.continuum.model.system.Profile;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import java.io.File;
36  import java.util.Collections;
37  import java.util.Enumeration;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Properties;
42  
43  /**
44   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
45   * @version $Id: AntBuildExecutor.java 1372260 2012-08-13 04:29:09Z brett $
46   */
47  public class AntBuildExecutor
48      extends AbstractBuildExecutor
49      implements ContinuumBuildExecutor
50  {
51      // ----------------------------------------------------------------------
52      //
53      // ----------------------------------------------------------------------
54  
55      public static final String CONFIGURATION_EXECUTABLE = "executable";
56  
57      public static final String CONFIGURATION_TARGETS = "targets";
58  
59      public static final String ID = ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR;
60  
61      // ----------------------------------------------------------------------
62      //
63      // ----------------------------------------------------------------------
64  
65      public AntBuildExecutor()
66      {
67          super( ID, true );
68      }
69  
70      // ----------------------------------------------------------------------
71      // ContinuumBuilder Implementation
72      // ----------------------------------------------------------------------
73  
74      public ContinuumBuildExecutionResult build( Project project, BuildDefinition buildDefinition, File buildOutput,
75                                                  List<Project> projectsWithCommonScmRoot, String projectScmRootUrl )
76          throws ContinuumBuildExecutorException
77      {
78          String executable = getInstallationService().getExecutorConfigurator(
79              InstallationService.ANT_TYPE ).getExecutable();
80  
81          StringBuffer arguments = new StringBuffer();
82  
83          String buildFile = getBuildFileForProject( project, buildDefinition );
84  
85          if ( !StringUtils.isEmpty( buildFile ) )
86          {
87              arguments.append( "-f " ).append( buildFile ).append( " " );
88          }
89  
90          arguments.append( StringUtils.clean( buildDefinition.getArguments() ) ).append( " " );
91  
92          Properties props = getContinuumSystemProperties( project );
93          for ( Enumeration itr = props.propertyNames(); itr.hasMoreElements(); )
94          {
95              String name = (String) itr.nextElement();
96              String value = props.getProperty( name );
97              arguments.append( "\"-D" ).append( name ).append( "=" ).append( value ).append( "\" " );
98          }
99  
100         arguments.append( StringUtils.clean( buildDefinition.getGoals() ) );
101 
102         Map<String, String> environments = getEnvironments( buildDefinition );
103         String antHome = environments.get( getInstallationService().getEnvVar( InstallationService.ANT_TYPE ) );
104         if ( StringUtils.isNotEmpty( antHome ) )
105         {
106             executable = antHome + File.separator + "bin" + File.separator + executable;
107             setResolveExecutable( false );
108         }
109 
110         return executeShellCommand( project, executable, arguments.toString(), buildOutput, environments, null, null );
111     }
112 
113     protected Map<String, String> getEnvironments( BuildDefinition buildDefinition )
114     {
115         Profile profile = buildDefinition.getProfile();
116         if ( profile == null )
117         {
118             return Collections.EMPTY_MAP;
119         }
120         Map<String, String> envVars = new HashMap<String, String>();
121         String javaHome = getJavaHomeValue( buildDefinition );
122         if ( !StringUtils.isEmpty( javaHome ) )
123         {
124             envVars.put( getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), javaHome );
125         }
126         Installation builder = profile.getBuilder();
127         if ( builder != null )
128         {
129             envVars.put( getInstallationService().getEnvVar( InstallationService.ANT_TYPE ), builder.getVarValue() );
130         }
131         envVars.putAll( getEnvironmentVariables( buildDefinition ) );
132         return envVars;
133 
134     }
135 
136     public void updateProjectFromCheckOut( File workingDirectory, Project p, BuildDefinition buildDefinition,
137                                            ScmResult scmResult )
138         throws ContinuumBuildExecutorException
139     {
140     }
141 }