View Javadoc

1   package com.atlassian.maven.plugins.licenses;
2   
3   import java.io.File;
4   
5   import org.apache.maven.artifact.Artifact;
6   import org.apache.maven.artifact.deployer.ArtifactDeployer;
7   import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
8   import org.apache.maven.artifact.repository.ArtifactRepository;
9   import org.apache.maven.artifact.repository.DefaultArtifactRepository;
10  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
11  import org.apache.maven.model.License;
12  import org.apache.maven.plugin.MojoExecutionException;
13  import org.apache.maven.project.MavenProject;
14  
15  /**
16   * This mojo downloads the license files for the dependencies and then deploys them to the specified repository.
17   * Deployed artifacts will be of type 'license'.
18   * 
19   * User can elect to overwrite existing licenses in the repository by setting the 'forceDeploy' flag.
20   * 
21   * @author Sherali Karimov
22   * @goal deploy
23   */
24  public class DeployMojo extends DownloadMojo
25  {
26      /** @component */
27      private ArtifactDeployer deployer;
28  
29      /**
30       * @parameter expression="${licenses.repositoryUrl}"
31       * @required
32       */
33  
34      private String repositoryUrl;
35      /**
36       * @parameter expression="${licenses.repositoryId}"
37       * @required
38       */
39      private String repositoryId;
40  
41      /**
42       * This goal will first check if the license actually exists in the
43       * repository or not. To avoid the check and deploy anyway, use this flag.
44       * 
45       * @parameter expression="${licenses.forceDeploy}" default-value="false"
46       */
47      private boolean forceDeploy;
48  
49      /**
50       * Delegates to the parent for the download and then deploys the newly downloaded file.
51       */
52      protected void downloadLicense(MavenProject depProject, License license, String fileName)
53              throws MojoExecutionException
54      {
55          super.downloadLicense(depProject, license, fileName);
56  
57          // deploy the file here
58          Artifact artifact = depProject.getArtifact();
59          Artifact licenseArtifact = artifactFactory.createArtifact(artifact.getGroupId(), artifact.getArtifactId(),
60                  artifact.getVersion(), null, AbstractLicensesMojo.LICENSE_TYPE);
61  
62          File file = new File(fileName);
63          ArtifactRepository licenseRepository = new DefaultArtifactRepository(repositoryId, repositoryUrl,
64                  new DefaultRepositoryLayout());
65  
66          try
67          {
68              // first we check the flag and it if is 'true' we will skip repository checking
69              // but if the flag is 'false' and we find the license in the repository - we will skip deploying
70              if (!forceDeploy && findLicenseInRepository(artifact) != null)
71              {
72                  getLog().info("License for artifact " + artifact + " is found in the repository");
73                  return;
74              }
75  
76              deployer.deploy(file, artifact, licenseRepository, localRepository);
77          }
78          catch (ArtifactDeploymentException e)
79          {
80              throw new MojoExecutionException("Failed to deploy license artifact " + licenseArtifact, e);
81          }
82      }
83  }