001/*
002 * Copyright 2012-2017 the original author or authors.
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.springframework.boot.cli.command.archive;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.IOException;
022
023import org.springframework.boot.cli.command.Command;
024import org.springframework.boot.loader.tools.JarWriter;
025import org.springframework.boot.loader.tools.Layouts;
026import org.springframework.boot.loader.tools.LibraryScope;
027
028/**
029 * {@link Command} to create a self-contained executable jar file from a CLI application.
030 *
031 * @author Andrey Stolyarov
032 * @author Phillip Webb
033 * @author Henri Kerola
034 * @since 1.3.0
035 */
036public class WarCommand extends ArchiveCommand {
037
038        public WarCommand() {
039                super("war", "Create a self-contained executable war "
040                                + "file from a Spring Groovy script", new WarOptionHandler());
041        }
042
043        private static final class WarOptionHandler extends ArchiveOptionHandler {
044
045                WarOptionHandler() {
046                        super("war", new Layouts.War());
047                }
048
049                @Override
050                protected LibraryScope getLibraryScope(File file) {
051                        String fileName = file.getName();
052                        if (fileName.contains("tomcat-embed")
053                                        || fileName.contains("spring-boot-starter-tomcat")) {
054                                return LibraryScope.PROVIDED;
055                        }
056                        return LibraryScope.COMPILE;
057                }
058
059                @Override
060                protected void addCliClasses(JarWriter writer) throws IOException {
061                        addClass(writer, null, "org.springframework.boot."
062                                        + "cli.app.SpringApplicationWebApplicationInitializer");
063                        super.addCliClasses(writer);
064                }
065
066                @Override
067                protected void writeClasspathEntry(JarWriter writer,
068                                ResourceMatcher.MatchedResource entry) throws IOException {
069                        writer.writeEntry(getLayout().getClassesLocation() + entry.getName(),
070                                        new FileInputStream(entry.getFile()));
071                }
072
073        }
074
075}