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