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.plugins.site.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 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 String structureProject = getStructure( project, false );
69
70 if ( structureProject == null )
71 {
72 throw new MojoExecutionException( "Missing site information." );
73 }
74
75 stagingDirectory = getStagingDirectory( project, reactorProjects, stagingDirectory );
76 getLog().info( "Using this directory for staging: " + stagingDirectory );
77
78 outputDirectory = new File( stagingDirectory, structureProject );
79
80
81 if ( !outputDirectory.exists() )
82 {
83 outputDirectory.mkdirs();
84 }
85
86 String outputRelativePath = PathTool.getRelativePath( stagingDirectory.getAbsolutePath(), new File(
87 outputDirectory, "dummy.html" ).getAbsolutePath() );
88 project.setUrl( outputRelativePath + "/" + structureProject );
89
90 MavenProject parent = siteTool.getParentProject( project, reactorProjects, localRepository );
91 if ( parent != null )
92 {
93 String structureParentProject = getStructure( parent, true );
94 if ( structureParentProject != null )
95 {
96 parent.setUrl( outputRelativePath + "/" + structureParentProject );
97 }
98 }
99
100 if ( reactorProjects != null && reactorProjects.size() > 1 )
101 {
102 Iterator reactorItr = reactorProjects.iterator();
103
104 while ( reactorItr.hasNext() )
105 {
106 MavenProject reactorProject = (MavenProject) reactorItr.next();
107
108 if ( reactorProject != null && reactorProject.getParent() != null
109 && project.getArtifactId().equals( reactorProject.getParent().getArtifactId() ) )
110 {
111 String structureReactorProject = getStructure( reactorProject, false );
112 reactorProject.setUrl( outputRelativePath + "/" + structureReactorProject );
113 }
114 }
115 }
116
117 super.execute();
118 }
119
120
121
122
123
124
125
126
127
128 protected File getStagingDirectory( MavenProject currentProject, List reactorProjects, File usersStagingDirectory )
129 {
130
131 if ( usersStagingDirectory != null )
132 {
133 getLog().debug( "stagingDirectory specified by the user." );
134 return usersStagingDirectory;
135 }
136 getLog().debug( "stagingDirectory NOT specified by the user." );
137
138
139 MavenProject topLevelProject = getTopLevelProject( reactorProjects );
140
141
142 File buildDirectory;
143 if ( topLevelProject == null )
144 {
145 getLog().debug( "No top level project found in the reactor, using the current project." );
146 buildDirectory = new File( currentProject.getBuild().getDirectory() );
147 }
148 else
149 {
150 getLog().debug( "Using the top level project found in the reactor." );
151 buildDirectory = new File( topLevelProject.getBuild().getDirectory() );
152 }
153
154 return new File( buildDirectory, DEFAULT_STAGING_DIRECTORY );
155 }
156
157
158
159
160
161
162
163 protected MavenProject getTopLevelProject( List reactorProjects )
164 {
165 MavenProject topLevelProject = null;
166 if ( reactorProjects != null )
167 {
168 Iterator iterator = reactorProjects.iterator();
169 while ( iterator.hasNext() )
170 {
171 MavenProject reactorProject = (MavenProject) iterator.next();
172 if ( reactorProject.isExecutionRoot() )
173 {
174 topLevelProject = reactorProject;
175 }
176 }
177 }
178 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 if ( project.getDistributionManagement() == null )
194 {
195 String hierarchy = project.getArtifactId();
196
197 MavenProject parent = project.getParent();
198 while ( parent != null )
199 {
200 hierarchy = parent.getArtifactId() + "/" + hierarchy;
201 parent = parent.getParent();
202 }
203
204 return hierarchy;
205 }
206
207 Site site = project.getDistributionManagement().getSite();
208 if ( site == null )
209 {
210 if ( !ignoreMissingSiteUrl )
211 {
212 throw new MojoFailureException(
213 "Missing site information in the distribution management element in the project: '"
214 + project.getName() + "'." );
215 }
216
217 return null;
218 }
219
220 if ( StringUtils.isEmpty( site.getUrl() ) )
221 {
222 if ( !ignoreMissingSiteUrl )
223 {
224 throw new MojoFailureException( "The URL in the site is missing in the project descriptor." );
225 }
226
227 return null;
228 }
229
230 Repository repository = new Repository( site.getId(), site.getUrl() );
231 StringBuffer hierarchy = new StringBuffer( 1024 );
232 hierarchy.append( repository.getHost() );
233 if ( !StringUtils.isEmpty( repository.getBasedir() ) )
234 {
235 if ( !repository.getBasedir().startsWith( "/" ) )
236 {
237 hierarchy.append( '/' );
238 }
239 hierarchy.append( repository.getBasedir() );
240 }
241
242 return hierarchy.toString().replaceAll( "[\\:\\?\\*]", "" );
243 }
244 }