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