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