Example usage for org.apache.maven.artifact.handler DefaultArtifactHandler DefaultArtifactHandler

List of usage examples for org.apache.maven.artifact.handler DefaultArtifactHandler DefaultArtifactHandler

Introduction

In this page you can find the example usage for org.apache.maven.artifact.handler DefaultArtifactHandler DefaultArtifactHandler.

Prototype

public DefaultArtifactHandler(String type) 

Source Link

Usage

From source file:com.ariatemplates.attester.maven.ArtifactExtractor.java

License:Apache License

public String inplaceExtractDependency(ArtifactRepository localRepository, Dependency dependency)
        throws Exception {
    Artifact artifact = new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(),
            dependency.getVersion(), dependency.getScope(), dependency.getType(), dependency.getClassifier(),
            new DefaultArtifactHandler("zip"));
    artifact = localRepository.find(artifact);
    File zipFile = artifact.getFile();
    File outputDirectory = getOutputDirectory(artifact);
    if (outputDirectory.exists()) {
        // if the directory already exists, don't touch it (by the way, it
        // may currently be in use by some other process)
        getLog().info("Artifact " + artifact + " is already extracted in " + outputDirectory);
    } else {//from  w  w w.  ja  va 2 s  .c o m
        getLog().info("Extracting artifact " + artifact + " to " + outputDirectory);
        File tempDirectory = null;
        try {
            // temporary directory where to extract
            tempDirectory = File.createTempFile("extract", null, outputDirectory.getParentFile());
            tempDirectory.delete(); // delete the file created by
                                    // createTempFile
                                    // (to replace it with a directory)
            unzip(zipFile, tempDirectory);
            // Move the temporary directory to its final location
            if (outputDirectory.exists()) {
                // the output directory was created in the mean time
                // (probably by another build running in parallel)
                getLog().info("Another process probably extracted the artifact at the same time.");
            } else if (!tempDirectory.renameTo(outputDirectory)) {
                throw new Exception(
                        "Failed to rename directory " + tempDirectory + " to " + outputDirectory + ".");
            }
        } catch (Exception e) {
            getLog().error("An exception occurred while extracting the " + artifact + " artifact.", e);
            throw e;
        } finally {
            if (tempDirectory != null && tempDirectory.exists()) {
                getLog().info("Deleting temporary directory " + tempDirectory);
                FileUtils.deleteQuietly(tempDirectory);
            }
        }
    }
    return outputDirectory.getAbsolutePath();
}

From source file:com.ariatemplates.attester.maven.RunNode.java

License:Apache License

protected void findNodeExecutable() {
    if (nodejsPath == null) {
        Artifact nodeArtifact = new DefaultArtifact("org.nodejs", "node", "0.10.32", "runtime", "exe", "win32",
                new DefaultArtifactHandler("exe"));
        nodeArtifact = session.getLocalRepository().find(nodeArtifact);
        nodejsPath = nodeArtifact.getFile();
    }//from  w  w w  .ja v a 2 s. c  o m
}

From source file:com.coderplus.apacheutils.translators.ClassifierTypeTranslator.java

License:Apache License

public Set<Artifact> translate(Set<Artifact> artifacts) {
    Set<Artifact> results;//from w w w  .j ava  2  s  .c  o  m

    results = new HashSet<Artifact>();
    for (Artifact artifact : artifacts) {
        // this translator must pass both type and classifier here so we
        // will use the
        // base artifact value if null comes in
        String useType;
        if (StringUtils.isNotEmpty(this.type)) {
            useType = this.type;
        } else {
            useType = artifact.getType();
        }

        String useClassifier;
        if (StringUtils.isNotEmpty(this.classifier)) {
            useClassifier = this.classifier;
        } else {
            useClassifier = artifact.getClassifier();
        }

        // Create a new artifact
        Artifact newArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(),
                artifact.getVersion(), Artifact.SCOPE_COMPILE, useType, useClassifier,
                new DefaultArtifactHandler(useType));

        // note the new artifacts will always have the scope set to null. We
        // should
        // reset it here so that it will pass other filters if needed
        newArtifact.setScope(artifact.getScope());

        results.add(newArtifact);
    }

    return results;
}

From source file:com.github.batkinson.plugins.PackMojo.java

License:Apache License

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {

    getLog().debug("starting pack");

    Packer packer = Pack200.newPacker();
    Unpacker unpacker = null;/*from w  ww.  j  av a2s . c om*/

    if (normalizeOnly)
        unpacker = Pack200.newUnpacker();

    String type = "jar.pack";
    if (compress)
        type = "jar.pack.gz";

    Map<String, String> packerProps = packer.properties();

    packerProps.put(Packer.EFFORT, effort);
    packerProps.put(Packer.SEGMENT_LIMIT, segmentLimit);
    packerProps.put(Packer.KEEP_FILE_ORDER, keepFileOrder);
    packerProps.put(Packer.MODIFICATION_TIME, modificationTime);
    packerProps.put(Packer.DEFLATE_HINT, deflateHint);

    if (stripCodeAttributes != null) {
        for (String attributeName : stripCodeAttributes) {
            getLog().debug("stripping " + attributeName);
            packerProps.put(Packer.CODE_ATTRIBUTE_PFX + attributeName, Packer.STRIP);
        }
    }

    if (failOnUnknownAttributes)
        packerProps.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);

    List<Artifact> artifactsToPack = new ArrayList<Artifact>();

    if (processMainArtifact)
        artifactsToPack.add(project.getArtifact());

    if (includeClassifiers != null && !includeClassifiers.isEmpty()) {
        for (Artifact secondaryArtifact : (List<Artifact>) project.getAttachedArtifacts())
            if ("jar".equals(secondaryArtifact.getType()) && secondaryArtifact.hasClassifier()
                    && includeClassifiers.contains(secondaryArtifact.getClassifier()))
                artifactsToPack.add(secondaryArtifact);
    }

    String action = normalizeOnly ? "normalizing" : "packing";

    getLog().info(action + " " + artifactsToPack.size() + " artifacts");

    for (Artifact artifactToPack : artifactsToPack) {

        File origFile = artifactToPack.getFile();
        File packFile = new File(origFile.getAbsolutePath() + ".pack");
        File zipFile = new File(packFile.getAbsolutePath() + ".gz");

        try {
            JarFile jar = new JarFile(origFile);
            FileOutputStream fos = new FileOutputStream(packFile);

            getLog().debug("packing " + origFile + " to " + packFile);
            packer.pack(jar, fos);

            getLog().debug("closing handles ...");
            jar.close();
            fos.close();

            if (normalizeOnly) {
                getLog().debug("unpacking " + packFile + " to " + origFile);
                JarOutputStream origJarStream = new JarOutputStream(new FileOutputStream(origFile));
                unpacker.unpack(packFile, origJarStream);

                getLog().debug("closing handles...");
                origJarStream.close();

                getLog().debug("unpacked file");
            } else {

                Artifact newArtifact = new DefaultArtifact(artifactToPack.getGroupId(),
                        artifactToPack.getArtifactId(), artifactToPack.getVersionRange(),
                        artifactToPack.getScope(), type, artifactToPack.getClassifier(),
                        new DefaultArtifactHandler(type));

                if (compress) {
                    getLog().debug("compressing " + packFile + " to " + zipFile);
                    GZIPOutputStream zipOut = new GZIPOutputStream(new FileOutputStream(zipFile));
                    IOUtil.copy(new FileInputStream(packFile), zipOut);
                    zipOut.close();
                    newArtifact.setFile(zipFile);
                } else {
                    newArtifact.setFile(packFile);
                }

                if (attachPackedArtifacts) {
                    getLog().debug("attaching " + newArtifact);
                    project.addAttachedArtifact(newArtifact);
                }
            }

            getLog().debug("finished " + action + " " + packFile);

        } catch (IOException e) {
            throw new MojoExecutionException("Failed to pack jar.", e);
        }
    }
}

From source file:com.googlecode.t7mp.steps.deployment.AbstractDeploymentStep.java

License:Apache License

protected List<AbstractArtifact> resolveArtifacts(List<? extends AbstractArtifact> artifacts) {
    List<AbstractArtifact> resolvedArtifacts = Lists.newArrayList();
    for (AbstractArtifact abstractArtifact : artifacts) {
        context.getLog().debug("Resolve artifact for " + abstractArtifact.toString());
        if (!abstractArtifact.getGroupId().equals("local")) {
            Artifact artifact;/*  w  ww  . j av  a 2s.c  om*/
            try {
                artifact = myArtifactResolver.resolve(abstractArtifact.getGroupId(),
                        abstractArtifact.getArtifactId(), abstractArtifact.getVersion(),
                        abstractArtifact.getClassifier(), abstractArtifact.getType(), Artifact.SCOPE_COMPILE);
            } catch (MojoExecutionException e) {
                throw new TomcatSetupException(e.getMessage(), e);
            }
            abstractArtifact.setArtifact(artifact);
            resolvedArtifacts.add(abstractArtifact);
        } else {
            Artifact artifact = new DefaultArtifact(abstractArtifact.getGroupId(),
                    abstractArtifact.getArtifactId(),
                    VersionRange.createFromVersion(abstractArtifact.getVersion()), Artifact.SCOPE_COMPILE,
                    abstractArtifact.getType(), null, new DefaultArtifactHandler("jar"), false);
            String resourceName = "/com/googlecode/t7mp/repo/" + abstractArtifact.getArtifactId() + "/"
                    + abstractArtifact.getVersion() + "/" + abstractArtifact.getArtifactId() + "-"
                    + abstractArtifact.getVersion() + ".jar";
            BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream(resourceName));
            File tempFile;
            try {
                tempFile = File.createTempFile("local_Artifact", ".maven.tmp");
                tempFile.deleteOnExit();
                IOUtils.copy(bis, new FileOutputStream(tempFile));
                artifact.setFile(tempFile);
                abstractArtifact.setArtifact(artifact);
                resolvedArtifacts.add(abstractArtifact);
            } catch (FileNotFoundException e) {
                throw new TomcatSetupException(e.getMessage(), e);
            } catch (IOException e) {
                throw new TomcatSetupException(e.getMessage(), e);
            }
        }
    }
    return resolvedArtifacts;
}

From source file:com.maestrodev.plugins.collabnet.FrsCopyWorker.java

License:Apache License

private String copyArtifact(FrsSession frsSession, String releaseId)
        throws MalformedURLException, RemoteException {
    Artifact artifact = new DefaultArtifact(artifactGroupId, artifactId,
            VersionRange.createFromVersion(artifactVersion), null, artifactType, artifactClassifier,
            new DefaultArtifactHandler(artifactType));
    String path = new DefaultRepositoryLayout().pathOf(artifact);
    String url = repositoryUrl + "/" + path;

    String filename = this.filename;
    if (StringUtils.isBlank(filename)) {
        filename = path.substring(path.lastIndexOf('/') + 1);
    }//from   w w w.j  a  va2  s.  c o  m

    String msg = "Uploading '" + url + "' to release '" + releaseId + "' as '" + filename + "'";
    logger.debug(msg);
    writeOutput(msg + "\n");

    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(repositoryUsername, repositoryPassword.toCharArray());
        }
    });

    return frsSession.uploadFileFromUrl(releaseId, new URL(url), filename, overwrite);
}

From source file:com.rebaze.maven.support.AetherUtils.java

License:Open Source License

public static ArtifactHandler newHandler(Artifact artifact) {
    String type = artifact.getProperty(ArtifactProperties.TYPE, artifact.getExtension());
    DefaultArtifactHandler handler = new DefaultArtifactHandler(type);
    handler.setExtension(artifact.getExtension());
    handler.setLanguage(artifact.getProperty(ArtifactProperties.LANGUAGE, null));
    handler.setAddedToClasspath(//from www .j ava2  s. com
            Boolean.parseBoolean(artifact.getProperty(ArtifactProperties.CONSTITUTES_BUILD_PATH, "")));
    handler.setIncludesDependencies(
            Boolean.parseBoolean(artifact.getProperty(ArtifactProperties.INCLUDES_DEPENDENCIES, "")));
    return handler;
}

From source file:de.berlios.jsunit.maven2.stub.SimpleMavenProjectStub.java

License:Apache License

/**
 * Create an artifact/*from w  w w  .ja v a  2  s  . c o  m*/
 * 
 * @param groupId the groupId
 * @param artifactId the artifactId
 * @param version the version
 * @param type the type
 * @param classifier the classifier
 * @param file the file itself
 * @return the artifact
 */
protected Artifact createArtifact(final String groupId, final String artifactId, final String version,
        final String type, final String classifier, final File file) {
    final Artifact artifact = new DefaultArtifact(groupId, artifactId, VersionRange.createFromVersion(version),
            "compile", type, classifier, new DefaultArtifactHandler(type));
    if (file != null) {
        artifact.setFile(file);
    }

    return artifact;
}

From source file:de.ecsec.maven.plugins.PackMojo.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException {
    getLog().debug("starting pack");

    init();//from w  ww .ja  v  a 2 s . co m

    int numArtifacts = artifacts.size();
    getLog().info("packing " + numArtifacts + " artifact" + (numArtifacts == 0 || numArtifacts > 1 ? "s" : ""));

    for (Artifact artifactToPack : artifacts) {
        List<Artifact> artifactList = new ArrayList<>();
        Artifact artifact;
        File origFile = artifactToPack.getFile();
        File packFile = new File(origFile.getAbsolutePath() + PACK200_FILE_ENDING);

        try {
            JarFile jar = new JarFile(origFile);
            try (FileOutputStream fos = new FileOutputStream(packFile)) {
                getLog().debug("packing " + origFile + " to " + packFile);
                packer.pack(jar, fos);
            }

            if (packAndGzip) {
                File packGzipFile = new File(origFile.getAbsolutePath() + PACK200_GZIP_FILE_ENDING);
                try (FileInputStream fis = new FileInputStream(packFile)) {
                    try (GZIPOutputStream gzipOut = new GZIPOutputStream(new FileOutputStream(packGzipFile))) {
                        getLog().debug("compressing " + packFile + " to " + packGzipFile);
                        IOUtil.copy(fis, gzipOut);
                    }
                }

                getLog().debug("removing " + packFile);
                packFile.delete();

                artifact = new DefaultArtifact(artifactToPack.getGroupId(), artifactToPack.getArtifactId(),
                        artifactToPack.getVersionRange(), artifactToPack.getScope(), PACK200_GZIP_TYPE,
                        artifactToPack.getClassifier(), new DefaultArtifactHandler(PACK200_GZIP_TYPE));

                artifact.setFile(packGzipFile);
            } else {
                artifact = new DefaultArtifact(artifactToPack.getGroupId(), artifactToPack.getArtifactId(),
                        artifactToPack.getVersionRange(), artifactToPack.getScope(), PACK200_TYPE,
                        artifactToPack.getClassifier(), new DefaultArtifactHandler(PACK200_TYPE));

                artifact.setFile(packFile);
            }

            artifactList.add(artifact);

            for (Artifact artifactToAttach : artifactList) {
                getLog().debug("attaching " + artifactToAttach);
                project.addAttachedArtifact(artifactToAttach);
            }

            getLog().debug("finished packing " + packFile);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to pack jar", e);
        }
    }
}

From source file:de.hwbllmnn.maven.JSSourceArchiveMojo.java

License:GNU General Public License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    File dir = new File(project.getBasedir(), "src/main/javascript");
    ZipOutputStream out = null;//from w  w  w.  j  a  va2s  .c o  m
    AttachedArtifact artifact = new AttachedArtifact(project.getArtifact(), "jslib", "source",
            new DefaultArtifactHandler("jslib"));

    List<File> list = new LinkedList<File>();

    for (Object o : project.getDependencyArtifacts()) {
        Artifact a = (Artifact) o;
        if (a.getType().equals("jslib")) {
            Artifact source = artifactFactory.createArtifactWithClassifier(a.getGroupId(), a.getArtifactId(),
                    a.getVersion(), "jslib", "source");
            try {
                artifactResolver.resolve(source, project.getRemoteArtifactRepositories(), localRepository);
            } catch (ArtifactResolutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ArtifactNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            list.add(source.getFile());
        }
    }

    File target = new File(project.getBasedir(), "target");
    if (!target.exists() && !target.mkdirs()) {
        throw new MojoFailureException("Could not create target directory!");
    }
    File file = new File(project.getBasedir(),
            "target/" + project.getArtifactId() + "-" + project.getVersion() + "-source.jslib");

    artifact.setFile(file);
    project.addAttachedArtifact(artifact);
    try {
        out = new ZipOutputStream(new FileOutputStream(file));
        zip(dir, out, dir.toURI());
        for (File f : list) {
            File tmp = new File(target, f.getName());
            FileInputStream in = new FileInputStream(f);
            try {
                unzip(in, tmp);
                zip(tmp, out, tmp.getAbsoluteFile().toURI());
            } finally {
                closeQuietly(in);
            }
        }
    } catch (IOException e) {
        getLog().error(e);
    } finally {
        closeQuietly(out);
    }
}