Example usage for org.apache.maven.artifact.installer DefaultArtifactInstaller install

List of usage examples for org.apache.maven.artifact.installer DefaultArtifactInstaller install

Introduction

In this page you can find the example usage for org.apache.maven.artifact.installer DefaultArtifactInstaller install.

Prototype

public void install(File source, Artifact artifact, ArtifactRepository localRepository)
            throws ArtifactInstallationException 

Source Link

Usage

From source file:org.apache.sling.ide.eclipse.m2e.EmbeddedArchetypeInstaller.java

License:Apache License

public void installArchetype() throws CoreException {
    try {/*from w  w w .  jav  a  2  s .c  o  m*/
        IMaven maven = MavenPlugin.getMaven();
        // first get the plexus container
        PlexusContainer container = ((MavenImpl) MavenPlugin.getMaven()).getPlexusContainer();

        // then get the DefaultMaven
        DefaultMaven mvn = (DefaultMaven) container.lookup(Maven.class);

        // now create a RepositorySystemSession
        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
        request.setLocalRepository(maven.getLocalRepository());

        // We need to support Maven 3.0.x as well, so we use reflection to
        // access Aether APIs in a manner which is compatible with all Maven 3.x versions
        // See https://maven.apache.org/docs/3.1.0/release-notes.html
        MavenSession session = reflectiveCreateMavenSession(container, mvn, request);
        LegacySupport legacy = container.lookup(LegacySupport.class);
        legacy.setSession(session);

        // then lookup the DefaultArtifactInstaller
        DefaultArtifactInstaller dai = (DefaultArtifactInstaller) container.lookup(ArtifactInstaller.class);

        final Set<Entry<String, InputStream>> entries = origins.entrySet();
        for (Iterator<Entry<String, InputStream>> it = entries.iterator(); it.hasNext();) {
            final Entry<String, InputStream> entry = it.next();
            final String fileExtension = entry.getKey();
            File tmpFile = File.createTempFile("slingClipseTmp", fileExtension);

            try (InputStream in = entry.getValue(); FileOutputStream fos = new FileOutputStream(tmpFile)) {
                IOUtils.copy(in, fos);
                // the below code uses the fileExtension as a type. Most of the time this is correct
                // and should be fine for our usage
                Artifact jarArtifact = new DefaultArtifact(groupId, artifactId, version, "", fileExtension, "",
                        new DefaultArtifactHandler(fileExtension));
                dai.install(tmpFile, jarArtifact, maven.getLocalRepository());
            } finally {
                FileUtils.deleteQuietly(tmpFile);
            }
        }

        Archetype archetype = new Archetype();
        archetype.setGroupId(groupId);
        archetype.setArtifactId(artifactId);
        archetype.setVersion(version);
        org.apache.maven.archetype.Archetype archetyper = MavenPluginActivator.getDefault().getArchetype();
        archetyper.updateLocalCatalog(archetype);
    } catch (CoreException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
    }
}