blob: ca673f01f4fd50d039d8e9832af0a4c7460b8273 [file] [log] [blame]
Stuart McCullocha132a142008-02-05 11:18:20 +00001/*
Stuart McCullochb657c2f2008-01-25 08:10:25 +00002 * 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 */
19package org.apache.felix.obr.plugin;
20
21
Stuart McCullocha132a142008-02-05 11:18:20 +000022import java.io.File;
23import java.net.URI;
Stuart McCullochd00bea32008-02-06 04:45:37 +000024import java.util.Arrays;
25import java.util.List;
Stuart McCullocha132a142008-02-05 11:18:20 +000026
27import org.apache.maven.artifact.manager.WagonManager;
28import org.apache.maven.artifact.repository.ArtifactRepository;
29import org.apache.maven.plugin.MojoExecutionException;
30import org.apache.maven.plugin.logging.Log;
31import org.apache.maven.project.MavenProject;
32import org.apache.maven.settings.Settings;
Stuart McCullochb657c2f2008-01-25 08:10:25 +000033
34
35/**
Stuart McCullochbf8a12f2008-02-05 09:49:09 +000036 * Deploys bundle details to a remote OBR repository (command-line goal)
Stuart McCulloch77824fb2008-01-29 03:45:07 +000037 *
Stuart McCullochbf8a12f2008-02-05 09:49:09 +000038 * @requiresProject false
Stuart McCullochb657c2f2008-01-25 08:10:25 +000039 * @goal deploy-file
40 * @phase deploy
Stuart McCulloch77824fb2008-01-29 03:45:07 +000041 *
Stuart McCullochb657c2f2008-01-25 08:10:25 +000042 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
43 */
Stuart McCullocha132a142008-02-05 11:18:20 +000044public final class ObrDeployFile extends AbstractFileMojo
Stuart McCullochb657c2f2008-01-25 08:10:25 +000045{
Stuart McCullocha132a142008-02-05 11:18:20 +000046 /**
47 * When true, ignore remote locking.
48 *
49 * @parameter expression="${ignoreLock}"
50 */
51 private boolean ignoreLock;
52
53 /**
54 * OBR Repository.
55 *
56 * @parameter expression="${obrRepository}"
57 */
58 private String obrRepository;
59
60 /**
Stuart McCullochd00bea32008-02-06 04:45:37 +000061 * Project types which this plugin supports.
62 *
63 * @parameter
64 */
65 private List supportedProjectTypes = Arrays.asList( new String[]
66 { "jar", "bundle" } );
67
68 /**
Stuart McCullocha132a142008-02-05 11:18:20 +000069 * Remote repository id, used to lookup authentication settings.
70 *
71 * @parameter expression="${repositoryId}" default-value="remote-repository"
72 * @required
73 */
74 private String repositoryId;
75
76 /**
77 * Remote OBR repository URL, where the bundle details are to be uploaded.
78 *
79 * @parameter expression="${url}"
80 * @required
81 */
82 private String url;
83
84 /**
85 * Optional public URL where the bundle has been deployed.
86 *
87 * @parameter expression="${bundleUrl}"
88 */
89 private String bundleUrl;
90
91 /**
92 * Local Repository.
93 *
94 * @parameter expression="${localRepository}"
95 * @required
96 * @readonly
97 */
98 private ArtifactRepository localRepository;
99
100 /**
101 * Local Maven settings.
102 *
103 * @parameter expression="${settings}"
104 * @required
105 * @readonly
106 */
107 private Settings settings;
108
109 /**
110 * The Wagon manager.
111 *
112 * @component
113 */
114 private WagonManager m_wagonManager;
115
116
117 public void execute() throws MojoExecutionException
Stuart McCullochb657c2f2008-01-25 08:10:25 +0000118 {
Stuart McCullocha132a142008-02-05 11:18:20 +0000119 MavenProject project = getProject();
120
Stuart McCullochd00bea32008-02-06 04:45:37 +0000121 if ( !supportedProjectTypes.contains( project.getPackaging() ) )
122 {
123 getLog().info( "Ignoring packaging type " + project.getPackaging() );
124 return;
125 }
126 else if ( "NONE".equalsIgnoreCase( obrRepository ) )
Stuart McCullocha132a142008-02-05 11:18:20 +0000127 {
128 getLog().info( "OBR update disabled (enable with -DobrRepository)" );
129 return;
130 }
131
Stuart McCullocha132a142008-02-05 11:18:20 +0000132 URI tempURI = ObrUtils.findRepositoryXml( "", obrRepository );
133 String repositoryName = new File( tempURI.getPath() ).getName();
134
135 Log log = getLog();
136 ObrUpdate update;
137
138 RemoteFileManager remoteFile = new RemoteFileManager( m_wagonManager, settings, log );
139 remoteFile.connect( repositoryId, url );
140
141 // ======== LOCK REMOTE OBR ========
142 log.info( "LOCK " + remoteFile + '/' + repositoryName );
143 remoteFile.lockFile( repositoryName, ignoreLock );
144 File downloadedRepositoryXml = null;
145
146 try
147 {
148 // ======== DOWNLOAD REMOTE OBR ========
149 log.info( "Downloading " + repositoryName );
150 downloadedRepositoryXml = remoteFile.get( repositoryName, ".xml" );
151
152 String mavenRepository = localRepository.getBasedir();
153
154 URI repositoryXml = downloadedRepositoryXml.toURI();
155 URI obrXmlFile = ObrUtils.toFileURI( obrXml );
156 URI bundleJar;
157
158 if ( null == file )
159 {
160 bundleJar = ObrUtils.findBundleJar( localRepository, project.getArtifact() );
161 }
162 else
163 {
164 bundleJar = file.toURI();
165 }
166
Stuart McCullocha02dc672008-02-05 16:23:45 +0000167 URI remoteBundleURI = null;
168 if ( null != bundleUrl )
169 {
170 remoteBundleURI = URI.create( bundleUrl );
171 }
172 else if ( null != file )
173 {
174 remoteBundleURI = URI.create( localRepository.pathOf( project.getArtifact() ) );
175 }
176
Stuart McCullocha132a142008-02-05 11:18:20 +0000177 Config userConfig = new Config();
178 userConfig.setRemoteBundle( remoteBundleURI );
179 userConfig.setPathRelative( true );
180 userConfig.setRemoteFile( true );
181
182 update = new ObrUpdate( repositoryXml, obrXmlFile, project, bundleJar, mavenRepository, userConfig, log );
183
184 update.updateRepository();
185
186 if ( downloadedRepositoryXml.exists() )
187 {
188 // ======== UPLOAD MODIFIED OBR ========
189 log.info( "Uploading " + repositoryName );
190 remoteFile.put( downloadedRepositoryXml, repositoryName );
191 }
192 }
193 catch ( Exception e )
194 {
195 log.warn( "Exception while updating remote OBR: " + e.getLocalizedMessage(), e );
196 }
197 finally
198 {
199 // ======== UNLOCK REMOTE OBR ========
200 log.info( "UNLOCK " + remoteFile + '/' + repositoryName );
201 remoteFile.unlockFile( repositoryName );
202 remoteFile.disconnect();
203
204 if ( null != downloadedRepositoryXml )
205 {
206 downloadedRepositoryXml.delete();
207 }
208 }
Stuart McCullochb657c2f2008-01-25 08:10:25 +0000209 }
210}