Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 384b7b1795685a5b961231eb11d922632522ee0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.editor.xml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
import org.codehaus.plexus.util.IOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;

class PomTemplateContextUtil {
  private static final Logger log = LoggerFactory.getLogger(PomTemplateContextUtil.class);

  public static final PomTemplateContextUtil INSTANCE = new PomTemplateContextUtil();

  private final Map<String, PluginDescriptor> descriptors = new HashMap<String, PluginDescriptor>();

  public PluginDescriptor getPluginDescriptor(String groupId, String artifactId, String version) {
    String name = groupId + ":" + artifactId + ":" + version;
    PluginDescriptor descriptor = descriptors.get(name);
    if(descriptor!=null) {
      return descriptor;
    }
    
    MavenPlugin plugin = MavenPlugin.getDefault();
    try {
      IMaven embedder = MavenPlugin.getDefault().getMaven();

      List<ArtifactRepository> repositories = embedder.getArtifactRepositories();

      Artifact artifact = embedder.resolve(groupId, artifactId, version, "maven-plugin", null,  repositories, null); //$NON-NLS-1$

      File file = artifact.getFile();
      if(file == null) {
        String msg = "Can't resolve plugin " + name; //$NON-NLS-1$
        log.error(msg);
      } else {
        InputStream is = null;
        ZipFile zf = null;
        try {
          zf = new ZipFile(file);
          ZipEntry entry = zf.getEntry("META-INF/maven/plugin.xml"); //$NON-NLS-1$
          if(entry != null) {
            is = zf.getInputStream(entry);
            PluginDescriptorBuilder builder = new PluginDescriptorBuilder();
            descriptor = builder.build(new InputStreamReader(is));
            descriptors.put(name, descriptor);
            return descriptor;
          }
        } catch(Exception ex) {
          String msg = "Can't read configuration for " + name; //$NON-NLS-1$
          log.error(msg, ex);
        } finally {
          IOUtil.close(is);
          try {
            zf.close();
          } catch(IOException ex) {
            // ignore
          }
        }
      }

    } catch(CoreException ex) {
      IStatus status = ex.getStatus();
      String msg = status.getMessage();
      if(status.getException() != null) {
        msg += "; " + status.getException().getMessage();
      }
      log.error(msg, ex);
    }
    return null;
  }
}

Back to the top