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 java.io.File; |
23 | import java.util.Collections; |
24 | import java.util.Enumeration; |
25 | import java.util.HashMap; |
26 | import java.util.Map; |
27 | import java.util.Properties; |
28 | |
29 | import org.apache.continuum.model.repository.LocalRepository; |
30 | import org.apache.maven.continuum.execution.AbstractBuildExecutor; |
31 | import org.apache.maven.continuum.execution.ContinuumBuildExecutionResult; |
32 | import org.apache.maven.continuum.execution.ContinuumBuildExecutor; |
33 | import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants; |
34 | import org.apache.maven.continuum.execution.ContinuumBuildExecutorException; |
35 | import org.apache.maven.continuum.installation.InstallationService; |
36 | import org.apache.maven.continuum.model.project.BuildDefinition; |
37 | import org.apache.maven.continuum.model.project.Project; |
38 | import org.apache.maven.continuum.model.system.Installation; |
39 | import org.apache.maven.continuum.model.system.Profile; |
40 | import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult; |
41 | import org.codehaus.plexus.util.StringUtils; |
42 | |
43 | /** |
44 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> |
45 | * @version $Id: MavenOneBuildExecutor.java 764863 2009-04-14 16:28:12Z evenisse $ |
46 | */ |
47 | public class MavenOneBuildExecutor |
48 | extends AbstractBuildExecutor |
49 | implements ContinuumBuildExecutor |
50 | { |
51 | public final static String CONFIGURATION_GOALS = "goals"; |
52 | |
53 | public final static String ID = ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR; |
54 | |
55 | /** |
56 | * @plexus.requirement |
57 | */ |
58 | private MavenOneMetadataHelper metadataHelper; |
59 | |
60 | // ---------------------------------------------------------------------- |
61 | // |
62 | // ---------------------------------------------------------------------- |
63 | |
64 | public MavenOneBuildExecutor() |
65 | { |
66 | super( ID, true ); |
67 | } |
68 | |
69 | public MavenOneMetadataHelper getMetadataHelper() |
70 | { |
71 | return metadataHelper; |
72 | } |
73 | |
74 | public void setMetadataHelper( MavenOneMetadataHelper metadataHelper ) |
75 | { |
76 | this.metadataHelper = metadataHelper; |
77 | } |
78 | |
79 | // ---------------------------------------------------------------------- |
80 | // Builder Implementation |
81 | // ---------------------------------------------------------------------- |
82 | |
83 | public ContinuumBuildExecutionResult build( Project project, BuildDefinition buildDefinition, File buildOutput ) |
84 | throws ContinuumBuildExecutorException |
85 | { |
86 | String executable = |
87 | getInstallationService().getExecutorConfigurator( InstallationService.MAVEN1_TYPE ).getExecutable(); |
88 | |
89 | StringBuffer arguments = new StringBuffer(); |
90 | |
91 | String buildFile = getBuildFileForProject( project, buildDefinition ); |
92 | |
93 | if ( !StringUtils.isEmpty( buildFile ) && !"project.xml".equals( buildFile ) ) |
94 | { |
95 | arguments.append( "-p " ).append( buildFile ).append( " " ); |
96 | } |
97 | |
98 | arguments.append( StringUtils.clean( buildDefinition.getArguments() ) ).append( " " ); |
99 | |
100 | Properties props = getContinuumSystemProperties( project ); |
101 | for ( Enumeration itr = props.propertyNames(); itr.hasMoreElements(); ) |
102 | { |
103 | String name = (String) itr.nextElement(); |
104 | String value = props.getProperty( name ); |
105 | arguments.append( "\"-D" ).append( name ).append( "=" ).append( value ).append( "\" " ); |
106 | } |
107 | |
108 | // append -Dmaven.repo.local if project group has a local repository |
109 | LocalRepository repository = project.getProjectGroup().getLocalRepository(); |
110 | if ( repository != null ) |
111 | { |
112 | arguments.append( "\"-Dmaven.repo.local=" ).append( StringUtils.clean( repository.getLocation() ) ).append( |
113 | "\" " ); |
114 | } |
115 | |
116 | arguments.append( StringUtils.clean( buildDefinition.getGoals() ) ); |
117 | |
118 | Map<String, String> environments = getEnvironments( buildDefinition ); |
119 | String m1Home = environments.get( getInstallationService().getEnvVar( InstallationService.MAVEN1_TYPE ) ); |
120 | if ( StringUtils.isNotEmpty( m1Home ) ) |
121 | { |
122 | executable = m1Home + File.separator + "bin" + File.separator + executable; |
123 | setResolveExecutable( false ); |
124 | } |
125 | |
126 | return executeShellCommand( project, executable, arguments.toString(), buildOutput, environments ); |
127 | } |
128 | |
129 | protected Map<String, String> getEnvironments( BuildDefinition buildDefinition ) |
130 | { |
131 | Profile profile = buildDefinition.getProfile(); |
132 | if ( profile == null ) |
133 | { |
134 | return Collections.EMPTY_MAP; |
135 | } |
136 | Map<String, String> envVars = new HashMap<String, String>(); |
137 | String javaHome = getJavaHomeValue( buildDefinition ); |
138 | if ( !StringUtils.isEmpty( javaHome ) ) |
139 | { |
140 | envVars.put( getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), javaHome ); |
141 | } |
142 | Installation builder = profile.getBuilder(); |
143 | if ( builder != null ) |
144 | { |
145 | envVars.put( getInstallationService().getEnvVar( InstallationService.MAVEN1_TYPE ), builder.getVarValue() ); |
146 | } |
147 | envVars.putAll( getEnvironmentVariables( buildDefinition ) ); |
148 | return envVars; |
149 | |
150 | } |
151 | |
152 | public void updateProjectFromCheckOut( File workingDirectory, Project project, BuildDefinition buildDefinition ) |
153 | throws ContinuumBuildExecutorException |
154 | { |
155 | File projectXmlFile = null; |
156 | |
157 | if ( buildDefinition != null ) |
158 | { |
159 | String buildFile = StringUtils.clean( buildDefinition.getBuildFile() ); |
160 | |
161 | if ( !StringUtils.isEmpty( buildFile ) ) |
162 | { |
163 | projectXmlFile = new File( workingDirectory, buildFile ); |
164 | } |
165 | } |
166 | |
167 | if ( projectXmlFile == null ) |
168 | { |
169 | projectXmlFile = new File( workingDirectory, "project.xml" ); |
170 | } |
171 | |
172 | if ( !projectXmlFile.exists() ) |
173 | { |
174 | throw new ContinuumBuildExecutorException( "Could not find Maven project descriptor." ); |
175 | } |
176 | |
177 | try |
178 | { |
179 | metadataHelper.mapMetadata( new ContinuumProjectBuildingResult(), projectXmlFile, project ); |
180 | } |
181 | catch ( MavenOneMetadataHelperException e ) |
182 | { |
183 | throw new ContinuumBuildExecutorException( "Error while mapping metadata.", e ); |
184 | } |
185 | } |
186 | } |