blob: 8af1114ffe05dfb1eaedecda8349d9677fa6d949 [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;
31import org.apache.maven.project.MavenProject;
32import org.apache.maven.settings.Settings;
33
34
35/**
36 * Deploys bundle details to a remote OBR repository (command-line goal)
37 *
38 * @requiresProject false
39 * @goal deploy-file
40 * @phase deploy
41 *
42 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
43 */
44public final class ObrDeployFile extends AbstractFileMojo
45{
46 /**
47 * When true, ignore remote locking.
48 *
49 * @parameter expression="${ignoreLock}"
50 */
51 private boolean ignoreLock;
52
53 /**
Stuart McCulloch5d6cb732008-02-18 05:24:46 +000054 * Remote OBR Repository.
55 *
56 * @parameter expression="${remoteOBR}"
57 */
58 private String remoteOBR;
59
60 /**
61 * Local OBR Repository.
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +000062 *
63 * @parameter expression="${obrRepository}"
64 */
65 private String obrRepository;
66
67 /**
68 * Project types which this plugin supports.
69 *
70 * @parameter
71 */
72 private List supportedProjectTypes = Arrays.asList( new String[]
73 { "jar", "bundle" } );
74
75 /**
76 * Remote repository id, used to lookup authentication settings.
77 *
78 * @parameter expression="${repositoryId}" default-value="remote-repository"
79 * @required
80 */
81 private String repositoryId;
82
83 /**
84 * Remote OBR repository URL, where the bundle details are to be uploaded.
85 *
86 * @parameter expression="${url}"
87 * @required
88 */
89 private String url;
90
91 /**
92 * Optional public URL where the bundle has been deployed.
93 *
94 * @parameter expression="${bundleUrl}"
95 */
96 private String bundleUrl;
97
98 /**
99 * Local Repository.
100 *
101 * @parameter expression="${localRepository}"
102 * @required
103 * @readonly
104 */
105 private ArtifactRepository localRepository;
106
107 /**
108 * Local Maven settings.
109 *
110 * @parameter expression="${settings}"
111 * @required
112 * @readonly
113 */
114 private Settings settings;
115
116 /**
117 * The Wagon manager.
118 *
119 * @component
120 */
121 private WagonManager m_wagonManager;
122
123
124 public void execute() throws MojoExecutionException
125 {
126 MavenProject project = getProject();
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000127 String projectType = project.getPackaging();
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000128
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000129 // ignore unsupported project types, useful when bundleplugin is configured in parent pom
130 if ( !supportedProjectTypes.contains( projectType ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000131 {
Stuart McCulloch3a965b92008-03-24 02:34:11 +0000132 getLog().warn( "Ignoring project type " + projectType +
133 " - supportedProjectTypes = " + supportedProjectTypes );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000134 return;
135 }
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000136 else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) )
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000137 {
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000138 getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000139 return;
140 }
141
Stuart McCulloch5d6cb732008-02-18 05:24:46 +0000142 // if the user doesn't supply an explicit name for the remote OBR file, use the local name instead
143 if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) )
144 {
145 remoteOBR = obrRepository;
146 }
147
148 URI tempURI = ObrUtils.findRepositoryXml( "", remoteOBR );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000149 String repositoryName = new File( tempURI.getPath() ).getName();
150
151 Log log = getLog();
152 ObrUpdate update;
153
154 RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
155 remoteFile.connect( repositoryId, url );
156
157 // ======== LOCK REMOTE OBR ========
158 log.info( "LOCK " + remoteFile + '/' + repositoryName );
159 remoteFile.lockFile( repositoryName, ignoreLock );
160 File downloadedRepositoryXml = null;
161
162 try
163 {
164 // ======== DOWNLOAD REMOTE OBR ========
165 log.info( "Downloading " + repositoryName );
166 downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
167
168 String mavenRepository = localRepository.getBasedir();
169
170 URI repositoryXml = downloadedRepositoryXml.toURI();
171 URI obrXmlFile = ObrUtils.toFileURI( obrXml );
172 URI bundleJar;
173
174 if ( null == file )
175 {
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000176 bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000177 }
178 else
179 {
180 bundleJar = file.toURI();
181 }
182
Stuart McCulloch2e99c3b2008-02-18 04:09:52 +0000183 Config userConfig = new Config();
184 userConfig.setRemoteFile( true );
185
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000186 if ( null != bundleUrl )
187 {
Stuart McCulloch2e99c3b2008-02-18 04:09:52 +0000188 // public URL differs from the bundle file location
189 userConfig.setRemoteBundle( URI.create( bundleUrl ) );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000190 }
191 else if ( null != file )
192 {
Stuart McCulloch2e99c3b2008-02-18 04:09:52 +0000193 // assume file will be deployed in remote repository, so find the remote relative location
194 userConfig.setRemoteBundle( URI.create( localRepository.pathOf( project.getArtifact() ) ) );
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000195 }
196
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000197 update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
198 update.parseRepositoryXml();
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000199
Stuart McCullocha1d3a292008-02-20 00:53:52 +0000200 update.updateRepository( bundleJar, null, null );
Stuart McCulloch0ae86ab2008-02-19 06:24:30 +0000201
202 update.writeRepositoryXml();
Stuart McCullochbb8b9fa2008-02-17 16:07:14 +0000203
204 if ( downloadedRepositoryXml.exists() )
205 {
206 // ======== UPLOAD MODIFIED OBR ========
207 log.info( "Uploading " + repositoryName );
208 remoteFile.put( downloadedRepositoryXml, repositoryName );
209 }
210 }
211 catch ( Exception e )
212 {
213 log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
214 }
215 finally
216 {
217 // ======== UNLOCK REMOTE OBR ========
218 log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
219 remoteFile.unlockFile( repositoryName );
220 remoteFile.disconnect();
221
222 if ( null != downloadedRepositoryXml )
223 {
224 downloadedRepositoryXml.delete();
225 }
226 }
227 }
228}