View Javadoc

1   package org.apache.maven.continuum.core.action;
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.model.repository.LocalRepository;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.artifact.deployer.ArtifactDeployer;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
27  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
29  import org.apache.maven.continuum.configuration.ConfigurationService;
30  import org.apache.maven.continuum.execution.ContinuumBuildExecutor;
31  import org.apache.maven.continuum.execution.manager.BuildExecutorManager;
32  import org.apache.maven.continuum.execution.maven.m2.MavenBuilderHelper;
33  import org.apache.maven.continuum.model.project.BuildDefinition;
34  import org.apache.maven.continuum.model.project.Project;
35  import org.apache.maven.continuum.project.ContinuumProjectState;
36  import org.apache.maven.continuum.utils.WorkingDirectoryService;
37  
38  import java.io.File;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
44   * @version $Id: DeployArtifactContinuumAction.java 1372260 2012-08-13 04:29:09Z brett $
45   * @plexus.component role="org.codehaus.plexus.action.Action"
46   * role-hint="deploy-artifact"
47   */
48  public class DeployArtifactContinuumAction
49      extends AbstractContinuumAction
50  {
51      /**
52       * @plexus.requirement
53       */
54      private ConfigurationService configurationService;
55  
56      /**
57       * @plexus.requirement
58       */
59      private BuildExecutorManager buildExecutorManager;
60  
61      /**
62       * @plexus.requirement
63       */
64      private WorkingDirectoryService workingDirectoryService;
65  
66      /**
67       * @plexus.requirement
68       */
69      private ArtifactDeployer artifactDeployer;
70  
71      /**
72       * @plexus.requirement
73       */
74      private MavenBuilderHelper builderHelper;
75  
76      /**
77       * @plexus.requirement
78       */
79      private ArtifactRepositoryFactory artifactRepositoryFactory;
80  
81      public void execute( Map context )
82          throws Exception
83      {
84          // ----------------------------------------------------------------------
85          // Get parameters from the context
86          // ----------------------------------------------------------------------
87  
88          File deploymentRepositoryDirectory = configurationService.getDeploymentRepositoryDirectory();
89  
90          if ( deploymentRepositoryDirectory != null )
91          {
92  
93              Project project = getProject( context );
94  
95              ContinuumBuildExecutor buildExecutor = buildExecutorManager.getBuildExecutor( project.getExecutorId() );
96  
97              // ----------------------------------------------------------------------
98              // This is really a precondition for this action to execute
99              // ----------------------------------------------------------------------
100 
101             if ( project.getState() == ContinuumProjectState.OK )
102             {
103                 BuildDefinition buildDefinition = getBuildDefinition( context );
104 
105                 String projectScmRootUrl = getProjectScmRootUrl( context, project.getScmUrl() );
106 
107                 List<Project> projectsWithCommonScmRoot = getListOfProjectsInGroupWithCommonScmRoot( context );
108 
109                 List<Artifact> artifacts = buildExecutor.getDeployableArtifacts( project,
110                                                                                  workingDirectoryService.getWorkingDirectory(
111                                                                                      project, projectScmRootUrl,
112                                                                                      projectsWithCommonScmRoot ),
113                                                                                  buildDefinition );
114 
115                 LocalRepository repository = project.getProjectGroup().getLocalRepository();
116 
117                 builderHelper.setLocalRepository( repository );
118 
119                 ArtifactRepository localRepository = builderHelper.getLocalRepository();
120 
121                 for ( Artifact artifact : artifacts )
122                 {
123                     ArtifactRepositoryLayout repositoryLayout = new DefaultRepositoryLayout();
124 
125                     if ( !deploymentRepositoryDirectory.exists() )
126                     {
127                         deploymentRepositoryDirectory.mkdirs();
128                     }
129 
130                     String location = deploymentRepositoryDirectory.toURL().toExternalForm();
131 
132                     ArtifactRepository deploymentRepository =
133                         artifactRepositoryFactory.createDeploymentArtifactRepository( "deployment-repository", location,
134                                                                                       repositoryLayout, true );
135 
136                     artifactDeployer.deploy( artifact.getFile(), artifact, deploymentRepository, localRepository );
137                 }
138             }
139         }
140     }
141 }