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 | |
import java.io.StringReader; |
25 | |
import java.io.Writer; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.List; |
28 | |
import java.util.Locale; |
29 | |
import java.util.Map; |
30 | |
|
31 | |
import org.apache.commons.io.IOUtils; |
32 | |
import org.apache.maven.doxia.site.decoration.DecorationModel; |
33 | |
import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader; |
34 | |
import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer; |
35 | |
import org.apache.maven.doxia.tools.SiteToolException; |
36 | |
import org.apache.maven.plugin.MojoExecutionException; |
37 | |
import org.apache.maven.project.MavenProject; |
38 | |
import org.apache.maven.project.MavenProjectHelper; |
39 | |
import org.codehaus.plexus.util.IOUtil; |
40 | |
import org.codehaus.plexus.util.ReaderFactory; |
41 | |
import org.codehaus.plexus.util.WriterFactory; |
42 | |
import org.codehaus.plexus.util.xml.XmlStreamReader; |
43 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | public class SiteDescriptorAttachMojo |
54 | |
extends AbstractSiteMojo |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private File basedir; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private MavenProjectHelper projectHelper; |
72 | |
|
73 | |
public void execute() |
74 | |
throws MojoExecutionException |
75 | |
{ |
76 | 0 | List<Locale> localesList = siteTool.getAvailableLocales( locales ); |
77 | |
|
78 | 0 | for ( Locale locale : localesList ) |
79 | |
{ |
80 | 0 | File descriptorFile = siteTool.getSiteDescriptorFromBasedir( toRelative( project.getBasedir(), |
81 | |
siteDirectory.getAbsolutePath() ), |
82 | |
basedir, locale ); |
83 | |
|
84 | 0 | if ( descriptorFile.exists() ) |
85 | |
{ |
86 | 0 | Map<String, String> props = new HashMap<String, String>(); |
87 | 0 | props.put( "reports", "<menu ref=\"reports\"/>" ); |
88 | 0 | props.put( "modules", "<menu ref=\"modules\"/>" ); |
89 | |
|
90 | |
DecorationModel decoration; |
91 | 0 | XmlStreamReader reader = null; |
92 | |
try |
93 | |
{ |
94 | 0 | reader = ReaderFactory.newXmlReader( descriptorFile ); |
95 | 0 | String siteDescriptorContent = IOUtil.toString( reader ); |
96 | |
|
97 | 0 | siteDescriptorContent = |
98 | |
siteTool.getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent, |
99 | |
getInputEncoding(), getOutputEncoding() ); |
100 | |
|
101 | 0 | decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) ); |
102 | |
} |
103 | 0 | catch ( XmlPullParserException e ) |
104 | |
{ |
105 | 0 | throw new MojoExecutionException( "Error parsing site descriptor", e ); |
106 | |
} |
107 | 0 | catch ( IOException e ) |
108 | |
{ |
109 | 0 | throw new MojoExecutionException( "Error reading site descriptor", e ); |
110 | |
} |
111 | 0 | catch ( SiteToolException e ) |
112 | |
{ |
113 | 0 | throw new MojoExecutionException( "Error when interpolating site descriptor", e ); |
114 | |
} |
115 | |
finally |
116 | |
{ |
117 | 0 | IOUtils.closeQuietly( reader ); |
118 | 0 | } |
119 | |
|
120 | 0 | MavenProject parentProject = siteTool.getParentProject( project, reactorProjects, localRepository ); |
121 | 0 | if ( parentProject != null && project.getUrl() != null && parentProject.getUrl() != null ) |
122 | |
{ |
123 | 0 | siteTool.populateParentMenu( decoration, locale, project, parentProject, true ); |
124 | |
} |
125 | |
try |
126 | |
{ |
127 | 0 | siteTool.populateModulesMenu( project, reactorProjects, localRepository, decoration, locale, true ); |
128 | |
} |
129 | 0 | catch ( SiteToolException e ) |
130 | |
{ |
131 | 0 | throw new MojoExecutionException( "Error when populating modules", e ); |
132 | 0 | } |
133 | |
|
134 | |
|
135 | 0 | String classifier = null; |
136 | 0 | int index = descriptorFile.getName().lastIndexOf( '.' ); |
137 | 0 | if ( index > 0 ) |
138 | |
{ |
139 | 0 | classifier = descriptorFile.getName().substring( 0, index ); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | throw new MojoExecutionException( "Unable to determine the classifier to use" ); |
144 | |
} |
145 | |
|
146 | |
|
147 | 0 | String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName(); |
148 | 0 | File interpolatedDescriptorFile = new File( project.getBuild().getDirectory(), filename ); |
149 | 0 | interpolatedDescriptorFile.getParentFile().mkdirs(); |
150 | |
|
151 | 0 | Writer writer = null; |
152 | |
try |
153 | |
{ |
154 | |
|
155 | 0 | writer = WriterFactory.newXmlWriter( interpolatedDescriptorFile ); |
156 | 0 | new DecorationXpp3Writer().write( writer, decoration ); |
157 | |
|
158 | 0 | getLog().debug( "Attaching the site descriptor '" + interpolatedDescriptorFile.getAbsolutePath() |
159 | |
+ "' with classifier '" + classifier + "' to the project." ); |
160 | 0 | projectHelper.attachArtifact( project, "xml", classifier, interpolatedDescriptorFile ); |
161 | |
} |
162 | 0 | catch ( IOException e ) |
163 | |
{ |
164 | 0 | throw new MojoExecutionException( "Unable to store interpolated site descriptor", e ); |
165 | |
} |
166 | |
finally |
167 | |
{ |
168 | 0 | IOUtils.closeQuietly( writer ); |
169 | 0 | } |
170 | |
} |
171 | 0 | } |
172 | 0 | } |
173 | |
} |