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 import org.apache.maven.shared.utils.PathTool;
34 import org.codehaus.plexus.util.FileUtils;
35
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 AbstractSiteDescriptorMojo
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
66
67
68
69 @Parameter( defaultValue = "true" )
70 private boolean pomPackagingOnly;
71
72 public void execute()
73 throws MojoExecutionException
74 {
75 if ( pomPackagingOnly && !"pom".equals( project.getPackaging() ) )
76 {
77
78 getLog().info( "Skipping because packaging '" + project.getPackaging() + "' is not pom." );
79 return;
80 }
81
82 boolean attachedSiteDescriptor = false;
83 for ( Locale locale : getLocales() )
84 {
85 File descriptorFile = siteTool.getSiteDescriptor( siteDirectory, locale );
86
87 if ( descriptorFile.exists() )
88 {
89 attachedSiteDescriptor = true;
90
91
92 String classifier = getClassifier( descriptorFile );
93
94 String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
95 File targetDescriptorFile = new File( project.getBuild().getDirectory(), filename );
96
97 try
98 {
99
100 FileUtils.copyFile( descriptorFile, targetDescriptorFile );
101
102 getLog().info( "Attaching '"
103 + PathTool.getRelativeFilePath( basedir.getAbsolutePath(), descriptorFile.getAbsolutePath() )
104 + "' site descriptor with classifier '" + classifier + "'." );
105 projectHelper.attachArtifact( project, "xml", classifier, targetDescriptorFile );
106 }
107 catch ( IOException e )
108 {
109 throw new MojoExecutionException( "Unable to copy site descriptor", e );
110 }
111 }
112 }
113
114 if ( !attachedSiteDescriptor )
115 {
116 getLog().info( "No site descriptor found: nothing to attach." );
117 }
118 }
119
120 private static String getClassifier( final File descriptorFile )
121 throws MojoExecutionException
122 {
123 final int index = descriptorFile.getName().lastIndexOf( '.' );
124
125 if ( index > 0 )
126 {
127 return descriptorFile.getName().substring( 0, index );
128 }
129 else
130 {
131 throw new MojoExecutionException( "Unable to determine the classifier to use" );
132 }
133 }
134 }