    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   */
    19  package org.apache.felix.obrplugin;
    20
    21
    22  import java.net.URI;
    23  import java.util.Arrays;
    24  import java.util.Iterator;
    25  import java.util.List;
    26
    27  import org.apache.maven.artifact.Artifact;
    28  import org.apache.maven.artifact.repository.ArtifactRepository;
    29  import org.apache.maven.plugin.AbstractMojo;
    30  import org.apache.maven.plugin.MojoExecutionException;
    31  import org.apache.maven.plugin.logging.Log;
    32  import org.apache.maven.project.MavenProject;
    33
    34
    35  /**
    36   * Installs bundle details in the local OBR repository (life-cycle goal)
    37   *
    38   * @goal install
    39   * @phase install
    40   * @threadSafe
    41   *
    42   * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
    43   */
    44  public final class ObrInstall extends AbstractMojo
    45  {
    46      /**
    47       * OBR Repository.
    48       *
    49       * @parameter expression="${obrRepository}"
    50       */
    51      private String obrRepository;
    52
    53      /**
    54       * Project types which this plugin supports.
    55       *
    56       * @parameter
    57       */
    58      private List supportedProjectTypes = Arrays.asList( new String[]
    59          { "jar", "bundle" } );
    60
    61      /**
    62       * Local Repository.
    63       *
    64       * @parameter expression="${localRepository}"
    65       * @required
    66       * @readonly
    67       */
    68      private ArtifactRepository localRepository;
    69
    70      /**
    71       * The Maven project.
    72       *
    73       * @parameter expression="${project}"
    74       * @required
    75       * @readonly
    76       */
    77      private MavenProject project;
    78
    79      /**
    80       * @parameter expression="${project.attachedArtifacts}
    81       * @required
    82       * @readonly
    83       */
    84      private List attachedArtifacts;
    85
    86      /**
    87       * Attached source artifact
    88       */
    89      private Artifact m_sourceArtifact;
    90
    91      /**
    92       * Attached doc artifact
    93       */
    94      private Artifact m_docArtifact;
    95
    96
    97      public void execute()
    98      {
    99          String projectType = project.getPackaging();
    100
    101         // ignore unsupported project types, useful when bundleplugin is configured in parent pom
    102         if ( !supportedProjectTypes.contains( projectType ) )
    103         {
    104             getLog().warn(
    105                 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
    106             return;
    107         }
    108         else if ( "NONE".equalsIgnoreCase( obrRepository ) || "false".equalsIgnoreCase( obrRepository ) )
    109         {
    110             getLog().info( "Local OBR update disabled (enable with -DobrRepository)" );
    111             return;
    112         }
    113
    114         // check for any attached sources or docs
    115         for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
    116         {
    117             Artifact artifact = ( Artifact ) i.next();
    118             if ( "sources".equals( artifact.getClassifier() ) )
    119             {
    120                 m_sourceArtifact = artifact;
    121             }
    122             else if ( "javadoc".equals( artifact.getClassifier() ) )
    123             {
    124                 m_docArtifact = artifact;
    125             }
    126         }
    127
    128         Log log = getLog();
    129         ObrUpdate update;
    130
    131         try
    132         {
    133             String mavenRepository = localRepository.getBasedir();
    134
    135             URI repositoryXml = ObrUtils.findRepositoryXml( mavenRepository, obrRepository );
    136             URI obrXmlFile = ObrUtils.findObrXml( project );
    137
    138             Config userConfig = new Config();
    139
    140             update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
    141             synchronized ( ObrUpdate.class ) // protect against concurrent in-process updates
    142             {
    143                 update.parseRepositoryXml();
    144
    145                 updateLocalBundleMetadata( project.getArtifact(), update );
    146                 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
    147                 {
    148                     updateLocalBundleMetadata( ( Artifact ) i.next(), update );
    149                 }
    150
    151                 update.writeRepositoryXml();
    152             }
    153         }
    154         catch ( Exception e )
    155         {
    156             log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
    157         }
    158     }
    159
    160
    161     private void updateLocalBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
    162     {
    163         if ( !supportedProjectTypes.contains( artifact.getType() ) )
    164         {
    165             return;
    166         }
    167         else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
    168         {
    169             getLog().error( "No artifact found, try \"mvn install bundle:install\"" );
    170             return;
    171         }
    172
    173         URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
    174
    175         URI sourceJar = null;
    176         if ( null != m_sourceArtifact )
    177         {
    178             sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
    179         }
    180
    181         URI docJar = null;
    182         if ( null != m_docArtifact )
    183         {
    184             docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
    185         }
    186
    187         update.updateRepository( bundleJar, sourceJar, docJar );
    188     }
    189 }