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