/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package npanday.plugin;

import org.apache.maven.artifact.deployer.ArtifactDeployer;
import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import npanday.artifact.ApplicationConfig;

import java.io.File;

/**
 * Deploys the .NET assembly to the remote repository
 *
 * @author Evan Worley
 * @author Zak Jacobson
 * @goal deploy
 * @phase deploy
 * @description Deploys the .NET assembly to the remote repository
 */
public class DeployMojo
    extends AbstractMojo
{

    /**
     * The maven project.
     *
     * @parameter expression="${project}"
     * @required
     */
    private MavenProject project;

    /**
     * The local maven repository
     *
     * @parameter expression="${localRepository}"
     * @required
     */
    private ArtifactRepository localRepo;

    /**
     * @component
     */
    private ArtifactDeployer artifactDeployer;

    /**
     * The project packaging type
     *
     * @parameter expression="${project.packaging}"
     * @required
     * @readonly
     */
    private String packaging;

    /**
     * The project classifier type
     *
     * @parameter
     */
    private String classifier;

    /**
     * @component
     */
    private ArtifactFactory artifactFactory;

    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        Artifact projectArtifact = project.getArtifact();

        if ( ! "pom".equals( packaging ) )
        {
            projectArtifact.addMetadata( new ProjectArtifactMetadata( projectArtifact, project.getFile() ) );
        }

        ApplicationConfig config = ApplicationConfig.Factory.createDefaultApplicationConfig( project.getArtifact(),
                                                                                             project.getBasedir(),
                                                                                             new File(
                                                                                                 project.getBuild().getDirectory() ) );
        File exePath = config.getRepositoryPath( new File( localRepo.getBasedir() ) );
        if ( exePath.exists() )
        {
            Artifact attachedArtifact = artifactFactory.createArtifact( projectArtifact.getGroupId(),
                                                                        projectArtifact.getArtifactId(),
                                                                        project.getVersion(), packaging, "exe.config" );
            try
            {
                artifactDeployer.deploy( exePath, attachedArtifact,
                                         project.getDistributionManagementArtifactRepository(), localRepo );
            }
            catch ( ArtifactDeploymentException e )
            {
                throw new MojoExecutionException( "NPANDAY-DEPLOY: Deploy Failed", e );
            }
        }

        if ( classifier != null )
        {
            Artifact attachedArtifact = artifactFactory.createArtifactWithClassifier( projectArtifact.getGroupId(),
                                                                        projectArtifact.getArtifactId(),
                                                                        project.getVersion(), packaging, classifier );
            try
            {
                artifactDeployer.deploy( project.getArtifact().getFile(), attachedArtifact,
                                         project.getDistributionManagementArtifactRepository(), localRepo );
            }
            catch ( ArtifactDeploymentException e )
            {
                throw new MojoExecutionException( "NPANDAY-DEPLOY: Deploy Failed", e );
            }
        }

        try
        {
            artifactDeployer.deploy( project.getArtifact().getFile(), projectArtifact,
                                     project.getDistributionManagementArtifactRepository(), localRepo );
        }
        catch ( ArtifactDeploymentException e )
        {
            throw new MojoExecutionException( "NPANDAY-DEPLOY: Deploy Failed", e );
        }
    }
}

@@ -1317,24 +1317,76 @@
 venProject;%0A
+import org.apache.maven.project.MavenProjectHelper;%0A
 import org.a
@@ -2350,27 +2350,60 @@
 %0A     * 
-The project
+Attach an additional artifact with the given
  classif
@@ -2405,21 +2405,17 @@
 assifier
- type
+.
 %0A     *%0A
@@ -2552,16 +2552,97 @@
 ctory;%0A%0A
+    /**%0A     * @component%0A     */%0A    private MavenProjectHelper projectHelper;%0A%0A
     publ
@@ -3573,1520 +3573,246 @@
     
-Artifact attachedArtifact = artifactFactory.createArtifact( projectArtifact.getGroupId(),%0A                                                                        projectArtifact.getArtifactId(),%0A                                                                        project.getVersion(), packaging, %22exe.config%22 );%0A            try%0A            %7B%0A                artifactDeployer.deploy( exePath, attachedArtifact,%0A                                         project.getDistributionManagementArtifactRepository(), localRepo );%0A            %7D%0A            catch ( ArtifactDeploymentException e )%0A            %7B%0A                throw new MojoExecutionException( %22NPANDAY-DEPLOY: Deploy Failed%22, e );%0A            %7D%0A        %7D%0A%0A        if ( classifier != null )%0A        %7B%0A            Artifact attachedArtifact = artifactFactory.createArtifactWithClassifier( projectArtifact.getGroupId(),%0A                                                                        projectArtifact.getArtifactId(),%0A                                                                        project.getVersion(), packaging, classifier );%0A            try%0A            %7B%0A                artifactDeployer.deploy( project.getArtifact().getFile(), attachedArtifact,%0A                                         project.getDistributionManagementArtifactRepository(), localRepo );%0A            %7D%0A            catch ( ArtifactDeploymentException e )%0A            %7B%0A                throw new MojoExecutionException( %22NPANDAY-DEPLOY: Deploy Failed%22, e );%0A            %7D
+projectHelper.attachArtifact( project, packaging, %22exe.config%22, exePath );%0A        %7D%0A%0A        if ( classifier != null )%0A        %7B%0A            projectHelper.attachArtifact( project, packaging, classifier, project.getArtifact().getFile() );
 %0A   

Correct problems with the deploy plugin using new snapshot build numbers for each artifact