blob: 237b52b073363bda5a1a689af50699c775fbe426 [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;
25import java.util.List;
26
27import org.apache.maven.artifact.manager.WagonManager;
28import org.apache.maven.artifact.repository.ArtifactRepository;
29import org.apache.maven.plugin.MojoExecutionException;
30import org.apache.maven.plugin.logging.Log;
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000031import org.apache.maven.plugins.annotations.Component;
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000032import org.apache.maven.plugins.annotations.LifecyclePhase;
33import org.apache.maven.plugins.annotations.Mojo;
34import org.apache.maven.plugins.annotations.Parameter;
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000035import org.apache.maven.project.MavenProject;
36import org.apache.maven.settings.Settings;
37
38
39/**
40 * Deploys bundle details to a remote OBR repository (command-line goal)
Carsten Ziegeler0a4eff12015-05-12 06:04:41 +000041 *
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000042 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
43 */
Carsten Ziegeler0a4eff12015-05-12 06:04:41 +000044@Mojo( name = "deploy-file", requiresProject = false, defaultPhase = LifecyclePhase.DEPLOY )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000045public final class ObrDeployFile extends AbstractFileMojo
46{
47 /**
48 * When true, ignore remote locking.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000049 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000050 @Parameter( property = "ignoreLock" )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000051 private boolean ignoreLock;
52
53 /**
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000054 * Remote OBR Repository.
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000055 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000056 @Parameter( property = "remoteOBR" )
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000057 private String remoteOBR;
58
59 /**
60 * Local OBR Repository.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000061 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000062 @Parameter( property = "obrRepository" )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000063 private String obrRepository;
64
65 /**
66 * Project types which this plugin supports.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000067 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000068 @Parameter
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000069 private List supportedProjectTypes = Arrays.asList( new String[]
70 { "jar", "bundle" } );
71
72 /**
73 * Remote repository id, used to lookup authentication settings.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000074 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000075 @Parameter( property = "repositoryId", defaultValue = "remote-repository", required = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000076 private String repositoryId;
77
78 /**
79 * Remote OBR repository URL, where the bundle details are to be uploaded.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000080 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000081 @Parameter( property = "url", required = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000082 private String url;
83
84 /**
85 * Optional public URL where the bundle has been deployed.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000086 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000087 @Parameter( property = "bundleUrl" )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000088 private String bundleUrl;
89
90 /**
91 * Local Repository.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000092 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000093 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000094 private ArtifactRepository localRepository;
95
96 /**
97 * Local Maven settings.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000098 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +000099 @Parameter( defaultValue = "${settings}", readonly = true, required = true )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000100 private Settings settings;
101
102 /**
103 * The Wagon manager.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000104 */
Carsten Ziegeler318c2cb2015-03-09 13:57:23 +0000105 @Component
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000106 private WagonManager m_wagonManager;
107
108
109 public void execute() throws MojoExecutionException
110 {
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000111 MavenProject project = getProject();
Clement Escoffiere89ca3b2008-05-29 16:10:43 +0000112 String projectType = project.getPackaging();
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000113
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000114 // ignore unsupported project types, useful when bundleplugin is configured in parent pom
115 if ( !supportedProjectTypes.contains( projectType ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000116 {
Stuart McCulloch464dbac2008-08-03 09:47:04 +0000117 getLog().warn(
118 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000119 return;
120 }
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000121 else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000122 {
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000123 getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000124 return;
125 }
126
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000127 // if the user doesn't supply an explicit name for the remote OBR file, use the local name instead
128 if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) )
129 {
130 remoteOBR = obrRepository;
131 }
132
133 URI tempURI = ObrUtils.findRepositoryXml( "", remoteOBR );
Stuart McCulloch435f3902008-08-02 16:50:31 +0000134 String repositoryName = new File( tempURI.getSchemeSpecificPart() ).getName();
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000135
136 Log log = getLog();
137 ObrUpdate update;
138
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000139 RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
140 remoteFile.connect( repositoryId, url );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000141
142 // ======== LOCK REMOTE OBR ========
143 log.info( "LOCK " + remoteFile + '/' + repositoryName );
144 remoteFile.lockFile( repositoryName, ignoreLock );
145 File downloadedRepositoryXml = null;
146
147 try
148 {
149 // ======== DOWNLOAD REMOTE OBR ========
150 log.info( "Downloading " + repositoryName );
151 downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
152
153 String mavenRepository = localRepository.getBasedir();
154
155 URI repositoryXml = downloadedRepositoryXml.toURI();
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000156 URI obrXmlFile = ObrUtils.toFileURI( obrXml );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000157 URI bundleJar;
158
159 if ( null == file )
160 {
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000161 bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000162 }
163 else
164 {
165 bundleJar = file.toURI();
166 }
167
Stuart McCulloch2e99c3b2008-02-18 04:09:52 +0000168 Config userConfig = new Config();
169 userConfig.setRemoteFile( true );
Clement Escoffier78c9efb2008-05-29 16:20:08 +0000170
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000171 if ( null != bundleUrl )
172 {
Stuart McCulloch2e99c3b2008-02-18 04:09:52 +0000173 // public URL differs from the bundle file location
Clement Escoffier210e3092009-04-23 16:35:20 +0000174 URI uri = URI.create( bundleUrl );
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000175 log.info( "Computed bundle uri: " + uri );
Clement Escoffier210e3092009-04-23 16:35:20 +0000176 userConfig.setRemoteBundle( uri );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000177 }
178 else if ( null != file )
179 {
Stuart McCulloch2e99c3b2008-02-18 04:09:52 +0000180 // assume file will be deployed in remote repository, so find the remote relative location
Clement Escoffier210e3092009-04-23 16:35:20 +0000181 URI uri = URI.create( localRepository.pathOf( project.getArtifact() ) );
Stuart McCullochd98b02e2011-06-28 23:20:37 +0000182 log.info( "Computed bundle uri: " + uri );
Clement Escoffier210e3092009-04-23 16:35:20 +0000183 userConfig.setRemoteBundle( uri );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000184 }
185
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000186 update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
187 update.parseRepositoryXml();
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000188
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000189 update.updateRepository( bundleJar, null, null );
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000190
191 update.writeRepositoryXml();
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000192
193 if ( downloadedRepositoryXml.exists() )
194 {
195 // ======== UPLOAD MODIFIED OBR ========
196 log.info( "Uploading " + repositoryName );
197 remoteFile.put( downloadedRepositoryXml, repositoryName );
198 }
199 }
200 catch ( Exception e )
201 {
202 log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
203 }
204 finally
205 {
206 // ======== UNLOCK REMOTE OBR ========
207 log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
208 remoteFile.unlockFile( repositoryName );
209 remoteFile.disconnect();
210
211 if ( null != downloadedRepositoryXml )
212 {
213 downloadedRepositoryXml.delete();
214 }
215 }
216 }
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000217}