Example usage for org.apache.maven.artifact ArtifactUtils copyArtifacts

List of usage examples for org.apache.maven.artifact ArtifactUtils copyArtifacts

Introduction

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

Prototype

public static <K, T extends Map<K, Artifact>> T copyArtifacts(Map<K, ? extends Artifact> from, T to) 

Source Link

Usage

From source file:org.sourcepit.tools.mojo.DeployCopiedArtifactMojo.java

License:Apache License

private void deployProject(DeployRequest request) throws MojoExecutionException, MojoFailureException {
    Artifact artifact = ArtifactUtils.copyArtifact(request.getProject().getArtifact());

    String packaging = request.getProject().getPackaging();
    File pomFile = request.getProject().getFile();

    @SuppressWarnings("unchecked")
    List<Artifact> attachedArtifacts = new ArrayList<Artifact>();
    ArtifactUtils.copyArtifacts(request.getProject().getAttachedArtifacts(), attachedArtifacts);

    ArtifactRepository repo = getDeploymentRepository(request.getProject(),
            request.getAltDeploymentRepository(), request.getAltReleaseDeploymentRepository(),
            request.getAltSnapshotDeploymentRepository());

    String protocol = repo.getProtocol();

    if (protocol.equalsIgnoreCase("scp")) {
        File sshFile = new File(System.getProperty("user.home"), ".ssh");

        if (!sshFile.exists()) {
            sshFile.mkdirs();//  w  w  w .  j  a  v  a  2  s. co m
        }
    }

    // Deploy the POM
    boolean isPomArtifact = "pom".equals(packaging);
    if (!isPomArtifact) {
        ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact, pomFile);
        artifact.addMetadata(metadata);
    }

    if (request.isUpdateReleaseInfo()) {
        artifact.setRelease(true);
    }

    int retryFailedDeploymentCount = request.getRetryFailedDeploymentCount();

    try {
        if (isPomArtifact) {
            deploy(pomFile, artifact, repo, getLocalRepository(), retryFailedDeploymentCount);
        } else {
            File file = artifact.getFile();

            if (file != null && file.isFile()) {
                deploy(file, artifact, repo, getLocalRepository(), retryFailedDeploymentCount);
            } else if (!attachedArtifacts.isEmpty()) {
                getLog().info("No primary artifact to deploy, deploying attached artifacts instead.");

                Artifact pomArtifact = artifactFactory.createProjectArtifact(artifact.getGroupId(),
                        artifact.getArtifactId(), artifact.getBaseVersion());
                pomArtifact.setFile(pomFile);
                if (request.isUpdateReleaseInfo()) {
                    pomArtifact.setRelease(true);
                }

                deploy(pomFile, pomArtifact, repo, getLocalRepository(), retryFailedDeploymentCount);

                // propagate the timestamped version to the main artifact for the attached artifacts to pick it up
                artifact.setResolvedVersion(pomArtifact.getVersion());
            } else {
                String message = "The packaging for this project did not assign a file to the build artifact";
                throw new MojoExecutionException(message);
            }
        }

        for (Artifact attached : attachedArtifacts) {
            deploy(attached.getFile(), attached, repo, getLocalRepository(), retryFailedDeploymentCount);
        }
    } catch (ArtifactDeploymentException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}