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 public class SiteDescriptorAttachMojo
44 extends AbstractSiteMojo
45 {
46
47
48
49
50
51 private File basedir;
52
53
54
55
56
57
58
59
60 private MavenProjectHelper projectHelper;
61
62
63
64
65 private boolean pomPackagingOnly;
66
67 public void execute()
68 throws MojoExecutionException
69 {
70 if ( pomPackagingOnly && !"pom".equals( project.getPackaging() ) )
71 {
72
73 return;
74 }
75
76 List<Locale> localesList = siteTool.getAvailableLocales( locales );
77
78 for ( Locale locale : localesList )
79 {
80 File descriptorFile = siteTool.getSiteDescriptorFromBasedir(
81 siteTool.getRelativePath( siteDirectory.getAbsolutePath(), project.getBasedir().getAbsolutePath() ),
82 basedir, locale );
83
84 if ( descriptorFile.exists() )
85 {
86
87 String classifier = getClassifier( descriptorFile );
88
89 String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
90 File targetDescriptorFile = new File( project.getBuild().getDirectory(), filename );
91
92 try
93 {
94
95 FileUtils.copyFile( descriptorFile, targetDescriptorFile );
96
97 getLog().debug( "Attaching the site descriptor '" + targetDescriptorFile.getAbsolutePath()
98 + "' with classifier '" + classifier + "' to the project." );
99 projectHelper.attachArtifact( project, "xml", classifier, targetDescriptorFile );
100 }
101 catch ( IOException e )
102 {
103 throw new MojoExecutionException( "Unable to copy site descriptor", e );
104 }
105 }
106 }
107 }
108
109 private static String getClassifier( final File descriptorFile )
110 throws MojoExecutionException
111 {
112 final int index = descriptorFile.getName().lastIndexOf( '.' );
113
114 if ( index > 0 )
115 {
116 return descriptorFile.getName().substring( 0, index );
117 }
118 else
119 {
120 throw new MojoExecutionException( "Unable to determine the classifier to use" );
121 }
122 }
123 }