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.model.Site; |
23 | |
import org.apache.maven.plugin.MojoExecutionException; |
24 | |
import org.apache.maven.plugin.MojoFailureException; |
25 | |
import org.apache.maven.project.MavenProject; |
26 | |
import org.apache.maven.wagon.repository.Repository; |
27 | |
import org.codehaus.plexus.util.PathTool; |
28 | |
import org.codehaus.plexus.util.StringUtils; |
29 | |
|
30 | |
import java.io.File; |
31 | |
import java.util.Iterator; |
32 | |
import java.util.List; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class SiteStageMojo |
49 | |
extends SiteMojo |
50 | |
{ |
51 | |
protected static final String DEFAULT_STAGING_DIRECTORY = "staging"; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
protected File stagingDirectory; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public void execute() |
66 | |
throws MojoExecutionException, MojoFailureException |
67 | |
{ |
68 | 0 | String structureProject = getStructure( project, false ); |
69 | |
|
70 | 0 | if ( structureProject == null ) |
71 | |
{ |
72 | 0 | throw new MojoExecutionException( "Missing site information." ); |
73 | |
} |
74 | |
|
75 | 0 | stagingDirectory = getStagingDirectory( project, reactorProjects, stagingDirectory ); |
76 | 0 | getLog().info( "Using this directory for staging: " + stagingDirectory ); |
77 | |
|
78 | 0 | outputDirectory = new File( stagingDirectory, structureProject ); |
79 | |
|
80 | |
|
81 | 0 | if ( !outputDirectory.exists() ) |
82 | |
{ |
83 | 0 | outputDirectory.mkdirs(); |
84 | |
} |
85 | |
|
86 | 0 | String outputRelativePath = PathTool.getRelativePath( stagingDirectory.getAbsolutePath(), new File( |
87 | |
outputDirectory, "dummy.html" ).getAbsolutePath() ); |
88 | 0 | project.setUrl( outputRelativePath + "/" + structureProject ); |
89 | |
|
90 | 0 | MavenProject parent = siteTool.getParentProject( project, reactorProjects, localRepository ); |
91 | 0 | if ( parent != null ) |
92 | |
{ |
93 | 0 | String structureParentProject = getStructure( parent, true ); |
94 | 0 | if ( structureParentProject != null ) |
95 | |
{ |
96 | 0 | parent.setUrl( outputRelativePath + "/" + structureParentProject ); |
97 | |
} |
98 | |
} |
99 | |
|
100 | 0 | if ( reactorProjects != null && reactorProjects.size() > 1 ) |
101 | |
{ |
102 | 0 | Iterator reactorItr = reactorProjects.iterator(); |
103 | |
|
104 | 0 | while ( reactorItr.hasNext() ) |
105 | |
{ |
106 | 0 | MavenProject reactorProject = (MavenProject) reactorItr.next(); |
107 | |
|
108 | 0 | if ( reactorProject != null && reactorProject.getParent() != null |
109 | |
&& project.getArtifactId().equals( reactorProject.getParent().getArtifactId() ) ) |
110 | |
{ |
111 | 0 | String structureReactorProject = getStructure( reactorProject, false ); |
112 | 0 | reactorProject.setUrl( outputRelativePath + "/" + structureReactorProject ); |
113 | |
} |
114 | |
} |
115 | |
} |
116 | |
|
117 | 0 | super.execute(); |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
protected File getStagingDirectory( MavenProject currentProject, List reactorProjects, File usersStagingDirectory ) |
129 | |
{ |
130 | |
|
131 | 0 | if ( usersStagingDirectory != null ) |
132 | |
{ |
133 | 0 | getLog().debug( "stagingDirectory specified by the user." ); |
134 | 0 | return usersStagingDirectory; |
135 | |
} |
136 | 0 | getLog().debug( "stagingDirectory NOT specified by the user." ); |
137 | |
|
138 | |
|
139 | 0 | MavenProject topLevelProject = getTopLevelProject( reactorProjects ); |
140 | |
|
141 | |
|
142 | |
File buildDirectory; |
143 | 0 | if ( topLevelProject == null ) |
144 | |
{ |
145 | 0 | getLog().debug( "No top level project found in the reactor, using the current project." ); |
146 | 0 | buildDirectory = new File( currentProject.getBuild().getDirectory() ); |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | 0 | getLog().debug( "Using the top level project found in the reactor." ); |
151 | 0 | buildDirectory = new File( topLevelProject.getBuild().getDirectory() ); |
152 | |
} |
153 | |
|
154 | 0 | return new File( buildDirectory, DEFAULT_STAGING_DIRECTORY ); |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
protected MavenProject getTopLevelProject( List reactorProjects ) |
164 | |
{ |
165 | 0 | MavenProject topLevelProject = null; |
166 | 0 | if ( reactorProjects != null ) |
167 | |
{ |
168 | 0 | Iterator iterator = reactorProjects.iterator(); |
169 | 0 | while ( iterator.hasNext() ) |
170 | |
{ |
171 | 0 | MavenProject reactorProject = (MavenProject) iterator.next(); |
172 | 0 | if ( reactorProject.isExecutionRoot() ) |
173 | |
{ |
174 | 0 | topLevelProject = reactorProject; |
175 | |
} |
176 | |
} |
177 | |
} |
178 | 0 | return topLevelProject; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
protected static String getStructure( MavenProject project, boolean ignoreMissingSiteUrl ) |
191 | |
throws MojoFailureException |
192 | |
{ |
193 | 0 | if ( project.getDistributionManagement() == null ) |
194 | |
{ |
195 | 0 | String hierarchy = project.getArtifactId(); |
196 | |
|
197 | 0 | MavenProject parent = project.getParent(); |
198 | 0 | while ( parent != null ) |
199 | |
{ |
200 | 0 | hierarchy = parent.getArtifactId() + "/" + hierarchy; |
201 | 0 | parent = parent.getParent(); |
202 | |
} |
203 | |
|
204 | 0 | return hierarchy; |
205 | |
} |
206 | |
|
207 | 0 | Site site = project.getDistributionManagement().getSite(); |
208 | 0 | if ( site == null ) |
209 | |
{ |
210 | 0 | if ( !ignoreMissingSiteUrl ) |
211 | |
{ |
212 | 0 | throw new MojoFailureException( |
213 | |
"Missing site information in the distribution management element in the project: '" |
214 | |
+ project.getName() + "'." ); |
215 | |
} |
216 | |
|
217 | 0 | return null; |
218 | |
} |
219 | |
|
220 | 0 | if ( StringUtils.isEmpty( site.getUrl() ) ) |
221 | |
{ |
222 | 0 | if ( !ignoreMissingSiteUrl ) |
223 | |
{ |
224 | 0 | throw new MojoFailureException( "The URL in the site is missing in the project descriptor." ); |
225 | |
} |
226 | |
|
227 | 0 | return null; |
228 | |
} |
229 | |
|
230 | 0 | Repository repository = new Repository( site.getId(), site.getUrl() ); |
231 | 0 | StringBuffer hierarchy = new StringBuffer( 1024 ); |
232 | 0 | hierarchy.append( repository.getHost() ); |
233 | 0 | if ( !StringUtils.isEmpty( repository.getBasedir() ) ) |
234 | |
{ |
235 | 0 | if ( !repository.getBasedir().startsWith( "/" ) ) |
236 | |
{ |
237 | 0 | hierarchy.append( '/' ); |
238 | |
} |
239 | 0 | hierarchy.append( repository.getBasedir() ); |
240 | |
} |
241 | |
|
242 | 0 | return hierarchy.toString().replaceAll( "[\\:\\?\\*]", "" ); |
243 | |
} |
244 | |
} |