Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 1 | /* |
| 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 McCulloch | c792b37 | 2008-02-17 16:12:24 +0000 | [diff] [blame] | 19 | package org.apache.felix.obrplugin; |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.net.URI; |
| 24 | import java.util.Arrays; |
| 25 | import java.util.List; |
| 26 | |
| 27 | import org.apache.maven.artifact.manager.WagonManager; |
| 28 | import org.apache.maven.artifact.repository.ArtifactRepository; |
| 29 | import org.apache.maven.plugin.MojoExecutionException; |
| 30 | import org.apache.maven.plugin.logging.Log; |
| 31 | import org.apache.maven.project.MavenProject; |
| 32 | import 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 | */ |
| 44 | public 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 McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 54 | * Remote OBR Repository. |
| 55 | * |
| 56 | * @parameter expression="${remoteOBR}" |
| 57 | */ |
| 58 | private String remoteOBR; |
| 59 | |
| 60 | /** |
| 61 | * Local OBR Repository. |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 62 | * |
| 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 McCulloch | 3a965b9 | 2008-03-24 02:34:11 +0000 | [diff] [blame^] | 127 | String projectType = project.getPackaging(); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 128 | |
Stuart McCulloch | 3a965b9 | 2008-03-24 02:34:11 +0000 | [diff] [blame^] | 129 | // ignore unsupported project types, useful when bundleplugin is configured in parent pom |
| 130 | if ( !supportedProjectTypes.contains( projectType ) ) |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 131 | { |
Stuart McCulloch | 3a965b9 | 2008-03-24 02:34:11 +0000 | [diff] [blame^] | 132 | getLog().warn( "Ignoring project type " + projectType + |
| 133 | " - supportedProjectTypes = " + supportedProjectTypes ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 134 | return; |
| 135 | } |
Stuart McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 136 | else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) ) |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 137 | { |
Stuart McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 138 | getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
Stuart McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 142 | // 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 McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 149 | 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 McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 176 | bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 177 | } |
| 178 | else |
| 179 | { |
| 180 | bundleJar = file.toURI(); |
| 181 | } |
| 182 | |
Stuart McCulloch | 2e99c3b | 2008-02-18 04:09:52 +0000 | [diff] [blame] | 183 | Config userConfig = new Config(); |
| 184 | userConfig.setRemoteFile( true ); |
| 185 | |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 186 | if ( null != bundleUrl ) |
| 187 | { |
Stuart McCulloch | 2e99c3b | 2008-02-18 04:09:52 +0000 | [diff] [blame] | 188 | // public URL differs from the bundle file location |
| 189 | userConfig.setRemoteBundle( URI.create( bundleUrl ) ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 190 | } |
| 191 | else if ( null != file ) |
| 192 | { |
Stuart McCulloch | 2e99c3b | 2008-02-18 04:09:52 +0000 | [diff] [blame] | 193 | // assume file will be deployed in remote repository, so find the remote relative location |
| 194 | userConfig.setRemoteBundle( URI.create( localRepository.pathOf( project.getArtifact() ) ) ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 197 | update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log ); |
| 198 | update.parseRepositoryXml(); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 199 | |
Stuart McCulloch | a1d3a29 | 2008-02-20 00:53:52 +0000 | [diff] [blame] | 200 | update.updateRepository( bundleJar, null, null ); |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 201 | |
| 202 | update.writeRepositoryXml(); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 203 | |
| 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 | } |