Example usage for org.apache.maven.artifact DefaultArtifact DefaultArtifact

List of usage examples for org.apache.maven.artifact DefaultArtifact DefaultArtifact

Introduction

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

Prototype

public DefaultArtifact(String groupId, String artifactId, VersionRange versionRange, String scope, String type,
            String classifier, ArtifactHandler artifactHandler) 

Source Link

Usage

From source file:ch.sourcepond.maven.plugin.repobuilder.AdditionalArtifact.java

License:Apache License

/**
 * @return// ww w. j  av a2  s . c  om
 * @throws MojoExecutionException
 */
Artifact toArtifact() throws MojoExecutionException {
    final DefaultArtifactHandler handler = new DefaultArtifactHandler();
    String extension = this.extension;
    if (isBlank(extension)) {
        extension = getFileExtension(file.getAbsolutePath());
    }

    if (isBlank(extension)) {
        throw new MojoExecutionException(
                "No extension specified; either add an extension to the artifact-file, or, specify parameter 'extension'! Invalid config item: "
                        + this);
    }

    handler.setExtension(extension);
    final Artifact artifact = new DefaultArtifact(groupId, artifactId, version, "", handler.getExtension(),
            classifier, handler);
    artifact.setFile(file);
    return artifact;
}

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 {/*  w ww .  j a v a 2 s  .c om*/
        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 .  j  av a 2  s. co m
}

From source file:com.barchart.jenkins.cascade.PluginUtilities.java

License:BSD License

/**
 * Build maven artifact from maven dependency.
 *///  w ww  .  j av a 2  s  .c  o  m
public static Artifact mavenArtifact(final Dependency dependency) {
    return new DefaultArtifact( //
            dependency.getGroupId(), //
            dependency.getArtifactId(), //
            dependency.getVersion(), //
            null, // scope
            dependency.getType(), // type
            "", // classifier
            null // handler
    );
}

From source file:com.barchart.jenkins.cascade.PluginUtilities.java

License:BSD License

/**
 * Build maven artifact from maven model.
 *///from   w w w. j  a v  a2 s.c o m
public static Artifact mavenArtifact(final Model model) {
    return new DefaultArtifact( //
            model.getGroupId(), //
            model.getArtifactId(), //
            model.getVersion(), //
            null, // scope
            model.getPackaging(), // type
            "", // classifier
            null // handler
    );
}

From source file:com.barchart.jenkins.cascade.PluginUtilities.java

License:BSD License

/**
 * Build maven artifact from maven parent.
 */// ww  w .j a v a 2s  .  com
public static Artifact mavenArtifact(final Parent parent) {
    return new DefaultArtifact( //
            parent.getGroupId(), //
            parent.getArtifactId(), //
            parent.getVersion(), //
            null, // scope
            "pom", // type
            "", // classifier
            null // handler
    );
}

From source file:com.bugvm.maven.plugin.AbstractBugVMMojo.java

License:Apache License

protected Artifact resolveBugVMDistArtifact() throws MojoExecutionException {

    MavenArtifactHandler handler = new MavenArtifactHandler("tar.gz");
    Artifact artifact = new DefaultArtifact("com.bugvm", "bugvm-dist", getBugVMVersion(), "", "tar.gz", null,
            handler);//from   w w w  . j  a va 2 s .  co  m
    return resolveArtifact(artifact);
}

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

License:Apache License

public Set<Artifact> translate(Set<Artifact> artifacts) {
    Set<Artifact> results;//from  ww  w  .  ja  v a 2  s.c om

    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.encodedknowledge.maven.dependency.sanity.ArtifactTestUtils.java

License:Apache License

public static Artifact createArtifact(String artifactString) {
    String[] parts = artifactString.split(":");
    if (parts.length < 3 || parts.length > 4) {
        throw new IllegalArgumentException(
                "artifactString must be in the format groupId:artifactId:version[:scope]");
    }/*from w  ww .  j a  v a  2s  .  c o  m*/
    String groupId = parts[0];
    String artifactId = parts[1];
    String version = parts[2];
    String scope = parts.length > 3 ? parts[3] : "compile";
    return new DefaultArtifact(groupId, artifactId, VersionRange.createFromVersion(version), scope, "jar", "",
            null);
}

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  w w . jav a2 s.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);
        }
    }
}