1 package org.apache.continuum.buildagent.build.execution.maven.m2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.artifact.Artifact;
29 import org.apache.maven.artifact.metadata.ArtifactMetadata;
30 import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
31 import org.apache.maven.continuum.model.project.BuildDefinition;
32 import org.apache.maven.continuum.model.project.Project;
33 import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.project.MavenProjectHelper;
36 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
37 import org.codehaus.plexus.util.StringUtils;
38
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.Enumeration;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Properties;
45
46 public class MavenTwoBuildExecutor
47 extends AbstractBuildExecutor
48 implements ContinuumAgentBuildExecutor
49 {
50 public static final String CONFIGURATION_GOALS = "goals";
51
52 public static final String ID = ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR;
53
54
55
56
57 private MavenProjectHelper projectHelper;
58
59
60
61
62 private BuildAgentMavenBuilderHelper buildAgentMavenBuilderHelper;
63
64 public MavenTwoBuildExecutor()
65 {
66 super( ID, true );
67 }
68
69 public MavenProjectHelper getProjectHelper()
70 {
71 return projectHelper;
72 }
73
74 public void setProjectHelper( MavenProjectHelper projectHelper )
75 {
76 this.projectHelper = projectHelper;
77 }
78
79 public BuildAgentMavenBuilderHelper getBuildAgentMavenBuilderHelper()
80 {
81 return buildAgentMavenBuilderHelper;
82 }
83
84 public void setBuildAgentMavenBuilderHelper( BuildAgentMavenBuilderHelper builderHelper )
85 {
86 this.buildAgentMavenBuilderHelper = builderHelper;
87 }
88
89 public ContinuumAgentBuildExecutionResult build( Project project, BuildDefinition buildDefinition, File buildOutput,
90 Map<String, String> environments, String localRepository )
91 throws ContinuumAgentBuildExecutorException, ContinuumAgentBuildCancelledException
92 {
93 String executable = getBuildAgentInstallationService().getExecutorConfigurator(
94 BuildAgentInstallationService.MAVEN2_TYPE ).getExecutable();
95
96 StringBuffer arguments = new StringBuffer();
97
98 String buildFile = getBuildFileForProject( buildDefinition );
99
100 if ( !StringUtils.isEmpty( buildFile ) && !"pom.xml".equals( buildFile ) )
101 {
102 arguments.append( "-f " ).append( buildFile ).append( " " );
103 }
104
105 arguments.append( StringUtils.clean( buildDefinition.getArguments() ) ).append( " " );
106
107 Properties props = getContinuumSystemProperties( project );
108 for ( Enumeration itr = props.propertyNames(); itr.hasMoreElements(); )
109 {
110 String name = (String) itr.nextElement();
111 String value = props.getProperty( name );
112 arguments.append( "\"-D" ).append( name ).append( "=" ).append( value ).append( "\" " );
113 }
114
115 if ( StringUtils.isNotEmpty( localRepository ) )
116 {
117 arguments.append( "\"-Dmaven.repo.local=" ).append( StringUtils.clean( localRepository ) ).append( "\" " );
118 }
119
120 arguments.append( StringUtils.clean( buildDefinition.getGoals() ) );
121
122 String m2Home = null;
123
124 if ( environments != null )
125 {
126 m2Home = environments.get( getBuildAgentInstallationService().getEnvVar(
127 BuildAgentInstallationService.MAVEN2_TYPE ) );
128 }
129
130 if ( StringUtils.isNotEmpty( m2Home ) )
131 {
132 executable = m2Home + File.separator + "bin" + File.separator + executable;
133 setResolveExecutable( false );
134 }
135
136 return executeShellCommand( project, executable, arguments.toString(), buildOutput, environments );
137 }
138
139 @Override
140 public List<Artifact> getDeployableArtifacts( Project continuumProject, File workingDirectory,
141 BuildDefinition buildDefinition )
142 throws ContinuumAgentBuildExecutorException
143 {
144 MavenProject project = getMavenProject( workingDirectory, buildDefinition );
145
146
147
148
149 Artifact artifact = project.getArtifact();
150
151 String projectPackaging = project.getPackaging();
152
153 boolean isPomArtifact = "pom".equals( projectPackaging );
154
155 if ( isPomArtifact )
156 {
157 artifact.setFile( project.getFile() );
158 }
159 else
160 {
161
162 ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, project.getFile() );
163
164 artifact.addMetadata( metadata );
165
166 String finalName = project.getBuild().getFinalName();
167
168 String filename = finalName + "." + artifact.getArtifactHandler().getExtension();
169
170 String buildDirectory = project.getBuild().getDirectory();
171
172 File artifactFile = new File( buildDirectory, filename );
173
174 artifact.setFile( artifactFile );
175
176
177 File sourcesFile = new File( buildDirectory, finalName + "-sources.jar" );
178
179 if ( sourcesFile.exists() )
180 {
181 projectHelper.attachArtifact( project, "java-source", "sources", sourcesFile );
182 }
183
184
185 File testsSourcesFile = new File( buildDirectory, finalName + "-test-sources.jar" );
186
187 if ( testsSourcesFile.exists() )
188 {
189 projectHelper.attachArtifact( project, "java-source", "test-sources", testsSourcesFile );
190 }
191
192
193 File javadocFile = new File( buildDirectory, finalName + "-javadoc.jar" );
194
195 if ( javadocFile.exists() )
196 {
197 projectHelper.attachArtifact( project, "javadoc", "javadoc", javadocFile );
198 }
199
200
201 File clientFile = new File( buildDirectory, finalName + "-client.jar" );
202
203 if ( clientFile.exists() )
204 {
205 projectHelper.attachArtifact( project, projectPackaging + "-client", "client", clientFile );
206 }
207
208
209 File testsFile = new File( buildDirectory, finalName + "-tests.jar" );
210
211 if ( testsFile.exists() )
212 {
213 projectHelper.attachArtifact( project, "jar", "tests", testsFile );
214 }
215 }
216
217 List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
218
219 List<Artifact> artifacts = new ArrayList<Artifact>( attachedArtifacts.size() + 1 );
220
221 if ( artifact.getFile().exists() )
222 {
223 artifacts.add( artifact );
224 }
225
226 for ( Artifact attachedArtifact : attachedArtifacts )
227 {
228 artifacts.add( attachedArtifact );
229 }
230
231 return artifacts;
232 }
233
234 public void updateProjectFromWorkingDirectory( File workingDirectory, Project project,
235 BuildDefinition buildDefinition )
236 throws ContinuumAgentBuildExecutorException
237 {
238 File f = getPomFile( getBuildFileForProject( buildDefinition ), workingDirectory );
239
240 if ( !f.exists() )
241 {
242 throw new ContinuumAgentBuildExecutorException( "Could not find Maven project descriptor." );
243 }
244
245 ContinuumProjectBuildingResult result = new ContinuumProjectBuildingResult();
246
247 buildAgentMavenBuilderHelper.mapMetadataToProject( result, f, project );
248
249 if ( result.hasErrors() )
250 {
251 throw new ContinuumAgentBuildExecutorException(
252 "Error while mapping metadata:" + result.getErrorsAsString() );
253 }
254 else
255 {
256 updateProject( project );
257 }
258 }
259
260 @Override
261 public MavenProject getMavenProject( File workingDirectory, BuildDefinition buildDefinition )
262 throws ContinuumAgentBuildExecutorException
263 {
264 ContinuumProjectBuildingResult result = new ContinuumProjectBuildingResult();
265
266 File f = getPomFile( getBuildFileForProject( buildDefinition ), workingDirectory );
267
268 if ( !f.exists() )
269 {
270 throw new ContinuumAgentBuildExecutorException( "Could not find Maven project descriptor '" + f + "'." );
271 }
272
273 MavenProject project = buildAgentMavenBuilderHelper.getMavenProject( result, f );
274
275 if ( result.hasErrors() )
276 {
277 throw new ContinuumAgentBuildExecutorException(
278 "Unable to read the Maven project descriptor '" + f + "': " + result.getErrorsAsString() );
279 }
280 return project;
281 }
282
283 private static File getPomFile( String projectBuildFile, File workingDirectory )
284 {
285 File f = null;
286
287 String buildFile = StringUtils.clean( projectBuildFile );
288
289 if ( !StringUtils.isEmpty( buildFile ) )
290 {
291 f = new File( workingDirectory, buildFile );
292 }
293
294 if ( f == null )
295 {
296 f = new File( workingDirectory, "pom.xml" );
297 }
298
299 return f;
300 }
301
302 }