View Javadoc

1   package org.apache.continuum.buildagent.build.execution.maven.m2;
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.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       * @plexus.requirement
56       */
57      private MavenProjectHelper projectHelper;
58  
59      /**
60       * @plexus.requirement
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         // Maven could help us out a lot more here by knowing how to get the deployment artifacts from a project.
147         // TODO: this is currently quite lame
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             // Attach pom
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             // sources jar
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             // tests sources jar
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             // javadoc jar
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             // client jar
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             // Tests jar
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 }