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; |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 25 | import java.util.Iterator; |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 26 | import java.util.List; |
| 27 | import java.util.regex.Matcher; |
| 28 | import java.util.regex.Pattern; |
| 29 | |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 30 | import org.apache.maven.artifact.Artifact; |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 31 | import org.apache.maven.artifact.manager.WagonManager; |
| 32 | import org.apache.maven.artifact.repository.ArtifactRepository; |
| 33 | import org.apache.maven.plugin.AbstractMojo; |
| 34 | import org.apache.maven.plugin.MojoExecutionException; |
| 35 | import org.apache.maven.plugin.logging.Log; |
| 36 | import org.apache.maven.project.MavenProject; |
| 37 | import org.apache.maven.settings.Settings; |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Deploys bundle details to a remote OBR repository (life-cycle goal) |
| 42 | * |
| 43 | * @goal deploy |
| 44 | * @phase deploy |
| 45 | * |
| 46 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 47 | */ |
| 48 | public final class ObrDeploy extends AbstractMojo |
| 49 | { |
| 50 | /** |
| 51 | * When true, ignore remote locking. |
| 52 | * |
| 53 | * @parameter expression="${ignoreLock}" |
| 54 | */ |
| 55 | private boolean ignoreLock; |
| 56 | |
| 57 | /** |
Stuart McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 58 | * Remote OBR Repository. |
| 59 | * |
| 60 | * @parameter expression="${remoteOBR}" default-value="NONE" |
| 61 | */ |
| 62 | private String remoteOBR; |
| 63 | |
| 64 | /** |
| 65 | * Local OBR Repository. |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 66 | * |
| 67 | * @parameter expression="${obrRepository}" |
| 68 | */ |
| 69 | private String obrRepository; |
| 70 | |
| 71 | /** |
| 72 | * Project types which this plugin supports. |
| 73 | * |
| 74 | * @parameter |
| 75 | */ |
| 76 | private List supportedProjectTypes = Arrays.asList( new String[] |
Stuart McCulloch | 4fd6239 | 2008-05-15 16:15:15 +0000 | [diff] [blame^] | 77 | { "jar", "bundle" } ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @parameter expression="${project.distributionManagementArtifactRepository}" |
| 81 | * @readonly |
| 82 | */ |
| 83 | private ArtifactRepository deploymentRepository; |
| 84 | |
| 85 | /** |
| 86 | * Alternative deployment repository. Format: id::layout::url |
| 87 | * |
| 88 | * @parameter expression="${altDeploymentRepository}" |
| 89 | */ |
| 90 | private String altDeploymentRepository; |
| 91 | |
| 92 | /** |
| 93 | * Local Repository. |
| 94 | * |
| 95 | * @parameter expression="${localRepository}" |
| 96 | * @required |
| 97 | * @readonly |
| 98 | */ |
| 99 | private ArtifactRepository localRepository; |
| 100 | |
| 101 | /** |
| 102 | * The Maven project. |
| 103 | * |
| 104 | * @parameter expression="${project}" |
| 105 | * @required |
| 106 | * @readonly |
| 107 | */ |
| 108 | private MavenProject project; |
| 109 | |
| 110 | /** |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 111 | * @parameter expression="${project.attachedArtifacts} |
| 112 | * @required |
| 113 | * @readonly |
| 114 | */ |
| 115 | private List attachedArtifacts; |
| 116 | |
| 117 | /** |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 118 | * Local Maven settings. |
| 119 | * |
| 120 | * @parameter expression="${settings}" |
| 121 | * @required |
| 122 | * @readonly |
| 123 | */ |
| 124 | private Settings settings; |
| 125 | |
| 126 | /** |
| 127 | * The Wagon manager. |
| 128 | * |
| 129 | * @component |
| 130 | */ |
| 131 | private WagonManager m_wagonManager; |
| 132 | |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 133 | /** |
| 134 | * Attached source artifact |
| 135 | */ |
| 136 | private Artifact m_sourceArtifact; |
| 137 | |
Stuart McCulloch | a1d3a29 | 2008-02-20 00:53:52 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Attached doc artifact |
| 140 | */ |
| 141 | private Artifact m_docArtifact; |
| 142 | |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 143 | |
| 144 | public void execute() throws MojoExecutionException |
| 145 | { |
Stuart McCulloch | 3a965b9 | 2008-03-24 02:34:11 +0000 | [diff] [blame] | 146 | String projectType = project.getPackaging(); |
| 147 | |
| 148 | // ignore unsupported project types, useful when bundleplugin is configured in parent pom |
| 149 | if ( !supportedProjectTypes.contains( projectType ) ) |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 150 | { |
Stuart McCulloch | 3a965b9 | 2008-03-24 02:34:11 +0000 | [diff] [blame] | 151 | getLog().warn( "Ignoring project type " + projectType + |
| 152 | " - supportedProjectTypes = " + supportedProjectTypes ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
Stuart McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 155 | else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) ) |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 156 | { |
Stuart McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 157 | getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Stuart McCulloch | a1d3a29 | 2008-02-20 00:53:52 +0000 | [diff] [blame] | 161 | // check for any attached sources or docs |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 162 | for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); ) |
| 163 | { |
| 164 | Artifact artifact = ( Artifact ) i.next(); |
| 165 | if ( "sources".equals( artifact.getClassifier() ) ) |
| 166 | { |
| 167 | m_sourceArtifact = artifact; |
Stuart McCulloch | a1d3a29 | 2008-02-20 00:53:52 +0000 | [diff] [blame] | 168 | } |
| 169 | else if ( "javadoc".equals( artifact.getClassifier() ) ) |
| 170 | { |
| 171 | m_docArtifact = artifact; |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Stuart McCulloch | 5d6cb73 | 2008-02-18 05:24:46 +0000 | [diff] [blame] | 175 | // if the user doesn't supply an explicit name for the remote OBR file, use the local name instead |
| 176 | if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) ) |
| 177 | { |
| 178 | remoteOBR = obrRepository; |
| 179 | } |
| 180 | |
| 181 | URI tempURI = ObrUtils.findRepositoryXml( "", remoteOBR ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 182 | String repositoryName = new File( tempURI.getPath() ).getName(); |
| 183 | |
| 184 | Log log = getLog(); |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 185 | ObrUpdate update; |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 186 | |
| 187 | RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log ); |
| 188 | openRepositoryConnection( remoteFile ); |
| 189 | |
| 190 | // ======== LOCK REMOTE OBR ======== |
| 191 | log.info( "LOCK " + remoteFile + '/' + repositoryName ); |
| 192 | remoteFile.lockFile( repositoryName, ignoreLock ); |
| 193 | File downloadedRepositoryXml = null; |
| 194 | |
| 195 | try |
| 196 | { |
| 197 | // ======== DOWNLOAD REMOTE OBR ======== |
| 198 | log.info( "Downloading " + repositoryName ); |
| 199 | downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" ); |
| 200 | |
| 201 | String mavenRepository = localRepository.getBasedir(); |
| 202 | |
| 203 | URI repositoryXml = downloadedRepositoryXml.toURI(); |
| 204 | URI obrXmlFile = ObrUtils.findObrXml( project.getResources() ); |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 205 | |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 206 | Config userConfig = new Config(); |
| 207 | userConfig.setRemoteFile( true ); |
| 208 | |
| 209 | update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log ); |
| 210 | update.parseRepositoryXml(); |
| 211 | |
| 212 | updateRemoteBundleMetadata( project.getArtifact(), update ); |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 213 | for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); ) |
| 214 | { |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 215 | updateRemoteBundleMetadata( ( Artifact ) i.next(), update ); |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 216 | } |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 217 | |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 218 | update.writeRepositoryXml(); |
| 219 | |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 220 | if ( downloadedRepositoryXml.exists() ) |
| 221 | { |
| 222 | // ======== UPLOAD MODIFIED OBR ======== |
| 223 | log.info( "Uploading " + repositoryName ); |
| 224 | remoteFile.put( downloadedRepositoryXml, repositoryName ); |
| 225 | } |
| 226 | } |
| 227 | catch ( Exception e ) |
| 228 | { |
| 229 | log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e ); |
| 230 | } |
| 231 | finally |
| 232 | { |
| 233 | // ======== UNLOCK REMOTE OBR ======== |
| 234 | log.info( "UNLOCK " + remoteFile + '/' + repositoryName ); |
| 235 | remoteFile.unlockFile( repositoryName ); |
| 236 | remoteFile.disconnect(); |
| 237 | |
| 238 | if ( null != downloadedRepositoryXml ) |
| 239 | { |
| 240 | downloadedRepositoryXml.delete(); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" ); |
| 246 | |
| 247 | |
| 248 | private void openRepositoryConnection( RemoteFileManager remoteFile ) throws MojoExecutionException |
| 249 | { |
| 250 | if ( deploymentRepository == null && altDeploymentRepository == null ) |
| 251 | { |
| 252 | String msg = "Deployment failed: repository element was not specified in the pom inside" |
| 253 | + " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter"; |
| 254 | |
| 255 | throw new MojoExecutionException( msg ); |
| 256 | } |
| 257 | |
| 258 | if ( altDeploymentRepository != null ) |
| 259 | { |
| 260 | getLog().info( "Using alternate deployment repository " + altDeploymentRepository ); |
| 261 | |
| 262 | Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository ); |
| 263 | if ( !matcher.matches() ) |
| 264 | { |
| 265 | throw new MojoExecutionException( "Invalid syntax for alternative repository \"" |
| 266 | + altDeploymentRepository + "\". Use \"id::layout::url\"." ); |
| 267 | } |
| 268 | |
| 269 | remoteFile.connect( matcher.group( 1 ).trim(), matcher.group( 3 ).trim() ); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | remoteFile.connect( deploymentRepository.getId(), deploymentRepository.getUrl() ); |
| 274 | } |
| 275 | } |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 276 | |
| 277 | |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 278 | private void updateRemoteBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 279 | { |
Stuart McCulloch | 4fd6239 | 2008-05-15 16:15:15 +0000 | [diff] [blame^] | 280 | if ( !supportedProjectTypes.contains( artifact.getType() ) ) |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 281 | { |
| 282 | return; |
| 283 | } |
Stuart McCulloch | 229f016 | 2008-04-23 13:52:42 +0000 | [diff] [blame] | 284 | else if ( null == artifact.getFile() || artifact.getFile().isDirectory() ) |
| 285 | { |
| 286 | getLog().error( "No artifact found, try \"mvn install bundle:deploy\"" ); |
| 287 | return; |
| 288 | } |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 289 | |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 290 | URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact ); |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 291 | |
Stuart McCulloch | a1d3a29 | 2008-02-20 00:53:52 +0000 | [diff] [blame] | 292 | URI sourceJar = null; |
Stuart McCulloch | 0ae86ab | 2008-02-19 06:24:30 +0000 | [diff] [blame] | 293 | if ( null != m_sourceArtifact ) |
| 294 | { |
| 295 | sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact ); |
| 296 | } |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 297 | |
Stuart McCulloch | a1d3a29 | 2008-02-20 00:53:52 +0000 | [diff] [blame] | 298 | URI docJar = null; |
| 299 | if ( null != m_docArtifact ) |
| 300 | { |
| 301 | docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact ); |
| 302 | } |
| 303 | |
| 304 | update.updateRepository( bundleJar, sourceJar, docJar ); |
Stuart McCulloch | d04364c | 2008-02-18 17:23:11 +0000 | [diff] [blame] | 305 | } |
Stuart McCulloch | bb8b9fa | 2008-02-17 16:07:14 +0000 | [diff] [blame] | 306 | } |