1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.site.descriptor;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Locale;
24
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugins.annotations.Component;
27 import org.apache.maven.plugins.annotations.LifecyclePhase;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.maven.project.MavenProjectHelper;
31 import org.apache.maven.shared.utils.PathTool;
32 import org.codehaus.plexus.util.FileUtils;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 @Mojo(name = "attach-descriptor", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
47 public class SiteDescriptorAttachMojo extends AbstractSiteDescriptorMojo {
48
49
50 @Parameter(property = "basedir", required = true, readonly = true)
51 private File basedir;
52
53
54
55
56
57
58 @Component
59 private MavenProjectHelper projectHelper;
60
61
62
63
64
65 @Parameter(defaultValue = "true")
66 private boolean pomPackagingOnly;
67
68 public void execute() throws MojoExecutionException {
69 if (pomPackagingOnly && !"pom".equals(project.getPackaging())) {
70
71 getLog().info("Skipping because packaging '" + project.getPackaging() + "' is not pom.");
72 return;
73 }
74
75 boolean attachedSiteDescriptor = false;
76 for (Locale locale : getLocales()) {
77 File descriptorFile = siteTool.getSiteDescriptor(siteDirectory, locale);
78
79 if (descriptorFile.exists()) {
80 attachedSiteDescriptor = true;
81
82
83 String classifier = getClassifier(descriptorFile);
84
85 String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
86 File targetDescriptorFile = new File(project.getBuild().getDirectory(), filename);
87
88 try {
89
90 FileUtils.copyFile(descriptorFile, targetDescriptorFile);
91
92 getLog().info("Attaching '"
93 + PathTool.getRelativeFilePath(basedir.getAbsolutePath(), descriptorFile.getAbsolutePath())
94 + "' site descriptor with classifier '" + classifier + "'.");
95 projectHelper.attachArtifact(project, "xml", classifier, targetDescriptorFile);
96 } catch (IOException e) {
97 throw new MojoExecutionException("Unable to copy site descriptor", e);
98 }
99 }
100 }
101
102 if (!attachedSiteDescriptor) {
103 getLog().info("No site descriptor found: nothing to attach.");
104 }
105 }
106
107 private static String getClassifier(final File descriptorFile) throws MojoExecutionException {
108 final int index = descriptorFile.getName().lastIndexOf('.');
109
110 if (index > 0) {
111 return descriptorFile.getName().substring(0, index);
112 } else {
113 throw new MojoExecutionException("Unable to determine the classifier to use");
114 }
115 }
116 }