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 ) |
48 | 0 | 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 | 0 | if ( pomPackagingOnly && !"pom".equals( project.getPackaging() ) ) |
75 | |
{ |
76 | |
|
77 | 0 | return; |
78 | |
} |
79 | |
|
80 | 0 | List<Locale> localesList = siteTool.getAvailableLocales( locales ); |
81 | |
|
82 | 0 | for ( Locale locale : localesList ) |
83 | |
{ |
84 | 0 | File descriptorFile = siteTool.getSiteDescriptorFromBasedir( |
85 | |
siteTool.getRelativePath( siteDirectory.getAbsolutePath(), project.getBasedir().getAbsolutePath() ), |
86 | |
basedir, locale ); |
87 | |
|
88 | 0 | if ( descriptorFile.exists() ) |
89 | |
{ |
90 | |
|
91 | 0 | String classifier = getClassifier( descriptorFile ); |
92 | |
|
93 | 0 | String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName(); |
94 | 0 | File targetDescriptorFile = new File( project.getBuild().getDirectory(), filename ); |
95 | |
|
96 | |
try |
97 | |
{ |
98 | |
|
99 | 0 | FileUtils.copyFile( descriptorFile, targetDescriptorFile ); |
100 | |
|
101 | 0 | getLog().debug( "Attaching the site descriptor '" + targetDescriptorFile.getAbsolutePath() |
102 | |
+ "' with classifier '" + classifier + "' to the project." ); |
103 | 0 | projectHelper.attachArtifact( project, "xml", classifier, targetDescriptorFile ); |
104 | |
} |
105 | 0 | catch ( IOException e ) |
106 | |
{ |
107 | 0 | throw new MojoExecutionException( "Unable to copy site descriptor", e ); |
108 | 0 | } |
109 | |
} |
110 | 0 | } |
111 | 0 | } |
112 | |
|
113 | |
private static String getClassifier( final File descriptorFile ) |
114 | |
throws MojoExecutionException |
115 | |
{ |
116 | 0 | final int index = descriptorFile.getName().lastIndexOf( '.' ); |
117 | |
|
118 | 0 | if ( index > 0 ) |
119 | |
{ |
120 | 0 | return descriptorFile.getName().substring( 0, index ); |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | 0 | throw new MojoExecutionException( "Unable to determine the classifier to use" ); |
125 | |
} |
126 | |
} |
127 | |
} |