001/*
002 * Copyright 2011-2012 Stephen Connolly.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.jszip.maven;
018
019import org.apache.maven.artifact.Artifact;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.apache.maven.plugin.MojoFailureException;
022import org.apache.maven.plugins.annotations.Component;
023import org.apache.maven.plugins.annotations.LifecyclePhase;
024import org.apache.maven.plugins.annotations.Mojo;
025import org.apache.maven.plugins.annotations.Parameter;
026import org.apache.maven.project.MavenProjectHelper;
027import org.codehaus.plexus.util.StringUtils;
028
029import java.io.File;
030
031/**
032 * Initializes the JSZIP artifact.
033 */
034@Mojo(name = "initialize", defaultPhase = LifecyclePhase.COMPILE)
035public class InitializeMojo extends AbstractJSZipMojo {
036
037    /**
038     * Directory containing the classes.
039     */
040    @Parameter(defaultValue = "src/main/js", required = true)
041    private File contentDirectory;
042
043    /**
044     * Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.
045     */
046    @Parameter
047    private String classifier;
048
049    /**
050     * Maven ProjectHelper.
051     */
052    @Component
053    private MavenProjectHelper projectHelper;
054
055    /**
056     * @see org.apache.maven.plugin.Mojo#execute()
057     */
058    public void execute() throws MojoExecutionException, MojoFailureException {
059        // In order to behave like JAR projects, we point to the unpacked files up between the phases
060        // compile and package. Once at the package phase, the packed artifact is used.
061
062        if (StringUtils.isEmpty(classifier)) {
063            project.getArtifact().setFile(contentDirectory);
064        } else {
065            boolean found = false;
066            for (Artifact artifact : project.getAttachedArtifacts()) {
067                if (StringUtils.equals(artifact.getGroupId(), project.getGroupId())
068                        && StringUtils.equals(artifact.getArtifactId(), project.getArtifactId())
069                        && StringUtils.equals(artifact.getVersion(), project.getVersion())
070                        && StringUtils.equals(artifact.getClassifier(), classifier)
071                        && StringUtils.equals(artifact.getType(), JSZIP_TYPE)) {
072                    if (artifact.getFile() == null) {
073                        artifact.setFile(contentDirectory);
074                    }
075                    found = true;
076                }
077            }
078            if (!found) {
079                projectHelper.attachArtifact(project, JSZIP_TYPE, classifier, contentDirectory);
080            }
081        }
082    }
083
084}