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.io.File;
23  import java.net.URI;
24  import java.util.Arrays;
25  import java.util.List;
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.plugins.annotations.Component;
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  import org.apache.maven.settings.Settings;
37  
38  
39  /**
40   * Deploys bundle details to a remote OBR repository (command-line goal)
41   *
42   * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
43   */
44  @Mojo( name = "deploy-file", requiresProject = false, defaultPhase = LifecyclePhase.DEPLOY )
45  public final class ObrDeployFile extends AbstractFileMojo
46  {
47      /**
48       * When true, ignore remote locking.
49       */
50      @Parameter( property = "ignoreLock" )
51      private boolean ignoreLock;
52  
53      /**
54       * Remote OBR Repository.
55       */
56      @Parameter( property = "remoteOBR" )
57      private String remoteOBR;
58  
59      /**
60       * Local OBR Repository.
61       */
62      @Parameter( property = "obrRepository" )
63      private String obrRepository;
64  
65      /**
66       * Project types which this plugin supports.
67       */
68      @Parameter
69      private List supportedProjectTypes = Arrays.asList( new String[]
70          { "jar", "bundle" } );
71  
72      /**
73       * Remote repository id, used to lookup authentication settings.
74       */
75      @Parameter( property = "repositoryId", defaultValue = "remote-repository", required = true )
76      private String repositoryId;
77  
78      /**
79       * Remote OBR repository URL, where the bundle details are to be uploaded.
80       */
81      @Parameter( property = "url", required = true )
82      private String url;
83  
84      /**
85       * Optional public URL where the bundle has been deployed.
86       */
87      @Parameter( property = "bundleUrl" )
88      private String bundleUrl;
89  
90      /**
91       * Local Repository.
92       */
93      @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
94      private ArtifactRepository localRepository;
95  
96      /**
97       * Local Maven settings.
98       */
99      @Parameter( defaultValue = "${settings}", readonly = true, required = true )
100     private Settings settings;
101 
102     /**
103      * The Wagon manager.
104      */
105     @Component
106     private WagonManager m_wagonManager;
107 
108 
109     public void execute() throws MojoExecutionException
110     {
111         MavenProject project = getProject();
112         String projectType = project.getPackaging();
113 
114         // ignore unsupported project types, useful when bundleplugin is configured in parent pom
115         if ( !supportedProjectTypes.contains( projectType ) )
116         {
117             getLog().warn(
118                 "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
119             return;
120         }
121         else if ( "NONE".equalsIgnoreCase( remoteOBR ) || "false".equalsIgnoreCase( remoteOBR ) )
122         {
123             getLog().info( "Remote OBR update disabled (enable with -DremoteOBR)" );
124             return;
125         }
126 
127         // if the user doesn't supply an explicit name for the remote OBR file, use the local name instead
128         if ( null == remoteOBR || remoteOBR.trim().length() == 0 || "true".equalsIgnoreCase( remoteOBR ) )
129         {
130             remoteOBR = obrRepository;
131         }
132 
133         URI tempURI = ObrUtils.findRepositoryXml( "", remoteOBR );
134         String repositoryName = new File( tempURI.getSchemeSpecificPart() ).getName();
135 
136         Log log = getLog();
137         ObrUpdate update;
138 
139         RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
140         remoteFile.connect( repositoryId, url );
141 
142         // ======== LOCK REMOTE OBR ========
143         log.info( "LOCK " + remoteFile + '/' + repositoryName );
144         remoteFile.lockFile( repositoryName, ignoreLock );
145         File downloadedRepositoryXml = null;
146 
147         try
148         {
149             // ======== DOWNLOAD REMOTE OBR ========
150             log.info( "Downloading " + repositoryName );
151             downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
152 
153             String mavenRepository = localRepository.getBasedir();
154 
155             URI repositoryXml = downloadedRepositoryXml.toURI();
156             URI obrXmlFile = ObrUtils.toFileURI( obrXml );
157             URI bundleJar;
158 
159             if ( null == file )
160             {
161                 bundleJar = ObrUtils.getArtifactURI( localRepository, project.getArtifact() );
162             }
163             else
164             {
165                 bundleJar = file.toURI();
166             }
167 
168             Config userConfig = new Config();
169             userConfig.setRemoteFile( true );
170 
171             if ( null != bundleUrl )
172             {
173                 // public URL differs from the bundle file location
174                 URI uri = URI.create( bundleUrl );
175                 log.info( "Computed bundle uri: " + uri );
176                 userConfig.setRemoteBundle( uri );
177             }
178             else if ( null != file )
179             {
180                 // assume file will be deployed in remote repository, so find the remote relative location
181                 URI uri = URI.create( localRepository.pathOf( project.getArtifact() ) );
182                 log.info( "Computed bundle uri: " + uri );
183                 userConfig.setRemoteBundle( uri );
184             }
185 
186             update = new ObrUpdate( repositoryXml, obrXmlFile, project, mavenRepository, userConfig, log );
187             update.parseRepositoryXml();
188 
189             update.updateRepository( bundleJar, null, null );
190 
191             update.writeRepositoryXml();
192 
193             if ( downloadedRepositoryXml.exists() )
194             {
195                 // ======== UPLOAD MODIFIED OBR ========
196                 log.info( "Uploading " + repositoryName );
197                 remoteFile.put( downloadedRepositoryXml, repositoryName );
198             }
199         }
200         catch ( Exception e )
201         {
202             log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
203         }
204         finally
205         {
206             // ======== UNLOCK REMOTE OBR ========
207             log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
208             remoteFile.unlockFile( repositoryName );
209             remoteFile.disconnect();
210 
211             if ( null != downloadedRepositoryXml )
212             {
213                 downloadedRepositoryXml.delete();
214             }
215         }
216     }
217 }