1 package org.apache.maven.plugins.site.descriptor;
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.Locale;
26
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugins.annotations.Component;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.project.MavenProjectHelper;
33
34 import org.codehaus.plexus.util.FileUtils;
35
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 AbstractSiteDescriptorMojo
50 {
51
52
53 @Parameter( property = "basedir", required = true, readonly = true )
54 private File basedir;
55
56
57
58
59
60
61 @Component
62 private MavenProjectHelper projectHelper;
63
64 @Parameter( defaultValue = "true" )
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 for ( Locale locale : getLocales() )
77 {
78 File descriptorFile = siteTool.getSiteDescriptor( siteDirectory, locale );
79
80 if ( descriptorFile.exists() )
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
91 FileUtils.copyFile( descriptorFile, targetDescriptorFile );
92
93 getLog().debug( "Attaching the site descriptor '" + targetDescriptorFile.getAbsolutePath()
94 + "' with classifier '" + classifier + "' to the project." );
95 projectHelper.attachArtifact( project, "xml", classifier, targetDescriptorFile );
96 }
97 catch ( IOException e )
98 {
99 throw new MojoExecutionException( "Unable to copy site descriptor", e );
100 }
101 }
102 }
103 }
104
105 private static String getClassifier( final File descriptorFile )
106 throws MojoExecutionException
107 {
108 final int index = descriptorFile.getName().lastIndexOf( '.' );
109
110 if ( index > 0 )
111 {
112 return descriptorFile.getName().substring( 0, index );
113 }
114 else
115 {
116 throw new MojoExecutionException( "Unable to determine the classifier to use" );
117 }
118 }
119 }