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