|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.plugins.site; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.apache.maven.plugin.MojoExecutionException; |
| 26 | +import org.apache.maven.plugins.annotations.Component; |
| 27 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 28 | +import org.apache.maven.plugins.annotations.Mojo; |
| 29 | +import org.apache.maven.plugins.annotations.Parameter; |
| 30 | +import org.apache.maven.project.MavenProjectHelper; |
| 31 | +import org.codehaus.plexus.archiver.Archiver; |
| 32 | +import org.codehaus.plexus.archiver.ArchiverException; |
| 33 | +import org.codehaus.plexus.archiver.FileSet; |
| 34 | +import org.codehaus.plexus.archiver.util.DefaultFileSet; |
| 35 | + |
| 36 | +/** |
| 37 | + * Adds the site resources (compressed in a ZIP archive) as dedicated artifact with classifier {@value #CLASSIFIER} to be installed/deployed. |
| 38 | + * Usually used in combination with {@link org.apache.maven.plugins.site.descriptor.SiteDescriptorAttachMojo} to also deploy the actual site descriptor. |
| 39 | + * This is used for sites inheriting from this project |
| 40 | + * |
| 41 | + * @since next |
| 42 | + */ |
| 43 | +@Mojo(name = SiteResourcesAttachMojo.GOAL_NAME, defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true) |
| 44 | +public class SiteResourcesAttachMojo extends AbstractSiteMojo { |
| 45 | + |
| 46 | + public static final String GOAL_NAME = "attach-site-resources"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Directory containing the <code>site.xml</code> file and the source for hand written docs (one directory |
| 50 | + * per Doxia-source-supported markup types) |
| 51 | + * @see <a href="/doxia/references/index.html">Doxia Markup Languages References</a>. |
| 52 | + */ |
| 53 | + @Parameter(defaultValue = "${basedir}/src/site") |
| 54 | + protected File siteDirectory; |
| 55 | + |
| 56 | + /** |
| 57 | + * Maven ProjectHelper. |
| 58 | + */ |
| 59 | + @Component |
| 60 | + private MavenProjectHelper projectHelper; |
| 61 | + |
| 62 | + @Component(hint = ARCHIVE_EXTENSION) |
| 63 | + private Archiver zipArchiver; |
| 64 | + |
| 65 | + /** |
| 66 | + * The file name patterns to exclude (potentially in addition to the default ones mentioned at {@link #addDefaultExcludes}). |
| 67 | + * The format of each pattern is described in {@link org.codehaus.plexus.util.DirectoryScanner}. |
| 68 | + * The comparison is performed against the file path relative to the {@link #siteDirectory}. |
| 69 | + * <p> |
| 70 | + * Each value is either a regex pattern if enclosed within {@code %regex[} and {@code ]}, otherwise an |
| 71 | + * <a href="https://ant.apache.org/manual/dirtasks.html#patterns">Ant pattern</a>. |
| 72 | + * Exclusions take precedence over inclusions via {@link #includes}. |
| 73 | + */ |
| 74 | + @Parameter(defaultValue = "**/.gitignore,**/.gitattributes", required = true) |
| 75 | + protected Set<String> excludes; |
| 76 | + |
| 77 | + /** |
| 78 | + * The file name patterns to include. The format of each pattern is described in {@link org.codehaus.plexus.util.DirectoryScanner}. |
| 79 | + * The comparison is performed against the file path relative to the {@link #siteDirectory}. |
| 80 | + * Since this is hardly predictable it is recommended to use only filename/directory name patterns here |
| 81 | + * but not take into account file system hierarchies! |
| 82 | + * <p> |
| 83 | + * Each value is either a regex pattern if enclosed within {@code %regex[} and {@code ]}, otherwise an |
| 84 | + * <a href="https://ant.apache.org/manual/dirtasks.html#patterns">Ant pattern</a>. |
| 85 | + * If this is not set, everything is included. |
| 86 | + */ |
| 87 | + @Parameter(required = false) |
| 88 | + protected Set<String> includes; |
| 89 | + |
| 90 | + /** |
| 91 | + * By default certain metadata files are excluded which means they will not be copied into the package. |
| 92 | + * If you need them for a particular reason you can do that by setting this parameter to {@code false}. |
| 93 | + * |
| 94 | + * @see org.codehaus.plexus.util.AbstractScanner#DEFAULTEXCLUDES |
| 95 | + */ |
| 96 | + @Parameter(defaultValue = "true") |
| 97 | + protected boolean addDefaultExcludes; |
| 98 | + |
| 99 | + /** |
| 100 | + * Attach site resources in archive only if packaging is pom. |
| 101 | + */ |
| 102 | + @Parameter(defaultValue = "true") |
| 103 | + private boolean pomPackagingOnly; |
| 104 | + |
| 105 | + public static final String CLASSIFIER = "site-resources"; |
| 106 | + public static final String ARCHIVE_EXTENSION = "zip"; |
| 107 | + |
| 108 | + public void execute() throws MojoExecutionException { |
| 109 | + if (pomPackagingOnly && !"pom".equals(project.getPackaging())) { |
| 110 | + // https://issues.apache.org/jira/browse/MSITE-597 |
| 111 | + getLog().info("Skipping because packaging '" + project.getPackaging() + "' is not pom."); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (siteDirectory.exists()) { |
| 116 | + try { |
| 117 | + File destFile = new File( |
| 118 | + project.getBuild().getDirectory(), |
| 119 | + project.getBuild().getFinalName() + CLASSIFIER + "." + ARCHIVE_EXTENSION); |
| 120 | + |
| 121 | + File siteResourcesArchiveFile = createArchive(zipArchiver, destFile); |
| 122 | + // Attach the site resources archive |
| 123 | + getLog().info("Attaching site resources archive with classifier '" + CLASSIFIER + "'."); |
| 124 | + projectHelper.attachArtifact(project, ARCHIVE_EXTENSION, CLASSIFIER, siteResourcesArchiveFile); |
| 125 | + } catch (IOException e) { |
| 126 | + throw new MojoExecutionException("Unable to archive site resources", e); |
| 127 | + } |
| 128 | + } else { |
| 129 | + getLog().warn("No site resources found: nothing to attach."); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private FileSet createFileSet() { |
| 134 | + DefaultFileSet fileSet = new DefaultFileSet(); |
| 135 | + fileSet.setDirectory(siteDirectory); |
| 136 | + fileSet.setExcludes(excludes.toArray(new String[0])); |
| 137 | + if (includes != null && !includes.isEmpty()) { |
| 138 | + fileSet.setIncludes(includes.toArray(new String[0])); |
| 139 | + } |
| 140 | + fileSet.setUsingDefaultExcludes(addDefaultExcludes); |
| 141 | + return fileSet; |
| 142 | + } |
| 143 | + |
| 144 | + public File createArchive(Archiver archiver, File destFile) throws ArchiverException, IOException { |
| 145 | + archiver.setDestFile(destFile); |
| 146 | + archiver.addFileSet(createFileSet()); |
| 147 | + archiver.createArchive(); |
| 148 | + return archiver.getDestFile(); |
| 149 | + } |
| 150 | + |
| 151 | + public File getSiteDirectory() { |
| 152 | + return siteDirectory; |
| 153 | + } |
| 154 | +} |
0 commit comments