cn.wanghaomiao.maven.plugin.seimi.packaging.SaveWebappStructurePostPackagingTask.java Source code

Java tutorial

Introduction

Here is the source code for cn.wanghaomiao.maven.plugin.seimi.packaging.SaveWebappStructurePostPackagingTask.java

Source

package cn.wanghaomiao.maven.plugin.seimi.packaging;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 cn.wanghaomiao.maven.plugin.seimi.util.WebappStructureSerializer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.io.IOException;

/**
 * Saves the webapp structure cache.
 *
 * @author Stephane Nicoll
 * @version $Id: SaveWebappStructurePostPackagingTask.java 1637197 2014-11-06 19:46:57Z khmarbaise $
 */
public class SaveWebappStructurePostPackagingTask implements WarPostPackagingTask {

    private final File targetFile;

    private final WebappStructureSerializer serialier;

    /**
     * @param targetFile {@link #targetFile}
     */
    public SaveWebappStructurePostPackagingTask(File targetFile) {
        this.targetFile = targetFile;
        this.serialier = new WebappStructureSerializer();
    }

    /**
     * {@inheritDoc}
     */
    public void performPostPackaging(WarPackagingContext context)
            throws MojoExecutionException, MojoFailureException {
        if (targetFile == null) {
            context.getLog().debug("Cache usage is disabled, not saving webapp structure.");
        } else {
            try {
                serialier.toXml(context.getWebappStructure(), targetFile);
                context.getLog().debug("Cache saved successfully.");
            } catch (IOException e) {
                throw new MojoExecutionException("Could not save webapp structure", e);
            }
        }
    }
}