1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.oneandone.maven.plugins.billofmaterials;
17
18 import com.google.common.base.*;
19 import com.google.common.collect.Collections2;
20 import com.google.common.collect.Lists;
21 import com.google.common.hash.HashCode;
22 import com.google.common.hash.HashFunction;
23 import com.google.common.hash.Hashing;
24 import com.google.common.io.Files;
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Locale;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugins.annotations.LifecyclePhase;
35 import org.apache.maven.plugins.annotations.Mojo;
36 import org.apache.maven.project.MavenProject;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @Mojo(name = "create", aggregator = false, defaultPhase = LifecyclePhase.INSTALL)
58 public class CreateBillOfMaterialsMojo extends AbstractBillOfMaterialsMojo {
59
60
61
62
63 private final HashFunction sha1 = Hashing.sha1();
64
65
66
67
68 private final Function<File, String> toBomStringFunction;
69
70
71
72
73 private final Function<Artifact, File> toFileFunction;
74
75
76
77
78 CreateBillOfMaterialsMojo() {
79 super();
80 toFileFunction = new ToFileFunction();
81 toBomStringFunction = new ToBomStringFunction(sha1);
82 }
83
84
85
86
87
88
89 CreateBillOfMaterialsMojo(File billOfMaterialsPath, MavenProject project) {
90 super(billOfMaterialsPath, project);
91 toFileFunction = new ToFileFunction();
92 toBomStringFunction = new ToBomStringFunction(sha1);
93 }
94
95 @Override
96 public void execute() throws MojoExecutionException, MojoFailureException {
97 try {
98 final List<File> files = getListOfArtifactsAsFiles();
99
100 final List<String> hashBaseNames = new ArrayList<>(Lists.transform(files, toBomStringFunction));
101 addHashEntryForPom(hashBaseNames);
102 writeResults(hashBaseNames);
103 } catch (IOException ex) {
104 throw new MojoExecutionException(ex.toString(), ex);
105 }
106 }
107
108
109
110
111
112 final List<File> getListOfArtifactsAsFiles() {
113 final MavenProject project = getProject();
114 final List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
115
116
117 final List<File> files = new ArrayList<>(
118 Collections2.filter(
119 Lists.transform(attachedArtifacts, toFileFunction),
120 Files.isFile()));
121 final String packaging = project.getPackaging();
122
123 if (!"pom".equals(packaging)) {
124 files.add(project.getArtifact().getFile());
125 }
126 return files;
127 }
128
129
130
131
132
133
134 void addHashEntryForPom(final List<String> hashBaseNames) throws IOException {
135 final MavenProject project = getProject();
136 final HashCode sha1OfPom = Files.hash(project.getFile(), sha1);
137 final String pomLine = String.format(Locale.ENGLISH, "%s %s-%s.pom",
138 sha1OfPom, project.getArtifactId(), project.getVersion());
139 hashBaseNames.add(pomLine);
140 }
141
142
143
144
145
146
147
148 void writeResults(final List<String> hashBaseNames) throws IOException {
149 final String hashBaseNamesAsString = Joiner.on("\n").join(hashBaseNames) + "\n";
150 final String userName = System.getProperty("user.name");
151 write(projectCommentToString(userName));
152 write(hashBaseNamesAsString);
153 }
154
155
156
157
158
159
160
161 void write(final String content) throws IOException {
162 final File bomFile = calculateBillOfMaterialsFile();
163 final File parentDirectory = bomFile.getParentFile();
164 if (!createParentDirectory(parentDirectory)) {
165 throw new IOException("Could not create parent directory for " + bomFile);
166 }
167 Files.append(content, bomFile, Charsets.UTF_8);
168 }
169
170
171
172
173
174
175
176 String projectCommentToString(final String userName) {
177 final MavenProject project = getProject();
178 return String.format(
179 Locale.ENGLISH,
180 "# %s:%s:%s user=%s\n",
181 project.getGroupId(), project.getArtifactId(), project.getVersion(), userName);
182 }
183
184
185
186
187
188
189
190 boolean createParentDirectory(final File parentDirectory) {
191 return parentDirectory.exists() || parentDirectory.mkdirs();
192 }
193 }