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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class DownloadMojo extends AbstractLicensesMojo
39 {
40
41
42
43
44
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
73
74
75 if (licenseCount > 0)
76 fileName += "-" + licenseCount;
77 licenseCount++;
78 downloadLicense(depProject, license, fileName);
79 }
80 }
81
82 public void onComplete()
83 {
84 }
85
86 public void onInit(int numDeps)
87 {
88 }
89 });
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
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
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
158
159
160
161
162
163 protected String generatePathToSaveLicense(Artifact artifact)
164 {
165
166 return outputDirectory + File.separator + artifact.getArtifactId() + "-" + artifact.getVersion() + "-LICENSE";
167 }
168 }