blob: 27bb5b7bc4eb0f7e7f85ae96f3affbb3b833d6b0 [file] [log] [blame]
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Stuart McCullochc792b372008-02-17 16:12:24 +000019package org.apache.felix.obrplugin;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000020
21
22import java.io.File;
23import java.net.URI;
24import java.util.Arrays;
Stuart McCullochd04364c2008-02-18 17:23:11 +000025import java.util.Iterator;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000026import java.util.List;
27import java.util.regex.Matcher;
28import java.util.regex.Pattern;
29
Stuart McCullochd04364c2008-02-18 17:23:11 +000030import org.apache.maven.artifact.Artifact;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000031import org.apache.maven.artifact.manager.WagonManager;
32import org.apache.maven.artifact.repository.ArtifactRepository;
33import org.apache.maven.plugin.AbstractMojo;
34import org.apache.maven.plugin.MojoExecutionException;
35import org.apache.maven.plugin.logging.Log;
36import org.apache.maven.project.MavenProject;
37import org.apache.maven.settings.Settings;
38
39
40/**
41 * Deploys bundle details to a remote OBR repository (life-cycle goal)
42 *
43 * @goal deploy
44 * @phase deploy
45 *
46 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
47 */
48public final class ObrDeploy extends AbstractMojo
49{
50 /**
51 * When true, ignore remote locking.
52 *
53 * @parameter expression="${ignoreLock}"
54 */
55 private boolean ignoreLock;
56
57 /**
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000058 * Remote OBR Repository.
59 *
60 * @parameter expression="${remoteOBR}" default-value="NONE"
61 */
62 private String remoteOBR;
63
64 /**
65 * Local OBR Repository.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000066 *
67 * @parameter expression="${obrRepository}"
68 */
69 private String obrRepository;
70
71 /**
72 * Project types which this plugin supports.
73 *
74 * @parameter
75 */
76 private List supportedProjectTypes = Arrays.asList( new String[]
Stuart McCulloch4fd62392008-05-15 16:15:15 +000077 { "jar", "bundle" } );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000078
79 /**
80 * @parameter expression="${project.distributionManagementArtifactRepository}"
81 * @readonly
82 */
83 private ArtifactRepository deploymentRepository;
84
85 /**
86 * Alternative deployment repository. Format: id::layout::url
87 *
88 * @parameter expression="${altDeploymentRepository}"
89 */
90 private String altDeploymentRepository;
91
92 /**
93 * Local Repository.
94 *
95 * @parameter expression="${localRepository}"
96 * @required
97 * @readonly
98 */
99 private ArtifactRepository localRepository;
100
101 /**
102 * The Maven project.
103 *
104 * @parameter expression="${project}"
105 * @required
106 * @readonly
107 */
108 private MavenProject project;
109
110 /**
Stuart McCullochd04364c2008-02-18 17:23:11 +0000111 * @parameter expression="${project.attachedArtifacts}
112 * @required
113 * @readonly
114 */
115 private List attachedArtifacts;
116
117 /**
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000118 * Local Maven settings.
119 *
120 * @parameter expression="${settings}"
121 * @required
122 * @readonly
123 */
124 private Settings settings;
125
126 /**
127 * The Wagon manager.
128 *
129 * @component
130 */
131 private WagonManager m_wagonManager;
132
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000133 /**
134 * Attached source artifact
135 */
136 private Artifact m_sourceArtifact;
137
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000138 /**
139 * Attached doc artifact
140 */
141 private Artifact m_docArtifact;
142
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000143
144 public void execute() throws MojoExecutionException
145 {
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000146 String projectType = project.getPackaging();
147
148 // ignore unsupported project types, useful when bundleplugin is configured in parent pom
149 if ( !supportedProjectTypes.contains( projectType ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000150 {
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000151 getLog().warn( "Ignoring project type " + projectType +
152 " - supportedProjectTypes = " + supportedProjectTypes );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000153 return;
154 }
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000155 else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000156 {
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000157 getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000158 return;
159 }
160
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000161 // check for any attached sources or docs
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000162 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
163 {
164 Artifact artifact = ( Artifact ) i.next();
165 if ( "sources".equals( artifact.getClassifier() ) )
166 {
167 m_sourceArtifact = artifact;
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000168 }
169 else if ( "javadoc".equals( artifact.getClassifier() ) )
170 {
171 m_docArtifact = artifact;
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000172 }
173 }
174
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000175 // if the user doesn't supply an explicit name for the remote OBR file, use the local name instead
176 if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) )
177 {
178 remoteOBR = obrRepository;
179 }
180
181 URI tempURI = ObrUtils.findRepositoryXml( "", remoteOBR );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000182 String repositoryName = new File( tempURI.getPath() ).getName();
183
184 Log log = getLog();
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000185 ObrUpdate update;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000186
187 RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
188 openRepositoryConnection( remoteFile );
189
190 // ======== LOCK REMOTE OBR ========
191 log.info( "LOCK " + remoteFile + '/' + repositoryName );
192 remoteFile.lockFile( repositoryName, ignoreLock );
193 File downloadedRepositoryXml = null;
194
195 try
196 {
197 // ======== DOWNLOAD REMOTE OBR ========
198 log.info( "Downloading " + repositoryName );
199 downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
200
201 String mavenRepository = localRepository.getBasedir();
202
203 URI repositoryXml = downloadedRepositoryXml.toURI();
204 URI obrXmlFile = ObrUtils.findObrXml( project.getResources() );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000205
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000206 Config userConfig = new Config();
207 userConfig.setRemoteFile( true );
208
209 update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
210 update.parseRepositoryXml();
211
212 updateRemoteBundleMetadata( project.getArtifact(), update );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000213 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
214 {
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000215 updateRemoteBundleMetadata( ( Artifact ) i.next(), update );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000216 }
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000217
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000218 update.writeRepositoryXml();
219
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000220 if ( downloadedRepositoryXml.exists() )
221 {
222 // ======== UPLOAD MODIFIED OBR ========
223 log.info( "Uploading " + repositoryName );
224 remoteFile.put( downloadedRepositoryXml, repositoryName );
225 }
226 }
227 catch ( Exception e )
228 {
229 log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
230 }
231 finally
232 {
233 // ======== UNLOCK REMOTE OBR ========
234 log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
235 remoteFile.unlockFile( repositoryName );
236 remoteFile.disconnect();
237
238 if ( null != downloadedRepositoryXml )
239 {
240 downloadedRepositoryXml.delete();
241 }
242 }
243 }
244
245 private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" );
246
247
248 private void openRepositoryConnection( RemoteFileManager remoteFile ) throws MojoExecutionException
249 {
250 if ( deploymentRepository == null && altDeploymentRepository == null )
251 {
252 String msg = "Deployment failed: repository element was not specified in the pom inside"
253 + " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter";
254
255 throw new MojoExecutionException( msg );
256 }
257
258 if ( altDeploymentRepository != null )
259 {
260 getLog().info( "Using alternate deployment repository " + altDeploymentRepository );
261
262 Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository );
263 if ( !matcher.matches() )
264 {
265 throw new MojoExecutionException( "Invalid syntax for alternative repository \""
266 + altDeploymentRepository + "\". Use \"id::layout::url\"." );
267 }
268
269 remoteFile.connect( matcher.group( 1 ).trim(), matcher.group( 3 ).trim() );
270 }
271 else
272 {
273 remoteFile.connect( deploymentRepository.getId(), deploymentRepository.getUrl() );
274 }
275 }
Stuart McCullochd04364c2008-02-18 17:23:11 +0000276
277
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000278 private void updateRemoteBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
Stuart McCullochd04364c2008-02-18 17:23:11 +0000279 {
Stuart McCulloch4fd62392008-05-15 16:15:15 +0000280 if ( !supportedProjectTypes.contains( artifact.getType() ) )
Stuart McCullochd04364c2008-02-18 17:23:11 +0000281 {
282 return;
283 }
Stuart McCulloch229f0162008-04-23 13:52:42 +0000284 else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
285 {
286 getLog().error( "No artifact found, try \"mvn install bundle:deploy\"" );
287 return;
288 }
Stuart McCullochd04364c2008-02-18 17:23:11 +0000289
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000290 URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000291
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000292 URI sourceJar = null;
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000293 if ( null != m_sourceArtifact )
294 {
295 sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
296 }
Stuart McCullochd04364c2008-02-18 17:23:11 +0000297
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000298 URI docJar = null;
299 if ( null != m_docArtifact )
300 {
301 docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
302 }
303
304 update.updateRepository( bundleJar, sourceJar, docJar );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000305 }
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000306}