ObrInstall xref

    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             update.parseRepositoryXml();
    142
    143             updateLocalBundleMetadata( project.getArtifact(), update );
    144             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
    145             {
    146                 updateLocalBundleMetadata( ( Artifact ) i.next(), update );
    147             }
    148
    149             update.writeRepositoryXml();
    150         }
    151         catch ( Exception e )
    152         {
    153             log.warn( "Exception while updating local OBR: " + e.getLocalizedMessage(), e );
    154         }
    155     }
    156
    157
    158     private void updateLocalBundleMetadata( Artifact artifact, ObrUpdate update ) throws MojoExecutionException
    159     {
    160         if ( !supportedProjectTypes.contains( artifact.getType() ) )
    161         {
    162             return;
    163         }
    164         else if ( null == artifact.getFile() || artifact.getFile().isDirectory() )
    165         {
    166             getLog().error( "No artifact found, try \"mvn install bundle:install\"" );
    167             return;
    168         }
    169
    170         URI bundleJar = ObrUtils.getArtifactURI( localRepository, artifact );
    171
    172         URI sourceJar = null;
    173         if ( null != m_sourceArtifact )
    174         {
    175             sourceJar = ObrUtils.getArtifactURI( localRepository, m_sourceArtifact );
    176         }
    177
    178         URI docJar = null;
    179         if ( null != m_docArtifact )
    180         {
    181             docJar = ObrUtils.getArtifactURI( localRepository, m_docArtifact );
    182         }
    183
    184         update.updateRepository( bundleJar, sourceJar, docJar );
    185     }
    186 }