1 package org.apache.maven.plugins.site;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.project.MavenProjectHelper;
30
31 import org.codehaus.plexus.util.FileUtils;
32
33 /**
34 * Adds the site descriptor (<code>site.xml</code>) to the list of files to be installed/deployed.
35 *
36 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37 * @version $Id: SiteDescriptorAttachMojo.html 816558 2012-05-08 12:00:46Z hboutemy $
38 * @goal attach-descriptor
39 * @phase package
40 */
41 public class SiteDescriptorAttachMojo
42 extends AbstractSiteMojo
43 {
44 /**
45 * @parameter expression="${basedir}"
46 * @required
47 * @readonly
48 */
49 private File basedir;
50
51 /**
52 * Maven ProjectHelper.
53 *
54 * @component
55 * @readonly
56 * @since 2.1.1
57 */
58 private MavenProjectHelper projectHelper;
59
60 public void execute()
61 throws MojoExecutionException
62 {
63 List<Locale> localesList = siteTool.getAvailableLocales( locales );
64
65 for ( Locale locale : localesList )
66 {
67 File descriptorFile = siteTool.getSiteDescriptorFromBasedir(
68 siteTool.getRelativePath( siteDirectory.getAbsolutePath(), project.getBasedir().getAbsolutePath() ),
69 basedir, locale );
70
71 if ( descriptorFile.exists() )
72 {
73 // Calculate the classifier to use
74 String classifier = getClassifier( descriptorFile );
75 // Prepare a file for the interpolated site descriptor
76 String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
77 File targetDescriptorFile = new File( project.getBuild().getDirectory(), filename );
78
79 try
80 {
81 // Copy the site descriptor to a file
82 FileUtils.copyFile( descriptorFile, targetDescriptorFile );
83 // Attach the site descriptor
84 getLog().debug( "Attaching the site descriptor '" + targetDescriptorFile.getAbsolutePath()
85 + "' with classifier '" + classifier + "' to the project." );
86 projectHelper.attachArtifact( project, "xml", classifier, targetDescriptorFile );
87 }
88 catch ( IOException e )
89 {
90 throw new MojoExecutionException( "Unable to copy site descriptor", e );
91 }
92 }
93 }
94 }
95
96 private static String getClassifier( final File descriptorFile )
97 throws MojoExecutionException
98 {
99 final int index = descriptorFile.getName().lastIndexOf( '.' );
100
101 if ( index > 0 )
102 {
103 return descriptorFile.getName().substring( 0, index );
104 }
105 else
106 {
107 throw new MojoExecutionException( "Unable to determine the classifier to use" );
108 }
109 }
110 }