Example usage for org.apache.maven.project MavenProject addAttachedArtifact

List of usage examples for org.apache.maven.project MavenProject addAttachedArtifact

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject addAttachedArtifact.

Prototype

public void addAttachedArtifact(Artifact artifact) throws DuplicateArtifactAttachmentException 

Source Link

Document

Add or replace an artifact.

Usage

From source file:com.puppetlabs.geppetto.forge.maven.plugin.Package.java

License:Open Source License

@Override
protected void invoke(Diagnostic result) throws Exception {
    Collection<File> moduleRoots = findModuleRoots();
    if (moduleRoots.isEmpty()) {
        result.addChild(new Diagnostic(ERROR, PACKAGE, "No modules found in repository"));
        return;//from w  w w.  j  a v a  2 s  .  co m
    }

    File buildDir = getBuildDir();
    buildDir.mkdirs();
    if (moduleRoots.size() == 1) {
        MavenProject project = getProject();
        File moduleRoot = moduleRoots.iterator().next();
        Metadata[] resultingMetadata = new Metadata[1];
        byte[][] resultingMD5 = new byte[1][];
        project.getArtifact()
                .setFile(buildForge(moduleRoot, buildDir, resultingMetadata, resultingMD5, result));

        Artifact pmriArtifact = repositorySystem.createArtifact(project.getGroupId(), project.getArtifactId(),
                project.getVersion(), "compile", "pmri");

        PuppetModuleReleaseInfo pmri = new PuppetModuleReleaseInfo();
        pmri.setMetadata(resultingMetadata[0]);
        pmri.populate(moduleRoot);

        File pmriFile = new File(buildDir, "release.pmri");
        OutputStream out = new FileOutputStream(pmriFile);
        try {
            Writer writer = new BufferedWriter(new OutputStreamWriter(out, Charsets.UTF_8));
            getGson().toJson(pmri, writer);
            writer.flush();
        } finally {
            out.close();
        }
        pmriArtifact.setFile(pmriFile);
        pmriArtifact.setResolved(true);
        project.addAttachedArtifact(pmriArtifact);
    } else {
        File builtModules = new File(buildDir, "builtModules");
        if (!(builtModules.mkdir() || builtModules.isDirectory())) {
            result.addChild(
                    new Diagnostic(ERROR, PACKAGE, "Unable to create directory" + builtModules.getPath()));
            return;
        }
        for (File moduleRoot : moduleRoots)
            buildForge(moduleRoot, builtModules, null, null, result);
    }
}

From source file:org.fluidity.deployment.maven.DependenciesSupportImpl.java

License:Apache License

@Override
public void saveArtifact(final MavenProject project, final File file, final String finalName,
        final String classifier, final String packaging, final Logger log) throws MojoExecutionException {
    final boolean unclassified = classifier == null || classifier.isEmpty();
    final String outputName = unclassified ? String.format("%s.%s", finalName, packaging)
            : String.format("%s-%s.%s", finalName, classifier, packaging);

    final File outputFile = new File(outputName);
    final String outputPath = outputFile.getAbsolutePath();

    final Artifact artifact = project.getArtifact();
    assert artifact != null;

    final File artifactFile = artifact.getFile();

    if (artifactFile != null && artifactFile.getAbsolutePath().equals(outputPath)) {
        log.info(String.format("Replacing %s: %s", packaging, artifactFile.getAbsolutePath()));

        if (!artifactFile.delete()) {
            throw new MojoExecutionException(String.format("Could not delete %s", artifactFile));
        }/*from  w  w  w  .  jav a2 s .  c o m*/

        if (!file.renameTo(artifactFile)) {
            throw new MojoExecutionException(String.format("Could not create %s", artifactFile));
        }
    } else {
        boolean replacing = false;

        for (final Artifact attached : project.getAttachedArtifacts()) {
            if (attached.getFile().getAbsolutePath().equals(outputPath)) {
                replacing = true;
                break;
            }
        }

        log.info(String.format("%s %s: %s", replacing ? "Replacing" : "Saving", packaging, outputPath));

        if (outputFile.exists() && !outputFile.delete()) {
            throw new MojoExecutionException(String.format("Could not delete %s", outputFile));
        }

        if (!file.renameTo(outputFile)) {
            throw new MojoExecutionException(String.format("Could not create %s", outputFile));
        }

        if (!replacing) {
            final DefaultArtifact attachment = new DefaultArtifact(project.getGroupId(),
                    project.getArtifactId(), project.getVersion(), artifact.getScope(), packaging, classifier,
                    artifact.getArtifactHandler());
            attachment.setFile(outputFile);

            project.addAttachedArtifact(attachment);
        }
    }
}

From source file:org.renjin.gcc.maven.GccBridgeHelper.java

License:Open Source License

public static void archiveHeaders(Log log, MavenProject project, File... includeDirectories)
        throws MojoExecutionException {

    File outputDir = new File(project.getBuild().getDirectory());
    File archiveFile = new File(outputDir, project.getBuild().getFinalName() + "-headers.jar");

    ensureDirExists(outputDir);/*from  www  .j  a  v a 2 s  .com*/

    try (JarOutputStream output = new JarOutputStream(new FileOutputStream(archiveFile))) {
        for (File includeDirectory : includeDirectories) {
            if (includeDirectory.exists()) {
                log.info("Archiving headers from " + includeDirectory.getAbsolutePath());
                archiveFiles(output, includeDirectory, "");
            }
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to create headers archive", e);
    }

    Artifact artifact = new AttachedArtifact(project.getArtifact(), "jar", "headers",
            new HeaderArtifactHandler());
    artifact.setFile(archiveFile);
    artifact.setResolved(true);

    project.addAttachedArtifact(artifact);
}