Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 1 | /* |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 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 | */ |
| 19 | package org.apache.felix.obr.plugin; |
| 20 | |
| 21 | |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 22 | import java.io.File; |
| 23 | import java.net.URI; |
Stuart McCulloch | d00bea3 | 2008-02-06 04:45:37 +0000 | [diff] [blame] | 24 | import java.util.Arrays; |
| 25 | import java.util.List; |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 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; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | /** |
Stuart McCulloch | bf8a12f | 2008-02-05 09:49:09 +0000 | [diff] [blame] | 36 | * Deploys bundle details to a remote OBR repository (command-line goal) |
Stuart McCulloch | 77824fb | 2008-01-29 03:45:07 +0000 | [diff] [blame] | 37 | * |
Stuart McCulloch | bf8a12f | 2008-02-05 09:49:09 +0000 | [diff] [blame] | 38 | * @requiresProject false |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 39 | * @goal deploy-file |
| 40 | * @phase deploy |
Stuart McCulloch | 77824fb | 2008-01-29 03:45:07 +0000 | [diff] [blame] | 41 | * |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 42 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 43 | */ |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 44 | public final class ObrDeployFile extends AbstractFileMojo |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 45 | { |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 46 | /** |
| 47 | * When true, ignore remote locking. |
| 48 | * |
| 49 | * @parameter expression="${ignoreLock}" |
| 50 | */ |
| 51 | private boolean ignoreLock; |
| 52 | |
| 53 | /** |
| 54 | * OBR Repository. |
| 55 | * |
| 56 | * @parameter expression="${obrRepository}" |
| 57 | */ |
| 58 | private String obrRepository; |
| 59 | |
| 60 | /** |
Stuart McCulloch | d00bea3 | 2008-02-06 04:45:37 +0000 | [diff] [blame] | 61 | * Project types which this plugin supports. |
| 62 | * |
| 63 | * @parameter |
| 64 | */ |
| 65 | private List supportedProjectTypes = Arrays.asList( new String[] |
| 66 | { "jar", "bundle" } ); |
| 67 | |
| 68 | /** |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 69 | * Remote repository id, used to lookup authentication settings. |
| 70 | * |
| 71 | * @parameter expression="${repositoryId}" default-value="remote-repository" |
| 72 | * @required |
| 73 | */ |
| 74 | private String repositoryId; |
| 75 | |
| 76 | /** |
| 77 | * Remote OBR repository URL, where the bundle details are to be uploaded. |
| 78 | * |
| 79 | * @parameter expression="${url}" |
| 80 | * @required |
| 81 | */ |
| 82 | private String url; |
| 83 | |
| 84 | /** |
| 85 | * Optional public URL where the bundle has been deployed. |
| 86 | * |
| 87 | * @parameter expression="${bundleUrl}" |
| 88 | */ |
| 89 | private String bundleUrl; |
| 90 | |
| 91 | /** |
| 92 | * Local Repository. |
| 93 | * |
| 94 | * @parameter expression="${localRepository}" |
| 95 | * @required |
| 96 | * @readonly |
| 97 | */ |
| 98 | private ArtifactRepository localRepository; |
| 99 | |
| 100 | /** |
| 101 | * Local Maven settings. |
| 102 | * |
| 103 | * @parameter expression="${settings}" |
| 104 | * @required |
| 105 | * @readonly |
| 106 | */ |
| 107 | private Settings settings; |
| 108 | |
| 109 | /** |
| 110 | * The Wagon manager. |
| 111 | * |
| 112 | * @component |
| 113 | */ |
| 114 | private WagonManager m_wagonManager; |
| 115 | |
| 116 | |
| 117 | public void execute() throws MojoExecutionException |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 118 | { |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 119 | MavenProject project = getProject(); |
| 120 | |
Stuart McCulloch | d00bea3 | 2008-02-06 04:45:37 +0000 | [diff] [blame] | 121 | if ( !supportedProjectTypes.contains( project.getPackaging() ) ) |
| 122 | { |
| 123 | getLog().info( "Ignoring packaging type " + project.getPackaging() ); |
| 124 | return; |
| 125 | } |
| 126 | else if ( "NONE".equalsIgnoreCase( obrRepository ) ) |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 127 | { |
| 128 | getLog().info( "OBR update disabled (enable with -DobrRepository)" ); |
| 129 | return; |
| 130 | } |
| 131 | |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 132 | URI tempURI = ObrUtils.findRepositoryXml( "", obrRepository ); |
| 133 | String repositoryName = new File( tempURI.getPath() ).getName(); |
| 134 | |
| 135 | Log log = getLog(); |
| 136 | ObrUpdate update; |
| 137 | |
| 138 | RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log ); |
| 139 | remoteFile.connect( repositoryId, url ); |
| 140 | |
| 141 | // ======== LOCK REMOTE OBR ======== |
| 142 | log.info( "LOCK " + remoteFile + '/' + repositoryName ); |
| 143 | remoteFile.lockFile( repositoryName, ignoreLock ); |
| 144 | File downloadedRepositoryXml = null; |
| 145 | |
| 146 | try |
| 147 | { |
| 148 | // ======== DOWNLOAD REMOTE OBR ======== |
| 149 | log.info( "Downloading " + repositoryName ); |
| 150 | downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" ); |
| 151 | |
| 152 | String mavenRepository = localRepository.getBasedir(); |
| 153 | |
| 154 | URI repositoryXml = downloadedRepositoryXml.toURI(); |
| 155 | URI obrXmlFile = ObrUtils.toFileURI( obrXml ); |
| 156 | URI bundleJar; |
| 157 | |
| 158 | if ( null == file ) |
| 159 | { |
| 160 | bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() ); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | bundleJar = file.toURI(); |
| 165 | } |
| 166 | |
Stuart McCulloch | a02dc67 | 2008-02-05 16:23:45 +0000 | [diff] [blame] | 167 | URI remoteBundleURI = null; |
| 168 | if ( null != bundleUrl ) |
| 169 | { |
| 170 | remoteBundleURI = URI.create( bundleUrl ); |
| 171 | } |
| 172 | else if ( null != file ) |
| 173 | { |
| 174 | remoteBundleURI = URI.create( localRepository.pathOf( project.getArtifact() ) ); |
| 175 | } |
| 176 | |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 177 | Config userConfig = new Config(); |
| 178 | userConfig.setRemoteBundle( remoteBundleURI ); |
| 179 | userConfig.setPathRelative( true ); |
| 180 | userConfig.setRemoteFile( true ); |
| 181 | |
| 182 | update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log ); |
| 183 | |
| 184 | update.updateRepository(); |
| 185 | |
| 186 | if ( downloadedRepositoryXml.exists() ) |
| 187 | { |
| 188 | // ======== UPLOAD MODIFIED OBR ======== |
| 189 | log.info( "Uploading " + repositoryName ); |
| 190 | remoteFile.put( downloadedRepositoryXml, repositoryName ); |
| 191 | } |
| 192 | } |
| 193 | catch ( Exception e ) |
| 194 | { |
| 195 | log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e ); |
| 196 | } |
| 197 | finally |
| 198 | { |
| 199 | // ======== UNLOCK REMOTE OBR ======== |
| 200 | log.info( "UNLOCK " + remoteFile + '/' + repositoryName ); |
| 201 | remoteFile.unlockFile( repositoryName ); |
| 202 | remoteFile.disconnect(); |
| 203 | |
| 204 | if ( null != downloadedRepositoryXml ) |
| 205 | { |
| 206 | downloadedRepositoryXml.delete(); |
| 207 | } |
| 208 | } |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 209 | } |
| 210 | } |