org.jfrog.jade.plugins.idea.IdeaCleanMojo.java Source code

Java tutorial

Introduction

Here is the source code for org.jfrog.jade.plugins.idea.IdeaCleanMojo.java

Source

package org.jfrog.jade.plugins.idea;

/*
 * Copyright 2005-2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.jfrog.maven.annomojo.annotations.MojoGoal;
import org.jfrog.maven.annomojo.annotations.MojoParameter;
import org.jfrog.jade.plugins.common.injectable.MvnInjectableMojoSupport;
import org.jfrog.jade.plugins.common.naming.GroupDefinition;
import org.jfrog.jade.plugins.common.naming.ProjectNameProvider;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
 * Plugin to remove existing idea files on the project
 *
 * @author Edwin Punzalan
 */
@MojoGoal("clean")
public class IdeaCleanMojo extends MvnInjectableMojoSupport {

    @SuppressWarnings({ "UnusedDeclaration" })
    @MojoParameter(expression = "cleanAll", defaultValue = "false", description = "Ask for deletion of all IdeaJ related file in all  basedirs")
    private boolean cleanAll;

    @MojoParameter
    private Set<GroupDefinition> groupDefinitions = new HashSet<GroupDefinition>();

    public ProjectNameProvider getNameProvider() {
        ProjectNameProvider np = super.getNameProvider();
        if (np != null && groupDefinitions != null && !groupDefinitions.isEmpty())
            np.setGroupDefinitions(groupDefinitions);
        return np;
    }

    public void execute() throws MojoExecutionException, MojoFailureException {
        File iprFile = getIdeaFile(".ipr");
        deleteFile(iprFile);

        File imlFile = getIdeaFile(".iml");
        deleteFile(imlFile);

        File iwsFile = getIdeaFile(".iws");
        deleteFile(iwsFile);
    }

    private File getIdeaFile(String extension) {
        MavenProject project = getProject();
        return new File(project.getBasedir(), getNameProvider().getProjectName(project) + extension);
    }

    private void deleteFile(File file) {
        if (file.exists()) {
            if (!file.isDirectory()) {
                FileUtils.fileDelete(file.getAbsolutePath());
            }
        }
    }
}