Example usage for org.apache.maven.cli MavenCli doMain

List of usage examples for org.apache.maven.cli MavenCli doMain

Introduction

In this page you can find the example usage for org.apache.maven.cli MavenCli doMain.

Prototype

public int doMain(String[] args, String workingDirectory, PrintStream stdout, PrintStream stderr) 

Source Link

Document

This supports painless invocation by the Verifier during embedded execution of the core ITs.

Usage

From source file:com.ibm.liberty.starter.it.api.v1.utils.MvnUtils.java

License:Apache License

public static int runMvnCommand(PrintStream outputStream, String tempDir, DownloadedZip zip, String... args) {
    String mvnMultiModuleProjectDirectory = tempDir + "/mvn/multi_module";
    System.setProperty(MavenCli.MULTIMODULE_PROJECT_DIRECTORY, mvnMultiModuleProjectDirectory);
    MavenCli cli = new MavenCli();
    return cli.doMain(args, zip.getLocation(), outputStream, outputStream);
}

From source file:com.iyonger.apm.web.handler.GroovyMavenProjectScriptHandler.java

License:Apache License

@Override
protected void prepareDistMore(Long testId, User user, FileEntry script, File distDir,
        PropertiesWrapper properties, ProcessingResultPrintStream processingResult) {
    String pomPathInSVN = PathUtils.join(getBasePath(script), "pom.xml");
    MavenCli cli = new MavenCli();
    processingResult.println("\nCopy dependencies by running 'mvn dependency:copy-dependencies"
            + " -DoutputDirectory=./lib -DexcludeScope=provided'");

    int result = cli.doMain(new String[] { // goal specification
            "dependency:copy-dependencies", // run dependency goal
            "-DoutputDirectory=./lib", // to the lib folder
            "-DexcludeScope=provided" // but exclude the provided
            // library
    }, distDir.getAbsolutePath(), processingResult, processingResult);
    boolean success = (result == 0);
    if (success) {
        processingResult.printf("\nDependencies in %s was copied.\n", pomPathInSVN);
        LOGGER.info("Dependencies in {} is copied into {}/lib folder", pomPathInSVN, distDir.getAbsolutePath());
    } else {/*from  w  w  w .ja v a2  s.  com*/
        processingResult.printf("\nDependencies copy in %s is failed.\n", pomPathInSVN);
        LOGGER.info("Dependencies copy in {} is failed.", pomPathInSVN);
    }
    // Then it's not necessary to include pom.xml anymore.
    FileUtils.deleteQuietly(new File(distDir, "pom.xml"));
    processingResult.setSuccess(result == 0);
}

From source file:es.eucm.ead.exporter.AndroidExporter.java

License:Open Source License

public void export(String projectPath, String destination, Properties properties, boolean installAndDeploy) {
    // Copy android-pom.xml to android folder
    InputStream apkpom = ClassLoader.getSystemResourceAsStream("android-pom.xml");

    InputStream manifest = generateManifest(properties);
    OutputStream os = null;//from  w  ww.  j  a va 2  s.c  o  m
    try {
        File projectFolder = new File(projectPath);
        File mavenFolder = FileUtils.createTempDir("eadmaven", "");
        File androidFolder = FileUtils.createTempDir("eadandroid", "");
        FileUtils.deleteRecursive(androidFolder);
        androidFolder.mkdirs();
        addTitle(mavenFolder, properties);
        addIcon(mavenFolder, properties);

        logger.info("Finished adding title & icon to {}", mavenFolder.getAbsolutePath());

        FileUtils.copy(apkpom, new FileOutputStream(new File(mavenFolder, "pom.xml")));
        FileUtils.copy(manifest, new FileOutputStream(new File(mavenFolder, "AndroidManifest.xml")));

        logger.info("Added manifest and pom; now copying game assets...");

        // Copy game assets to folder
        FileUtils.copyRecursive(projectFolder, androidFolder, androidFolder);
        // Copy engine assets to folder
        for (String s : R.RESOURCES) {
            InputStream ris = ClassLoader.getSystemResourceAsStream(s);
            OutputStream ros = null;
            try {
                String fileName = s.substring(s.lastIndexOf('/') + 1);
                String folder = s.substring(0, s.lastIndexOf('/'));
                File rf = new File(androidFolder.getAbsolutePath() + '/' + folder);
                rf.mkdirs();
                ros = new FileOutputStream(new File(rf, fileName));
                FileUtils.copy(ris, ros);
            } catch (Exception e) {
                logger.error("Problem copying resources {}", s, e);
            } finally {
                if (ris != null) {
                    ris.close();
                }
                if (ros != null) {
                    ros.close();
                }
            }
        }

        logger.info("... game assets & engine assets copied over");

        String[] mavenArguments;

        if (installAndDeploy) {
            mavenArguments = new String[] { "-Dresources=" + androidFolder.getAbsolutePath(),
                    "-Dandroid.sdk.path=" + properties.getProperty(SDK_HOME), "-X", "clean", "compile",
                    "install", "android:deploy", "android:run" };
        } else {
            mavenArguments = new String[] { "-Dresources=" + androidFolder.getAbsolutePath(),
                    "-Dandroid.sdk.path=" + properties.getProperty(SDK_HOME), "-X", "clean", "compile",
                    "install", };
        }

        logger.info("Starting maven build of game .apk...");

        MavenCli maven = new MavenCli();
        maven.doMain(mavenArguments, mavenFolder.getAbsolutePath(), getStdOut(), getStdErr());

        logger.info("... finished. Now copying .apk to final destination");

        // Copy apk to destination
        if (destination != null) {
            File destinationFile = new File(destination);
            if (destinationFile.isDirectory()) {
                destinationFile = new File(destinationFile, "eAdventuregame.apk");
            } else if (!destination.endsWith(".apk")) {
                destination += ".apk";
                destinationFile = new File(destination);
            }
            FileUtils.copy(new File(mavenFolder.getAbsolutePath() + "/target", "game-android.apk"),
                    destinationFile);
        }
    } catch (Exception e) {
        logger.error("Error exporting to apk", e);
    } finally {
        if (apkpom != null) {
            try {
                apkpom.close();
            } catch (IOException e) {
                logger.error("", e);
            }
        }

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                logger.error("", e);
            }
        }

        if (manifest != null) {
            try {
                manifest.close();
            } catch (IOException e) {
                logger.error("", e);
            }
        }
    }
}

From source file:es.eucm.ead.exporter.JarExporter.java

License:Open Source License

/**
 * @param projectFolder folder containing a folder (name as parameter 'resourcesFolder') with all the assets of the game
 * @param destination       destination file to ouput the .jar
 *///from   w ww . j  a v a 2 s  .  co m
public void export(String projectFolder, String destination, PrintStream out) {
    // Copy desktop-pom.xml to desktop folder
    InputStream jarpom = ClassLoader.getSystemResourceAsStream("desktop-pom.xml");
    OutputStream os = null;
    try {
        File desktopFolder = FileUtils.createTempDir("eaddesktop", "");
        desktopFolder.mkdirs();

        // Copy pom to temp folder
        FileUtils.copy(jarpom, new FileOutputStream(new File(desktopFolder, "pom.xml")));

        // Generate the jar with maven
        MavenCli maven = new MavenCli();
        maven.doMain(new String[] { "-X", "-Dresources=" + projectFolder, "clean", "compile", "install",
                "assembly:single" }, desktopFolder.getAbsolutePath(), out, out);

        // Copy jar to destination
        File destinationFile = new File(destination);
        if (destinationFile.isDirectory()) {
            destinationFile = new File(destinationFile, "eAdventuregame.jar");
        } else if (!destination.endsWith(".jar")) {
            destination += ".jar";
            destinationFile = new File(destination);
        }

        // Copy to destination file
        if (destinationFile != null) {
            FileUtils.copy(new File(desktopFolder.getAbsolutePath() + "/target",
                    "game-desktop-1.0-jar-with-dependencies.jar"), destinationFile);
        }
    } catch (Exception e) {

    } finally {
        if (jarpom != null) {
            try {
                jarpom.close();
            } catch (IOException e) {
            }
        }

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
    }

}

From source file:licenseUtil.Utils.java

License:Apache License

public static void writeEffectivePom(File projectDirectory, String fullEffectivePomFilename) {
    MavenCli cli = new MavenCli();
    logger.info("Write effective-pom to \"" + fullEffectivePomFilename + "\"");
    cli.doMain(
            new String[] { "org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom",
                    "-Doutput=" + fullEffectivePomFilename },
            projectDirectory.getAbsolutePath(), new PrintStream(new OutputStream() {
                public void write(int b) {
                    //NO-OP
                }/*from  ww  w  . j  ava 2 s  .  c o m*/
            }), System.err);
}

From source file:net.ssehub.easy.libs.maven.MavenPrg.java

License:Apache License

@Override
public int execute(String[] args, String workingDirectory, PrintStream stdout, PrintStream stderr) {
    MavenCli cli = new MavenCli();
    return cli.doMain(args, workingDirectory, System.out, System.out);
}

From source file:org.guvnor.server.BuildServiceImpl.java

License:Apache License

@Override
public BuildResults buildAndDeploy(final Project project) {

    BuildResults buildResults = new BuildResults();

    try {//www  . j a v a2  s  . c  o  m

        projectVisitor.visit(project);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(out);

        MavenCli cli = new MavenCli();
        int result = cli.doMain(new String[] { "clean", "install" },
                projectVisitor.getRootFolder().getAbsolutePath(), printStream, printStream);

        if (result != 0) {
            BuildMessage message = new BuildMessage();
            message.setId(123);
            message.setText(new String(out.toByteArray()));
            message.setLevel(Level.ERROR);
            buildResults.addBuildMessage(message);
        }

        deployer.deploy(projectVisitor.getTargetFolder());

    } catch (IOException e) {
        buildResults.addBuildMessage(reportError(e));
    } finally {

        try {
            FileUtils.deleteDirectory(projectVisitor.getGuvnorTempFolder());
        } catch (IOException e) {
            buildResults.addBuildMessage(reportError(e));
        }
    }

    return buildResults;
}

From source file:org.ihtsdo.otf.tcc.rest.server.SetupServerDependencies.java

License:Apache License

public boolean execute(String args[]) throws IOException {
    String appHome = System.getenv("CATALINA_HOME") + "/temp/bdb";

    context.log("App home: " + appHome);

    File app = new File(appHome);

    if (!app.exists()) {
        app.mkdirs();//ww  w. ja  v  a 2 s .  c  o  m
    }

    context.log("The default user settings file " + MavenCli.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath());
    System.setProperty("org.slf4j.simpleLogger.showLogName", "false");
    System.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
    System.setProperty("org.slf4j.simpleLogger.levelInBrackets", "true");
    System.setProperty("org.ihtsdo.otf.tcc.datastore.working-dir", appHome + "/target");
    System.setProperty("org.ihtsdo.otf.tcc.datastore.bdb-location", appHome + "/target/berkeley-db");

    MavenCli cli = new MavenCli();

    // Write to the pom.xml
    File pomDir = new File(appHome);

    if (!pomDir.exists()) {
        pomDir.mkdirs();
    }

    String username = System.getProperty("org.ihtsdo.otf.tcc.repository.username");
    if (username == null || username.isEmpty()) {
        context.log("WARNING: Username is null. Please set "
                + "'org.ihtsdo.otf.tcc.repository.username'  to a proper "
                + "value in the CATALINA_OPTS environmental variable");
    }
    String password = System.getProperty("org.ihtsdo.otf.tcc.repository.password");
    if (password == null || password.isEmpty()) {
        context.log("WARNING: Password is null. Please set "
                + "'org.ihtsdo.otf.tcc.repository.password' to a proper "
                + "value in the CATALINA_OPTS environmental variable");
    }
    String pomResource = "/WEB-INF/classes/org/ihtsdo/otf/serversetup/pom.xml";
    String settingsResource = "/WEB-INF/classes/org/ihtsdo/otf/serversetup/settings.xml";

    context.log("pom: " + context.getResource(pomResource));

    File pomFile = new File(pomDir + "/pom.xml");
    if (pomFile.exists()) {
        context.log("Pom file exists. Now deleting.");
        pomFile.delete();
    }
    BufferedReader pomReader = new BufferedReader(
            new InputStreamReader(context.getResourceAsStream(pomResource), "UTF-8"));
    BufferedWriter pomWriter = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(pomFile), "UTF-8"));

    try {
        String settingsLine;

        while ((settingsLine = pomReader.readLine()) != null) {
            pomWriter.write(settingsLine + "\n");
        }
    } finally {
        pomReader.close();
        pomWriter.close();
    }

    // Write the settings.xml file
    context.log("settings: " + context.getResource(settingsResource));
    File settingsFile = new File(appHome, "settings.xml");
    if (settingsFile.exists()) {
        context.log("Settings file exists. Now deleting.");
        settingsFile.delete();
    }
    context.log("settings path: " + settingsFile.getAbsolutePath());

    BufferedReader settingsReader = new BufferedReader(
            new InputStreamReader(context.getResourceAsStream(settingsResource), "UTF-8"));
    BufferedWriter settingsWriter = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(settingsFile), "UTF-8"));

    try {
        String s;

        while ((s = settingsReader.readLine()) != null) {
            s = s.replace("<username>user</username>", "<username>" + username + "</username>");
            s = s.replace("<password>password</password>", "<password>" + password + "</password>");
            s = s.replace("<localRepository>mvn-repo</localRepository>",
                    "<localRepository>" + appHome + "/mvn-repo</localRepository>");
            settingsWriter.write(s + "\n");
        }
    } finally {
        settingsReader.close();
        settingsWriter.close();
    }

    if ((args == null) || (args.length == 0)) {
        args = new String[] { "-e", "-C", "-settings", settingsFile.getAbsolutePath(), "-U", "clean",
                "install" };
    }

    OutputStream stringStream = new ContextLoggerStream();
    PrintStream mavenOutputStream = new PrintStream(stringStream);

    int result = cli.doMain(args, pomDir.getAbsolutePath(), mavenOutputStream, mavenOutputStream);

    context.log(stringStream.toString());

    if (result == 0) {
        context.log("Embedded maven build succeeded: " + result);
    } else {
        context.log("Embedded maven build failed: " + result);
        return false;
    }
    return true;
}

From source file:org.jabox.application.MavenCreateProject.java

License:Open Source License

/**
 * @param project//w  ww  .  ja v a  2 s. c om
 * @param baseDir
 *            the base directory where to add the project files.
 * @return
 * @throws IOException
 * @throws InvalidRepositoryException
 * @throws MavenExecutionException
 */
public static String createProjectWithMavenCore(final Project project, final String baseDir)
        throws IOException, InvalidRepositoryException, MavenExecutionException {

    MavenArchetype ma = project.getMavenArchetype();
    String[] args = new String[] { "archetype:generate", "-DarchetypeGroupId=" + ma.getArchetypeGroupId(),
            "-DarchetypeArtifactId=" + ma.getArchetypeArtifactId(),
            "-DarchetypeVersion=" + ma.getArchetypeVersion(), "-DgroupId=org.jabox", "-Dversion=1.0.0-SNAPSHOT",
            "-Dpackage=org.jabox", "-DartifactId=" + project.getName(), "-DinteractiveMode=false" };
    MavenCli cli = new MavenCli();
    cli.doMain(args, baseDir, System.out, System.err);

    return baseDir + File.separatorChar + project.getName();
}

From source file:org.javalite.db_migrator.AbstractIntegrationSpec.java

License:Apache License

protected String execute(String dir, String... args) throws IOException, InterruptedException {
    OutputStream outos = null;/*from   w ww .  ja  va2  s  .c  om*/
    PrintStream outps = null;
    OutputStream erros = null;
    PrintStream errps = null;
    try {
        outos = new ByteArrayOutputStream();
        outps = new PrintStream(outos);
        erros = new ByteArrayOutputStream();
        errps = new PrintStream(erros);
        MavenCli cli = new MavenCli();
        int code = cli.doMain(args, dir, outps, errps);
        String out = outos.toString();
        String err = erros.toString();
        if (code != 0) {
            System.out.println("TEST MAVEN EXECUTION START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            System.out.print("Executing: mvn");
            for (String arg : args) {
                System.out.print(' ');
                System.out.print(arg);
            }
            System.out.println();
            System.out.print("Exit code: ");
            System.out.println(code);
            System.out.print(out);
            System.err.print(err);
            System.out.println("TEST MAVEN EXECUTION END <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        }
        return out + err;
    } finally {
        closeQuietly(errps);
        closeQuietly(erros);
        closeQuietly(outps);
        closeQuietly(outos);
    }
}