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 java.io.File;
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
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.artifact.Artifact;
36 import org.apache.maven.artifact.metadata.ArtifactMetadata;
37 import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
38 import org.apache.maven.continuum.model.project.BuildDefinition;
39 import org.apache.maven.continuum.model.project.Project;
40 import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
41 import org.apache.maven.project.MavenProject;
42 import org.apache.maven.project.MavenProjectHelper;
43 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
44 import org.codehaus.plexus.util.StringUtils;
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,
90 File buildOutput, Map<String, String> environments,
91 String localRepository )
92 throws ContinuumAgentBuildExecutorException, ContinuumAgentBuildCancelledException
93 {
94 String executable = getBuildAgentInstallationService().getExecutorConfigurator( BuildAgentInstallationService.MAVEN2_TYPE )
95 .getExecutable();
96
97 StringBuffer arguments = new StringBuffer();
98
99 String buildFile = getBuildFileForProject( buildDefinition );
100
101 if ( !StringUtils.isEmpty( buildFile ) && !"pom.xml".equals( buildFile ) )
102 {
103 arguments.append( "-f " ).append( buildFile ).append( " " );
104 }
105
106 arguments.append( StringUtils.clean( buildDefinition.getArguments() ) ).append( " " );
107
108 Properties props = getContinuumSystemProperties( project );
109 for ( Enumeration itr = props.propertyNames(); itr.hasMoreElements(); )
110 {
111 String name = (String) itr.nextElement();
112 String value = props.getProperty( name );
113 arguments.append( "\"-D" ).append( name ).append( "=" ).append( value ).append( "\" " );
114 }
115
116 if ( StringUtils.isNotEmpty( localRepository ) )
117 {
118 arguments.append( "\"-Dmaven.repo.local=" ).append( StringUtils.clean( localRepository ) ).append( "\" " );
119 }
120
121 arguments.append( StringUtils.clean( buildDefinition.getGoals() ) );
122
123 String m2Home = null;
124
125 if ( environments != null )
126 {
127 m2Home = environments.get( getBuildAgentInstallationService().getEnvVar( 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, BuildDefinition buildDefinition )
235 throws ContinuumAgentBuildExecutorException
236 {
237 File f = getPomFile( getBuildFileForProject( buildDefinition ), workingDirectory );
238
239 if ( !f.exists() )
240 {
241 throw new ContinuumAgentBuildExecutorException( "Could not find Maven project descriptor." );
242 }
243
244 ContinuumProjectBuildingResult result = new ContinuumProjectBuildingResult();
245
246 buildAgentMavenBuilderHelper.mapMetadataToProject( result, f, project );
247
248 if ( result.hasErrors() )
249 {
250 throw new ContinuumAgentBuildExecutorException( "Error while mapping metadata:" + result.getErrorsAsString() );
251 }
252 else
253 {
254 updateProject( project );
255 }
256 }
257
258 @Override
259 public MavenProject getMavenProject( File workingDirectory, BuildDefinition buildDefinition )
260 throws ContinuumAgentBuildExecutorException
261 {
262 ContinuumProjectBuildingResult result = new ContinuumProjectBuildingResult();
263
264 File f = getPomFile( getBuildFileForProject( buildDefinition ), workingDirectory );
265
266 if ( !f.exists() )
267 {
268 throw new ContinuumAgentBuildExecutorException( "Could not find Maven project descriptor '" + f + "'." );
269 }
270
271 MavenProject project = buildAgentMavenBuilderHelper.getMavenProject( result, f );
272
273 if ( result.hasErrors() )
274 {
275 throw new ContinuumAgentBuildExecutorException(
276 "Unable to read the Maven project descriptor '" + f + "': " + result.getErrorsAsString() );
277 }
278 return project;
279 }
280
281 private static File getPomFile( String projectBuildFile, File workingDirectory )
282 {
283 File f = null;
284
285 String buildFile = StringUtils.clean( projectBuildFile );
286
287 if ( !StringUtils.isEmpty( buildFile ) )
288 {
289 f = new File( workingDirectory, buildFile );
290 }
291
292 if ( f == null )
293 {
294 f = new File( workingDirectory, "pom.xml" );
295 }
296
297 return f;
298 }
299
300 }