blob: 11f09b2250102008bf07540cbb952fa7720426c0 [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;
Stuart McCulloch69ab1132008-08-02 18:47:46 +000024import java.net.URL;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000025import java.util.Arrays;
Stuart McCullochd04364c2008-02-18 17:23:11 +000026import java.util.Iterator;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000027import java.util.List;
28import java.util.regex.Matcher;
29import java.util.regex.Pattern;
30
Stuart McCullochd04364c2008-02-18 17:23:11 +000031import org.apache.maven.artifact.Artifact;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000032import org.apache.maven.artifact.manager.WagonManager;
33import org.apache.maven.artifact.repository.ArtifactRepository;
34import org.apache.maven.plugin.AbstractMojo;
35import org.apache.maven.plugin.MojoExecutionException;
36import org.apache.maven.plugin.logging.Log;
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000037import org.apache.maven.plugins.annotations.Component;
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000038import org.apache.maven.plugins.annotations.LifecyclePhase;
39import org.apache.maven.plugins.annotations.Mojo;
40import org.apache.maven.plugins.annotations.Parameter;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000041import org.apache.maven.project.MavenProject;
42import org.apache.maven.settings.Settings;
43
44
45/**
46 * Deploys bundle details to a remote OBR repository (life-cycle goal)
Carsten Ziegeler0a4eff12015-05-12 06:04:41 +000047 *
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000048 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
49 */
Carsten Ziegeler0a4eff12015-05-12 06:04:41 +000050@Mojo( name = "deploy", threadSafe = true, defaultPhase = LifecyclePhase.DEPLOY )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000051public final class ObrDeploy extends AbstractMojo
52{
53 /**
54 * When true, ignore remote locking.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000055 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000056 @Parameter( property = "ignoreLock" )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000057 private boolean ignoreLock;
58
59 /**
Stuart McCulloch69ab1132008-08-02 18:47:46 +000060 * Optional public URL prefix for the remote repository.
Stuart McCulloch69ab1132008-08-02 18:47:46 +000061 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000062 @Parameter( property = "prefixUrl" )
Stuart McCulloch69ab1132008-08-02 18:47:46 +000063 private String prefixUrl;
64
65 /**
66 * Optional public URL where the bundle has been deployed.
Stuart McCulloch69ab1132008-08-02 18:47:46 +000067 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000068 @Parameter( property = "bundleUrl" )
Stuart McCulloch69ab1132008-08-02 18:47:46 +000069 private String bundleUrl;
70
71 /**
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000072 * Remote OBR Repository.
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000073 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000074 @Parameter( property = "remoteOBR", defaultValue = "NONE" )
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000075 private String remoteOBR;
76
77 /**
78 * Local OBR Repository.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000079 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000080 @Parameter( property = "obrRepository" )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000081 private String obrRepository;
82
83 /**
84 * Project types which this plugin supports.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000085 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000086 @Parameter
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000087 private List supportedProjectTypes = Arrays.asList( new String[]
Stuart McCulloch4fd62392008-05-15 16:15:15 +000088 { "jar", "bundle" } );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000089
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000090 @Parameter( defaultValue = "${project.distributionManagementArtifactRepository}", readonly = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000091 private ArtifactRepository deploymentRepository;
92
93 /**
94 * Alternative deployment repository. Format: id::layout::url
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000095 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000096 @Parameter( property = "altDeploymentRepository" )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000097 private String altDeploymentRepository;
98
99 /**
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000100 * OBR specific deployment repository. Format: id::layout::url
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000101 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +0000102 @Parameter( property = "obrDeploymentRepository" )
103 private String obrDeploymentRepository;
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000104
105 /**
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000106 * Local Repository.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000107 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +0000108 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000109 private ArtifactRepository localRepository;
110
111 /**
112 * The Maven project.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000113 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +0000114 @Parameter( defaultValue = "${project}", readonly = true, required = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000115 private MavenProject project;
116
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +0000117 @Parameter( defaultValue = "${project.attachedArtifacts}", readonly = true, required = true )
Stuart McCullochd04364c2008-02-18 17:23:11 +0000118 private List attachedArtifacts;
119
120 /**
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000121 * Local Maven settings.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000122 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +0000123 @Parameter( defaultValue = "${settings}", readonly = true, required = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000124 private Settings settings;
125
126 /**
127 * The Wagon manager.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000128 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +0000129 @Component
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000130 private WagonManager m_wagonManager;
131
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000132 /**
133 * Attached source artifact
134 */
135 private Artifact m_sourceArtifact;
136
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000137 /**
138 * Attached doc artifact
139 */
140 private Artifact m_docArtifact;
141
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000142
143 public void execute() throws MojoExecutionException
144 {
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000145 String projectType = project.getPackaging();
146
147 // ignore unsupported project types, useful when bundleplugin is configured in parent pom
148 if ( !supportedProjectTypes.contains( projectType ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000149 {
Stuart McCulloch464dbac2008-08-03 09:47:04 +0000150 getLog().warn(
151 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000152 return;
153 }
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000154 else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000155 {
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000156 getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000157 return;
158 }
159
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000160 // check for any attached sources or docs
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000161 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
162 {
163 Artifact artifact = ( Artifact ) i.next();
164 if ( "sources".equals( artifact.getClassifier() ) )
165 {
166 m_sourceArtifact = artifact;
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000167 }
168 else if ( "javadoc".equals( artifact.getClassifier() ) )
169 {
170 m_docArtifact = artifact;
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000171 }
172 }
173
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000174 // if the user doesn't supply an explicit name for the remote OBR file, use the local name instead
175 if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) )
176 {
177 remoteOBR = obrRepository;
178 }
179
180 URI tempURI = ObrUtils.findRepositoryXml( "", remoteOBR );
Stuart McCulloch435f3902008-08-02 16:50:31 +0000181 String repositoryName = new File( tempURI.getSchemeSpecificPart() ).getName();
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000182
183 Log log = getLog();
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000184 ObrUpdate update;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000185
186 RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
187 openRepositoryConnection( remoteFile );
188
189 // ======== LOCK REMOTE OBR ========
190 log.info( "LOCK " + remoteFile + '/' + repositoryName );
191 remoteFile.lockFile( repositoryName, ignoreLock );
192 File downloadedRepositoryXml = null;
193
194 try
195 {
196 // ======== DOWNLOAD REMOTE OBR ========
197 log.info( "Downloading " + repositoryName );
198 downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
199
200 String mavenRepository = localRepository.getBasedir();
201
202 URI repositoryXml = downloadedRepositoryXml.toURI();
Stuart McCulloch25687f82011-06-28 00:15:26 +0000203 URI obrXmlFile = ObrUtils.findObrXml( project );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000204
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000205 Config userConfig = new Config();
206 userConfig.setRemoteFile( true );
Stuart McCulloch464dbac2008-08-03 09:47:04 +0000207
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000208 if ( bundleUrl != null )
209 {
210 // public URL differs from the bundle file location
Clement Escoffier210e3092009-04-23 16:35:20 +0000211 URI uri = URI.create( bundleUrl );
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000212 log.info( "Computed bundle uri: " + uri );
Clement Escoffier210e3092009-04-23 16:35:20 +0000213 userConfig.setRemoteBundle( uri );
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000214 }
215 else if ( prefixUrl != null )
216 {
217 // support absolute bundle URLs based on given prefix
218 URI bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() );
Stuart McCulloch464dbac2008-08-03 09:47:04 +0000219 String relative = ObrUtils.getRelativeURI( ObrUtils.toFileURI( mavenRepository ), bundleJar )
220 .toASCIIString();
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000221 URL resourceURL = new URL( new URL( prefixUrl + '/' ), relative );
Clement Escoffier210e3092009-04-23 16:35:20 +0000222 URI uri = URI.create( resourceURL.toString() );
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000223 log.info( "Computed bundle uri: " + uri );
Clement Escoffier210e3092009-04-23 16:35:20 +0000224 userConfig.setRemoteBundle( uri );
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000225 }
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000226
227 update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
228 update.parseRepositoryXml();
229
230 updateRemoteBundleMetadata( project.getArtifact(), update );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000231 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
232 {
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000233 updateRemoteBundleMetadata( ( Artifact ) i.next(), update );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000234 }
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000235
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000236 update.writeRepositoryXml();
237
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000238 if ( downloadedRepositoryXml.exists() )
239 {
240 // ======== UPLOAD MODIFIED OBR ========
241 log.info( "Uploading " + repositoryName );
242 remoteFile.put( downloadedRepositoryXml, repositoryName );
243 }
244 }
245 catch ( Exception e )
246 {
247 log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
248 }
249 finally
250 {
251 // ======== UNLOCK REMOTE OBR ========
252 log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
253 remoteFile.unlockFile( repositoryName );
254 remoteFile.disconnect();
255
256 if ( null != downloadedRepositoryXml )
257 {
258 downloadedRepositoryXml.delete();
259 }
260 }
261 }
262
263 private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" );
264
265
266 private void openRepositoryConnection( RemoteFileManager remoteFile ) throws MojoExecutionException
267 {
Stuart McCulloch69ab1132008-08-02 18:47:46 +0000268 // use OBR specific deployment location?
269 if ( obrDeploymentRepository != null )
270 {
271 altDeploymentRepository = obrDeploymentRepository;
272 }
273
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000274 if ( deploymentRepository == null && altDeploymentRepository == null )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000275 {
276 String msg = "Deployment failed: repository element was not specified in the pom inside"
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000277 + " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter";
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000278
279 throw new MojoExecutionException( msg );
280 }
281
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000282 if ( altDeploymentRepository != null )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000283 {
284 getLog().info( "Using alternate deployment repository " + altDeploymentRepository );
285
286 Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository );
287 if ( !matcher.matches() )
288 {
289 throw new MojoExecutionException( "Invalid syntax for alternative repository \""
290 + altDeploymentRepository + "\". Use \"id::layout::url\"." );
291 }
292
293 remoteFile.connect( matcher.group( 1 ).trim(), matcher.group( 3 ).trim() );
294 }
295 else
296 {
297 remoteFile.connect( deploymentRepository.getId(), deploymentRepository.getUrl() );
298 }
299 }
Stuart McCullochd04364c2008-02-18 17:23:11 +0000300
301
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000302 private void updateRemoteBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
Stuart McCullochd04364c2008-02-18 17:23:11 +0000303 {
Stuart McCulloch4fd62392008-05-15 16:15:15 +0000304 if ( !supportedProjectTypes.contains( artifact.getType() ) )
Stuart McCullochd04364c2008-02-18 17:23:11 +0000305 {
306 return;
307 }
Stuart McCulloch229f0162008-04-23 13:52:42 +0000308 else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
309 {
310 getLog().error( "No artifact found, try \"mvn install bundle:deploy\"" );
311 return;
312 }
Stuart McCullochd04364c2008-02-18 17:23:11 +0000313
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000314 URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000315
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000316 URI sourceJar = null;
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000317 if ( null != m_sourceArtifact )
318 {
319 sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
320 }
Stuart McCullochd04364c2008-02-18 17:23:11 +0000321
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000322 URI docJar = null;
323 if ( null != m_docArtifact )
324 {
325 docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
326 }
327
328 update.updateRepository( bundleJar, sourceJar, docJar );
Stuart McCullochd04364c2008-02-18 17:23:11 +0000329 }
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000330}