b2s.idea.mavenize.App.java Source code

Java tutorial

Introduction

Here is the source code for b2s.idea.mavenize.App.java

Source

/**
 *
 * Copyright to the original author or authors.
 *
 * 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.
 */
package b2s.idea.mavenize;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.io.IOUtils;

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

public class App {
    protected static JarCombiner jarCombiner = new JarCombiner();

    public static void main(String... args) throws ParseException {
        Options options = new Options();
        options.addOption("i", "idea-dir", true, "Path to root directory of an intellij instance");
        options.addOption("o", "output-dir", true, "Path to put the newly created jar");

        if (args.length == 0) {
            error("", options);
            return;
        }

        PosixParser parser = new PosixParser();
        CommandLine commandLine = parser.parse(options, args);

        if (commandLine.hasOption("i")) {
            String ideaDirValue = commandLine.getOptionValue("i");
            File ideaFolder = new File(ideaDirValue);
            if (!ideaFolder.exists()) {
                error("This path [" + ideaDirValue + "] can not be found", options);
                return;
            }

            File libFolder = new File(ideaFolder, "lib");
            if (!libFolder.exists()) {
                error("The [lib] folder was not found in the IDEA folder", options);
                return;
            }

            if (commandLine.hasOption("output-dir")) {
                File output = new File(commandLine.getOptionValue("output-dir"), determineFilename(ideaFolder));
                jarCombiner.combineAllJarsIn(libFolder, output);
                System.out.println("Combined Jar: " + output);
            } else {
                error("Please provide an output directory", options);
            }
        } else {
            error("Please provide the IDEA directory path", options);
        }
    }

    private static String determineFilename(File ideaFolder) {
        String version = determineSdkVersion(ideaFolder);
        return "idea-sdk-" + version + ".jar";
    }

    private static String determineSdkVersion(File ideaFolder) {
        try {
            return IOUtils.toString(new FileInputStream(new File(ideaFolder, "build.txt")));
        } catch (IOException e) {
            throw new RuntimeException("A problem occurred when attempting to determine the version of IDEA", e);
        }
    }

    private static void error(String message, Options options) {
        System.out.println("ERROR:" + message);
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("<jar-file> -i <idea-dir> -o <output-dir>", options);
    }
}