View Javadoc

1   package com.atlassian.maven.plugins.licenses;
2   
3   import java.io.BufferedOutputStream;
4   import java.io.File;
5   import java.io.FileNotFoundException;
6   import java.io.FileOutputStream;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  import java.net.MalformedURLException;
11  import java.net.URL;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.apache.maven.artifact.Artifact;
16  import org.apache.maven.model.License;
17  import org.apache.maven.plugin.MojoExecutionException;
18  import org.apache.maven.plugin.MojoFailureException;
19  import org.apache.maven.project.MavenProject;
20  import org.codehaus.plexus.util.IOUtil;
21  
22  /**
23   * After resolving all the dependencies of this project, for each one of them,
24   * this mojo attemps to determine a valid URL for license location for this
25   * project and download it.
26   * 
27   * First of all it attempts to extract the license location from the project
28   * POM. If that fails - performs a search for other potential license locations
29   * inside the JAR as defined in {@link #LICENSE_PATH_OPTIONS}
30   * 
31   * The file name is structured as artifactId-version-LICENSE. For projects
32   * with multiple licenses the file name is artifactId-version-LICENSE-n
33   * where n is a license counter (starting from 0).
34   * 
35   * @author Sherali Karimov
36   * @goal download
37   */
38  public class DownloadMojo extends AbstractLicensesMojo
39  {
40      /**
41       * Target directory where to save downloaded license files
42       * 
43       * @parameter expression="${licenses.outputDirectory}" default-value="${project.build.directory}/licenses/"
44       * @required
45       */
46      protected String outputDirectory;
47  
48      public void execute() throws MojoExecutionException, MojoFailureException
49      {
50          visitDependencyProjects(new ProjectVisitor()
51          {
52              public void visit(MavenProject depProject) throws MojoExecutionException
53              {
54                  List licenses = depProject.getLicenses();
55                  Artifact artifact = depProject.getArtifact();
56                  String fileName = generatePathToSaveLicense(artifact);
57  
58                  if (licenses.isEmpty())
59                  {
60                      downloadLicense(depProject, null, fileName);
61                      return;
62                  }
63                  int licenseCount = 0;
64                  for (Iterator licIter = licenses.iterator(); licIter.hasNext();)
65                  {
66                      License license = (License) licIter.next();
67                      if (getLog().isDebugEnabled())
68                      {
69                          getLog().debug("Attempting to fetch " + license.getName() + " for " + artifact);
70                      }
71  
72                      // TODO - the file name fiddling doesn't delong here!
73                      // find a better way of dealing with multiple licenses in a
74                      // fileName
75                      if (licenseCount > 0)
76                          fileName += "-" + licenseCount;
77                      licenseCount++;
78                      downloadLicense(depProject, license, fileName);
79                  }
80              }
81  
82              public void onComplete()
83              {/* nothing to do, really */
84              }
85  
86              public void onInit(int numDeps)
87              {/* nothing to do, really */
88              }
89          });
90      }
91  
92      /**
93       * Attemps to determine a valid URL for license location for this project
94       * and download it.
95       * 
96       * @param depProject -
97       *            maven project of the artifact for which the license is being
98       *            looked up
99       * @param license -
100      *            license object from the maven project. If this is null, this
101      *            method will search the artifact location
102      * @param fileName -
103      *            target file path where to save the license if found
104      */
105     protected void downloadLicense(MavenProject depProject, License license, String fileName)
106             throws MojoExecutionException
107     {
108         Artifact artifact = depProject.getArtifact();
109         String urlStr = createValidLicenseUrl(license, artifact);
110         if (urlStr == null)
111         {
112             getLog().error(artifact + " contains no licensing information.");
113             return;
114         }
115 
116         try
117         {
118             URL url = new URL(urlStr);
119             InputStream in = url.openStream();
120 
121             // make sure the directory exists
122             File targetDir = new File(fileName).getParentFile();
123             if (!targetDir.exists())
124             {
125                 getLog().debug("Creating directory " + targetDir.getPath());
126                 targetDir.mkdirs();
127             }
128 
129             OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
130             getLog().info("Loading license for " + artifact + " from " + urlStr + " into " + fileName);
131 
132             try
133             {
134                 IOUtil.copy(in, out);
135             }
136             finally
137             {
138                 IOUtil.close(out);
139                 IOUtil.close(in);
140             }
141         }
142         catch (MalformedURLException e)
143         {
144             getLog().error("Failed to download the license for " + artifact + ". URL is invalid: " + urlStr, e);
145         }
146         catch (FileNotFoundException e)
147         {
148             getLog().error("File name is invalid " + fileName, e);
149         }
150         catch (IOException e)
151         {
152             getLog().error("Failed to download the license for " + artifact, e);
153         }
154     }
155 
156     /**
157      * The file name is structured as artifactId-version-LICENSE
158      * 
159      * @param artifact -
160      *            artifact for which to generate the license output location
161      * @return file name for the license output location
162      */
163     protected String generatePathToSaveLicense(Artifact artifact)
164     {
165         // TODO - infer the extension correctly from the URL
166         return outputDirectory + File.separator + artifact.getArtifactId() + "-" + artifact.getVersion() + "-LICENSE";
167     }
168 }