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