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