| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.apache.maven.plugin.eclipse; |
| 20 | |
|
| 21 | |
import java.io.File; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.util.Collection; |
| 24 | |
import java.util.Collections; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.List; |
| 27 | |
import java.util.Properties; |
| 28 | |
import java.util.jar.Attributes; |
| 29 | |
import java.util.jar.JarFile; |
| 30 | |
import java.util.jar.Manifest; |
| 31 | |
|
| 32 | |
import org.apache.maven.artifact.Artifact; |
| 33 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
| 34 | |
import org.apache.maven.plugin.AbstractMojo; |
| 35 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 36 | |
import org.apache.maven.plugin.MojoFailureException; |
| 37 | |
import org.apache.maven.plugin.logging.Log; |
| 38 | |
import org.apache.maven.project.MavenProject; |
| 39 | |
import org.apache.maven.project.MavenProjectBuilder; |
| 40 | |
import org.apache.maven.project.ProjectBuildingException; |
| 41 | |
import org.apache.maven.shared.osgi.Maven2OsgiConverter; |
| 42 | |
import org.codehaus.plexus.archiver.ArchiverException; |
| 43 | |
import org.codehaus.plexus.archiver.UnArchiver; |
| 44 | |
import org.codehaus.plexus.archiver.manager.ArchiverManager; |
| 45 | |
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; |
| 46 | |
import org.codehaus.plexus.components.interactivity.InputHandler; |
| 47 | |
import org.codehaus.plexus.util.FileUtils; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public class InstallPluginsMojo |
| 57 | |
extends AbstractMojo |
| 58 | |
{ |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public static final String PROP_UNPACK_PLUGIN = "eclipse.unpack"; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
private File eclipseDir; |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private boolean overwrite; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private Collection artifacts; |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
private String pluginDependencyTypes; |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
private ArtifactRepository localRepository; |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
private MavenProjectBuilder projectBuilder; |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
private ArchiverManager archiverManager; |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
private InputHandler inputHandler; |
| 130 | |
|
| 131 | |
|
| 132 | |
private File pluginsDir; |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
private Maven2OsgiConverter maven2OsgiConverter; |
| 138 | |
|
| 139 | |
public InstallPluginsMojo() |
| 140 | 0 | { |
| 141 | |
|
| 142 | 0 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
protected InstallPluginsMojo( File eclipseDir, boolean overwrite, List dependencyArtifacts, |
| 146 | |
String pluginDependencyTypes, ArtifactRepository localRepository, |
| 147 | |
MavenProjectBuilder projectBuilder, ArchiverManager archiverManager, |
| 148 | |
InputHandler inputHandler, Log log ) |
| 149 | 48 | { |
| 150 | 48 | this.eclipseDir = eclipseDir; |
| 151 | 48 | this.overwrite = overwrite; |
| 152 | 48 | artifacts = dependencyArtifacts; |
| 153 | 48 | this.pluginDependencyTypes = pluginDependencyTypes; |
| 154 | 48 | this.localRepository = localRepository; |
| 155 | 48 | this.projectBuilder = projectBuilder; |
| 156 | 48 | this.archiverManager = archiverManager; |
| 157 | 48 | this.inputHandler = inputHandler; |
| 158 | 48 | setLog( log ); |
| 159 | 48 | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public void execute() |
| 167 | |
throws MojoExecutionException, MojoFailureException |
| 168 | |
{ |
| 169 | 48 | if ( eclipseDir == null ) |
| 170 | |
{ |
| 171 | 0 | getLog().info( "Eclipse directory? " ); |
| 172 | |
|
| 173 | |
String eclipseDirString; |
| 174 | |
try |
| 175 | |
{ |
| 176 | 0 | eclipseDirString = inputHandler.readLine(); |
| 177 | |
} |
| 178 | 0 | catch ( IOException e ) |
| 179 | |
{ |
| 180 | 0 | throw new MojoExecutionException( "Unable to read from standard input", e ); |
| 181 | 0 | } |
| 182 | |
|
| 183 | 0 | eclipseDir = new File( eclipseDirString ); |
| 184 | |
} |
| 185 | |
|
| 186 | 48 | if ( eclipseDir.exists() && !eclipseDir.isDirectory() ) |
| 187 | |
{ |
| 188 | 0 | throw new MojoFailureException( "Invalid Eclipse directory: " + eclipseDir ); |
| 189 | |
} |
| 190 | 48 | else if ( !eclipseDir.exists() ) |
| 191 | |
{ |
| 192 | 0 | eclipseDir.mkdirs(); |
| 193 | |
} |
| 194 | |
|
| 195 | 48 | for ( Iterator it = artifacts.iterator(); it.hasNext(); ) |
| 196 | |
{ |
| 197 | 48 | Artifact artifact = (Artifact) it.next(); |
| 198 | |
|
| 199 | 48 | if ( pluginDependencyTypes.indexOf( artifact.getType() ) > -1 ) |
| 200 | |
{ |
| 201 | 44 | getLog().debug( "Processing Eclipse plugin dependency: " + artifact.getId() ); |
| 202 | |
|
| 203 | |
MavenProject project; |
| 204 | |
|
| 205 | |
try |
| 206 | |
{ |
| 207 | 44 | project = |
| 208 | |
projectBuilder.buildFromRepository( artifact, Collections.EMPTY_LIST, localRepository, true ); |
| 209 | |
} |
| 210 | 0 | catch ( ProjectBuildingException e ) |
| 211 | |
{ |
| 212 | 0 | throw new MojoExecutionException( "Failed to load project metadata (POM) for: " + artifact.getId(), |
| 213 | |
e ); |
| 214 | 44 | } |
| 215 | |
|
| 216 | 44 | install( artifact, project ); |
| 217 | |
} |
| 218 | |
else |
| 219 | |
{ |
| 220 | 4 | getLog().debug( |
| 221 | |
"Skipping dependency: " |
| 222 | |
+ artifact.getId() |
| 223 | |
+ ". Set pluginDependencyTypes with a comma-separated list of types to change this." ); |
| 224 | |
} |
| 225 | |
} |
| 226 | 48 | } |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
private void install( Artifact artifact, MavenProject project ) |
| 253 | |
throws MojoExecutionException, MojoFailureException |
| 254 | |
{ |
| 255 | 44 | if ( pluginsDir == null ) |
| 256 | |
{ |
| 257 | 44 | pluginsDir = new File( eclipseDir, "plugins" ); |
| 258 | |
} |
| 259 | |
|
| 260 | 44 | if ( !pluginsDir.exists() || !pluginsDir.isDirectory() ) |
| 261 | |
{ |
| 262 | 0 | throw new MojoFailureException( "Invalid Eclipse directory: " + eclipseDir |
| 263 | |
+ " (plugins directory is missing or not a directory)." ); |
| 264 | |
} |
| 265 | |
|
| 266 | 44 | boolean installAsJar = true; |
| 267 | |
|
| 268 | 44 | Properties properties = project.getProperties(); |
| 269 | 44 | if ( properties != null ) |
| 270 | |
{ |
| 271 | 44 | installAsJar = !Boolean.valueOf( properties.getProperty( PROP_UNPACK_PLUGIN, "false" ) ).booleanValue(); |
| 272 | |
} |
| 273 | |
|
| 274 | 44 | Attributes attributes = null; |
| 275 | |
try |
| 276 | |
{ |
| 277 | |
|
| 278 | 44 | JarFile jar = new JarFile( artifact.getFile(), false ); |
| 279 | 44 | Manifest manifest = jar.getManifest(); |
| 280 | 44 | if ( manifest == null ) |
| 281 | |
{ |
| 282 | 4 | getLog().debug( |
| 283 | |
"Ignoring " + artifact.getArtifactId() |
| 284 | |
+ " as it is does not have a Manifest (and so is not an OSGi bundle)" ); |
| 285 | 4 | return; |
| 286 | |
} |
| 287 | 40 | attributes = manifest.getMainAttributes(); |
| 288 | |
} |
| 289 | 0 | catch ( IOException e ) |
| 290 | |
{ |
| 291 | 0 | throw new MojoExecutionException( "Unable to read manifest of plugin " |
| 292 | |
+ artifact.getFile().getAbsolutePath(), e ); |
| 293 | 40 | } |
| 294 | |
|
| 295 | 40 | String pluginName = formatEclipsePluginName( artifact ); |
| 296 | |
|
| 297 | 40 | File pluginFile = new File( pluginsDir, pluginName + ".jar" ); |
| 298 | 40 | File pluginDir = new File( pluginsDir, pluginName ); |
| 299 | |
|
| 300 | 40 | boolean skipped = true; |
| 301 | |
|
| 302 | |
|
| 303 | 40 | Object bundleName = attributes.getValue( "Bundle-Name" ); |
| 304 | 40 | Object bundleSymbolicName = attributes.getValue( "Bundle-SymbolicName" ); |
| 305 | 40 | if ( bundleSymbolicName == null && bundleName == null ) |
| 306 | |
{ |
| 307 | 4 | getLog().debug( |
| 308 | |
"Ignoring " + artifact.getArtifactId() |
| 309 | |
+ " as it is not an OSGi bundle (no Bundle-SymbolicName or Bundle-Name in manifest)" ); |
| 310 | 4 | return; |
| 311 | |
} |
| 312 | |
|
| 313 | 36 | if ( overwrite ) |
| 314 | |
{ |
| 315 | 8 | if ( pluginFile.exists() || pluginDir.exists() ) |
| 316 | |
{ |
| 317 | 8 | getLog().warn( "Overwriting old plugin with contents of: " + artifact.getId() ); |
| 318 | |
|
| 319 | 8 | getLog().debug( "Removing old plugin from both: " + pluginFile + " and: " + pluginDir ); |
| 320 | |
|
| 321 | |
try |
| 322 | |
{ |
| 323 | 8 | FileUtils.forceDelete( pluginDir ); |
| 324 | 8 | FileUtils.forceDelete( pluginFile ); |
| 325 | |
} |
| 326 | 0 | catch ( IOException e ) |
| 327 | |
{ |
| 328 | 0 | throw new MojoExecutionException( "Failed to remove old plugin from: " + pluginFile + " or: " |
| 329 | |
+ pluginDir, e ); |
| 330 | 8 | } |
| 331 | |
|
| 332 | 8 | getLog().debug( "Removal of old plugin is complete; proceeding with plugin installation." ); |
| 333 | |
} |
| 334 | |
|
| 335 | 8 | performFileOperations( installAsJar, artifact, pluginFile, pluginDir ); |
| 336 | |
|
| 337 | 8 | skipped = false; |
| 338 | |
} |
| 339 | 28 | else if ( installAsJar && !pluginFile.exists() ) |
| 340 | |
{ |
| 341 | 24 | performFileOperations( installAsJar, artifact, pluginFile, pluginDir ); |
| 342 | |
|
| 343 | 24 | skipped = false; |
| 344 | |
} |
| 345 | 4 | else if ( !installAsJar && !pluginDir.exists() ) |
| 346 | |
{ |
| 347 | 4 | performFileOperations( installAsJar, artifact, pluginFile, pluginDir ); |
| 348 | |
|
| 349 | 4 | skipped = false; |
| 350 | |
} |
| 351 | |
|
| 352 | 36 | if ( skipped ) |
| 353 | |
{ |
| 354 | 0 | if ( installAsJar ) |
| 355 | |
{ |
| 356 | 0 | getLog().info( |
| 357 | |
"Skipping plugin installation for: " + artifact.getId() + "; file: " + pluginFile |
| 358 | |
+ " already exists. Set overwrite = true to override this." ); |
| 359 | |
} |
| 360 | 0 | else if ( !installAsJar ) |
| 361 | |
{ |
| 362 | 0 | getLog().info( |
| 363 | |
"Skipping plugin installation for: " + artifact.getId() + "; directory: " + pluginDir |
| 364 | |
+ " already exists. Set overwrite = true to override this." ); |
| 365 | |
} |
| 366 | |
} |
| 367 | 36 | } |
| 368 | |
|
| 369 | |
private void performFileOperations( boolean installAsJar, Artifact artifact, File pluginFile, File pluginDir ) |
| 370 | |
throws MojoExecutionException |
| 371 | |
{ |
| 372 | 36 | File artifactFile = artifact.getFile(); |
| 373 | |
|
| 374 | 36 | if ( installAsJar ) |
| 375 | |
{ |
| 376 | |
try |
| 377 | |
{ |
| 378 | 28 | getLog().debug( "Copying: " + artifact.getId() + " to: " + pluginFile ); |
| 379 | |
|
| 380 | 28 | FileUtils.copyFile( artifactFile, pluginFile ); |
| 381 | |
} |
| 382 | 0 | catch ( IOException e ) |
| 383 | |
{ |
| 384 | 0 | throw new MojoExecutionException( "Failed to copy Eclipse plugin: " + artifact.getId() + "\nfrom: " |
| 385 | |
+ artifact.getFile() + "\nto: " + pluginFile, e ); |
| 386 | 28 | } |
| 387 | |
} |
| 388 | |
else |
| 389 | |
{ |
| 390 | |
try |
| 391 | |
{ |
| 392 | 8 | getLog().debug( "Expanding: " + artifact.getId() + " into: " + pluginDir ); |
| 393 | |
|
| 394 | 8 | pluginDir.mkdirs(); |
| 395 | |
|
| 396 | 8 | UnArchiver unarchiver = archiverManager.getUnArchiver( artifactFile ); |
| 397 | |
|
| 398 | 8 | unarchiver.setSourceFile( artifactFile ); |
| 399 | 8 | unarchiver.setDestDirectory( pluginDir ); |
| 400 | 8 | unarchiver.extract(); |
| 401 | |
} |
| 402 | 0 | catch ( NoSuchArchiverException e ) |
| 403 | |
{ |
| 404 | 0 | throw new MojoExecutionException( "Could not find unarchiver for: " + artifactFile, e ); |
| 405 | |
} |
| 406 | 0 | catch ( ArchiverException e ) |
| 407 | |
{ |
| 408 | 0 | throw new MojoExecutionException( "Could not extract: " + artifactFile, e ); |
| 409 | |
} |
| 410 | 0 | catch ( IOException e ) |
| 411 | |
{ |
| 412 | 0 | throw new MojoExecutionException( "Could not extract: " + artifactFile, e ); |
| 413 | 8 | } |
| 414 | |
} |
| 415 | 36 | } |
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
private String formatEclipsePluginName( Artifact artifact ) |
| 424 | |
{ |
| 425 | 40 | return maven2OsgiConverter.getBundleSymbolicName( artifact ) + "_" |
| 426 | |
+ maven2OsgiConverter.getVersion( artifact.getVersion() ); |
| 427 | |
} |
| 428 | |
|
| 429 | |
} |