package io.takari.maven.plugins;

import io.takari.maven.plugins.util.AetherUtils;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Server;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.repository.Authentication;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RemoteRepository.Builder;
import org.eclipse.aether.util.artifact.SubArtifact;
import org.eclipse.aether.util.repository.AuthenticationBuilder;

/**
 * @author Jason van Zyl
 */
@Mojo(name = "deploy", defaultPhase = LifecyclePhase.DEPLOY)
public class Deploy extends TakariLifecycleMojo {

  // TODO deploy at the end to prevent corruption

  // polyglot conversion to detect the project type and convert on the way out the door
  // how to help people fork projects and deploy elsewhere: the alt deployment repo may have issues
  // - a standard property to replace in a conventional way?

  @Override
  public void executeMojo() throws MojoExecutionException {
    installProject(project);
  }

  private void installProject(MavenProject project) throws MojoExecutionException {

    DeployRequest deployRequest = new DeployRequest();

    //
    // Primary artifact
    //
    Artifact artifact = AetherUtils.toArtifact(project.getArtifact());
    deployRequest.addArtifact(artifact);

    //
    // POM
    //
    Artifact pomArtifact = new SubArtifact(artifact, "", "pom");
    pomArtifact = pomArtifact.setFile(project.getFile());
    deployRequest.addArtifact(pomArtifact);

    //
    // Attached artifacts
    //
    for (org.apache.maven.artifact.Artifact attachedArtifact : project.getAttachedArtifacts()) {
      deployRequest.addArtifact(AetherUtils.toArtifact(attachedArtifact));
    }

    deployRequest.setRepository(remoteRepository(project));

    try {
      repositorySystem.deploy(repositorySystemSession, deployRequest);
    } catch (DeploymentException e) {
      throw new MojoExecutionException(e.getMessage(), e);
    }
  }

  //
  // All this logic about finding the right repository needs to be standardized
  //
  public RemoteRepository remoteRepository(MavenProject project) {

    // alternate repository
    // make it easy to fork and deploy
    // offline

    DeploymentRepository deploymentRepository;

    if (ArtifactUtils.isSnapshot(project.getVersion())) {
      deploymentRepository = project.getDistributionManagement().getSnapshotRepository();
    } else {
      deploymentRepository = project.getDistributionManagement().getRepository();
    }

    Builder remoteRepositoryBuilder =
        new RemoteRepository.Builder(deploymentRepository.getId(), "default",
            deploymentRepository.getUrl());

    Server server = settings.getServer(deploymentRepository.getId());
    if (server != null) {
      if (server.getUsername() != null && server.getPassword() != null) {
        Authentication authentication = new AuthenticationBuilder() //
            .addUsername(server.getUsername()) //
            .addPassword(server.getPassword()) //
            .build();
        remoteRepositoryBuilder.setAuthentication(authentication);
      }
    }

    return remoteRepositoryBuilder.build();
  }
}

@@ -81,108 +81,8 @@
 s;%0A%0A
-import org.apache.maven.artifact.ArtifactUtils;%0Aimport org.apache.maven.model.DeploymentRepository;%0A
 impo
@@ -292,49 +292,8 @@
 ct;%0A
-import org.apache.maven.settings.Server;%0A
 impo
@@ -447,61 +447,8 @@
 on;%0A
-import org.eclipse.aether.repository.Authentication;%0A
 impo
@@ -528,161 +528,33 @@
 her.
-repository.RemoteRepository.Builder;%0Aimport org.eclipse.aether.util.artifact.SubArtifact;%0Aimport org.eclipse.aether.util.repository.AuthenticationBuilder
+util.artifact.SubArtifact
 ;%0A%0A/
@@ -2219,1037 +2219,91 @@
 ) %7B%0A
-%0A
     
-// alternate repository%0A    // make it easy to fork and deploy%0A    // offline%0A%0A    DeploymentRepository deploymentRepository;%0A%0A    if (ArtifactUtils.isSnapshot(project.getVersion())) %7B%0A      deploymentRepository = project.getDistributionManagement().getSnapshotRepository();%0A    %7D else %7B%0A      deploymentRepository = project.getDistributionManagement().getRepository();%0A    %7D%0A%0A    Builder remoteRepositoryBuilder =%0A        new RemoteRepository.Builder(deploymentRepository.getId(), %22default%22,%0A            deploymentRepository.getUrl());%0A%0A    Server server = settings.getServer(deploymentRepository.getId());%0A    if (server != null) %7B%0A      if (server.getUsername() != null && server.getPassword() != null) %7B%0A        Authentication authentication = new AuthenticationBuilder() //%0A            .addUsername(server.getUsername()) //%0A            .addPassword(server.getPassword()) //%0A            .build();%0A        remoteRepositoryBuilder.setAuthentication(authentication);%0A      %7D%0A    %7D%0A%0A    return remoteRepositoryBuilder.build(
+return AetherUtils.toRepo(project.getDistributionManagementArtifactRepository()
 );%0A 

Use MavenProject#getDistributionManagementArtifactRepository during deploy